C#實(shí)現(xiàn)兩個窗體之間數(shù)值傳送的方法
本文實(shí)例講述了C#實(shí)現(xiàn)兩個窗體之間數(shù)值傳送的方法。分享給大家供大家參考,具體如下:
以下是本人常用的方法,其實(shí)方法很多,但我覺得這兩種我比較好理解,要是哪位朋友有比較簡單的易懂的其他方法,希望不吝賜教。
方法一:
比如要在FORM2里得到FORM1里的值,先在FORM1里定義一個公有的字符串
然后FORM2里用FORM1去實(shí)例化一個對象
最后用 f.zhi來取得FORM1里的值。(f.Show()也是一個道理,即對象名.方法名)
方法二:
比如要在FORM1里得到FORM2里的值,利用GET,SET方法。
在FORM2里放一個TEXTBOX,寫一個公有屬性
public string transsformValue
{
get
{
return this.textBox1.Text;
}
set
{
this.textBox1.Text=value;
}
}
在FORM1里這么寫(在里面也加一個TEXTBOX):.
FORM2 f=new FORM2(); f.transsformValue="aaaa"; textBox1=f.transsformValue; f.Show();
這樣運(yùn)行后是將FORM2的文本框的值設(shè)為“aaaa”,并且顯示在FORM1里的文本框里
實(shí)例演示
FORM1里這么寫:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication17
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
InputBox f = new InputBox();
f.Title = "請輸入對話框";
f.TipText = "請輸入年齡";
if (f.ShowDialog() == DialogResult.OK)
this.label1.Text = f.Message;
}
}
}
//InputBox的FORMl里這么寫
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication17
{
public partial class InputBox : Form
{
public InputBox()
{
InitializeComponent();
}
public string Title
{
set { this.Text = value; }
}
public string Message
{
get { return this.Input.Text; }
}
public string TipText
{
set { this.Tip.Text = value; }
}
private void InputBox_Load(object sender, EventArgs e)
{
this.AcceptButton = this.btnOK;
this.CancelButton = this.btnCancel;
this.btnOK.DialogResult = DialogResult.OK;
this.btnCancel.DialogResult = DialogResult.Cancel;
}
}
}
運(yùn)行效果截圖如下:

希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
WPF實(shí)現(xiàn)繪制統(tǒng)計(jì)圖(柱狀圖)的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何基于WPF實(shí)現(xiàn)實(shí)現(xiàn)統(tǒng)計(jì)圖(柱狀圖)的繪制,文中的示例代碼簡潔易懂,對我們學(xué)習(xí)WPF有一定幫助,感興趣的可以了解一下2022-07-07
C# TreeView從數(shù)據(jù)庫綁定數(shù)據(jù)的示例
這篇文章主要介紹了C# TreeView從數(shù)據(jù)庫綁定數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#調(diào)用Matlab生成的dll方法的詳細(xì)說明
這篇文章詳細(xì)介紹了C#調(diào)用Matlab生成的dll方法,有需要的朋友可以參考一下2013-09-09
c# WPF中System.Windows.Interactivity的使用
這篇文章主要介紹了c# WPF中System.Windows.Interactivity的使用,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
c#方法中調(diào)用參數(shù)的值傳遞方式和引用傳遞方式以及ref與out的區(qū)別深入解析
以下是對c#方法中調(diào)用參數(shù)的值傳遞方式和引用傳遞方式,以及ref與out的區(qū)進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-07-07

