C#完成兩個窗體之間數值傳送的辦法。本站提示廣大學習愛好者:(C#完成兩個窗體之間數值傳送的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成兩個窗體之間數值傳送的辦法正文
本文實例講述了C#完成兩個窗體之間數值傳送的辦法。分享給大家供大家參考,詳細如下:
以下是自己常用的辦法,其實辦法很多,但我覺得這兩種我比擬好了解,要是哪位冤家有比擬復雜的易懂的其他辦法,希望不吝賜教。
辦法一:
比方要在FORM2裡失掉FORM1裡的值,先在FORM1裡定義一個私有的字符串
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();
這樣運轉後是將FORM2的文本框的值設為“aaaa”,並且顯示在FORM1裡的文本框裡
實例演示
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;
}
}
}
運轉效果截圖如下:
希望本文所述對大家C#順序設計有所協助。