[英]Send values from one form to another form
我想在两个表单(c#)之间传递值。 我该怎么做?
我有两种形式:Form1 和 Form2。
Form1 包含一个按钮。 当我单击该按钮时,Form2 应该打开并且 Form1 应该处于非活动模式(即不可选择)。
Form2 包含一个文本框和一个提交按钮。 当我在 Form2 的文本框中键入任何消息并单击提交按钮时,Form2 应该关闭并且 Form1 应该突出显示提交的值。
我该怎么做? 有人可以通过一个简单的例子帮助我做到这一点。
对此有几种解决方案,但这是我倾向于使用的模式。
// Form 1
// inside the button click event
using(Form2 form2 = new Form2())
{
if(form2.ShowDialog() == DialogResult.OK)
{
someControlOnForm1.Text = form2.TheValue;
}
}
和...
// Inside Form2
// Create a public property to serve the value
public string TheValue
{
get { return someTextBoxOnForm2.Text; }
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(textBox1.Text);
frm2.Show();
}
public Form2(string qs)
{
InitializeComponent();
textBox1.Text = qs;
}
定义属性
public static class ControlID {
public static string TextData { get; set; }
}
在form2
private void button1_Click(object sender, EventArgs e)
{
ControlID.TextData = txtTextData.Text;
}
获取form1
和form3
的数据
private void button1_Click(object sender, EventArgs e)
{
string text= ControlID.TextData;
}
经过一系列将数据从一种形式传递到另一种形式的斗争后,我终于找到了一个稳定的答案。 它就像魅力一样。
您需要做的就是以一种形式将public static datatype 'variableName'
声明为public static datatype 'variableName'
,并将值分配给要传递给另一种形式的该变量,并直接使用表单名称(不要创建这种形式的对象作为静态变量可以直接访问)并访问这个变量值。
这样的例子是,
表格1
public static int quantity;
quantity=TextBox1.text; \\Value which you want to pass
表格2
TextBox2.Text=Form1.quantity;\\ Data will be placed in TextBox2
在form1
声明一个公共字符串
public string getdata;
在form1
按钮中
form2 frm= new form2();
this.hide();
form2.show();
要将数据发送到form1
您可以尝试该事件中的任何事件和代码
form1 frm= new form1();
form1.getdata="some string to be sent to form1";
现在在关闭form2
并打开form1
,您可以在getdata
字符串中使用返回的数据。
在 form1 公共字符串 TextBoxString 中声明字符串;
在form1点击事件中添加
private void button1_Click(object sender, EventArgs e)
{
Form1 newform = new Form1();
newform = this;
this.Hide();
MySecform = new Form2(ref newform);
MySecform.Show();
}
在 form2 构造函数中
public Form2(ref Form1 form1handel)
{
firstformRef = form1handel;
InitializeComponent();
}
在 form2 crate 变量 Form1 firstformRef 中;
private void Submitt_Click(object sender, EventArgs e)
{
firstformRef.TextBoxString = textBox1.Text;
this.Close();
firstformRef.Show();
}
在此代码中,您将文本传递给 Form2。 Form2 显示 textBox1 中的文本。 用户在 textBox1 中键入新文本并按下提交按钮。 Form1 抓取该文本并将其显示在 Form1 上的文本框中。
public class Form2 : Form
{
private string oldText;
public Form2(string newText):this()
{
oldText = newText;
btnSubmit.DialogResult = DialogResult.OK;
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = oldText;
}
public string getText()
{
return textBox1.Text;
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
}
这是 Form1 代码:
public class Form1:Form
{
using (Form2 dialogForm = new Form2("old text to show in Form2"))
{
DialogResult dr = dialogForm.ShowDialog(this);
if (dr == DialogResult.OK)
{
tbSubmittedText = dialogForm.getText();
}
dialogForm.Close();
}
}
好的,所以Form1
有一个文本框,首先你必须在 textbox 属性中将此Form1
文本框设置为 public 。
代码表1:
Public button1_click()
{
Form2 secondForm = new Form2(this);
secondForm.Show();
}
在构造函数中将Form1
作为this
传递。
代码表格2:
Private Form1 _firstForm;
Public Form2(Form1 firstForm)
{
_firstForm = firstForm:
}
Public button_click()
{
_firstForm.textBox.text=label1.text;
This.Close();
}
表格 1 代码:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
MessageBox.Show("Form1 Message :"+Form2.t.Text); //can put label also in form 1 to show the value got from form2
}
表格 2 代码:
public Form2()
{
InitializeComponent();
t = textBox1; //Initialize with static textbox
}
public static TextBox t=new TextBox(); //make static to get the same value as inserted
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
有用!
您可以将 Form1 的文本框作为参数传递,如下所示:
在 Form 1 按钮处理程序上:
private void button2_Click(object sender, EventArgs e)
{
Form2 newWindow = new Form2(textBoxForReturnValue);
newWindow.Show();
}
在表格 2 上
public static TextBox textBox2; // class atribute
public Form2(TextBox textBoxForReturnValue)
{
textBox2= textBoxForReturnValue;
}
private void btnClose_Click(object sender, EventArgs e)
{
textBox2.Text = dataGridView1.CurrentCell.Value.ToString().Trim();
this.Close();
}
构造函数是在表单或 Gui 对象之间传递数据的最佳方式,您可以这样做。 在 form1 单击按钮中,您应该具有:
Form1.Enable = false;
Form2 f = new Form2();
f.ShowDialog();
在表单 2 中,当用户点击按钮时,它应该有一个类似这样或类似的代码:
this.Close();
Form1 form = new Form1(textBox1.Text)
form.Show();
进入表单 1 的表单加载后,您可以添加代码以在从构造函数获取值时执行任何操作。
如何将值从表单传递到另一个表单
1.) 转到 Form2 然后双击它。 在代码中输入这个。
public Form2(string v)
{
InitializeComponent();
textBox1.Text = v;
}
2.) 转到 Form1 然后双击它。 在代码中输入这个。 //在Form1中的命令按钮上
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2(textBox1.Text);
F2.Show();
}
这很简单。 假设你有两个窗体 Form1 和 Form2,你想把 textbox1 的记录从 Form1 发送到 Form2 并在 Form2 的 label1 中显示这条记录; 然后在 Form2 中创建一个名为 label1 的标签并转到 label1 的属性并设置 'Modifiers'=public 并在 Form one 中创建一个带有 id textBox1 的文本框和一个名称为 submit 的按钮,然后在按钮单击事件上编写以下代码
button1_Click(object sender, EventArgs e)
{
Form2 obj=new Form2();
obj.label1.text=textBox1.text.ToString();
obj.show();
}
就是这样......通过这种方式你可以将数据集记录绑定到另一个表单的数据网格视图......
使用以下方法即可轻松获得价值:
var form1 = new Form1();
string sample = form1.examplestring;
设置值:
var form1 = new Form1();
form1.examplestring = example;
但检查字符串为“公共字符串”
public string example = "test";
如果您愿意,可以使用不同的方法。
使用 System.Action
您可以将其视为传递给子窗体的回调函数。
// -------- IN THE MAIN FORM --------
// CALLING THE CHILD FORM IN YOUR CODE LOOKS LIKE THIS
Options frmOptions = new Options(UpdateSettings);
frmOptions.Show();
// YOUR FUNCTION IN THE MAIN FORM TO BE EXECUTED
public void UpdateSettings(string data)
{
// DO YOUR STUFF HERE
}
// -------- IN THE CHILD FORM --------
Action<string> UpdateSettings = null;
// IN THE CHILD FORMS CONSTRUCTOR
public Options(Action<string> UpdateSettings)
{
InitializeComponent();
this.UpdateSettings = UpdateSettings;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
// CALLING THE CALLBACK FUNCTION
if (UpdateSettings != null)
UpdateSettings("some data");
}
OpenForms 方法
这种方法很简单( 2 行)。 但仅适用于打开的表单。 你需要做的就是在你想要传递一些数据的地方添加这两行。
Main frmMain = (Main)Application.OpenForms["Main"];
frmMain.UpdateSettings("Some data");
我在这里提供了对类似问题的回答
你可以用这个;
表单 1 按钮 1 单击
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
this.Hide();
frm2.Show();
}
并将其添加到 Form2
public string info = "";
表单 2 按钮 1 单击
private void button1_Click(object sender, EventArgs e)
{
info = textBox1.Text;
this.Hide();
BeginInvoke(new MethodInvoker(() =>
{
Gogo();
}));
}
public void Gogo()
{
Form1 frm = new Form1();
frm.Show();
frm.Text = info;
}
如果将 Form 中某个控件的 Modifiers 属性更改为 Public,则另一个 Form 可以访问该控件。 铁:
Form2 frm;
private void Form1_Load(object sender, EventArgs e)
{
frm = new Form2();
frm.Show();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(frm.txtUserName.Text);
//txtUserName is a TextBox with Modifiers=Public
}
标签属性接收对象值(C#将值发送到另一个表单)
private void btn_Send_Click(object sender, EventArgs e)
{
Form frm = new formToSend();
frm.tag = obj;
frm.ShowDialog();
}
接收从前一个表单发送的值( frm )
例如:发送的数据是字符串(我们需要先进行类型转换,因为标签值是一个对象)
public Receive_Form()
{
InitializeComponent();
MessageBox.Show((string)this.Tag);
}
如何使用公共事件
我会这样做。
public class Form2
{
public event Action<string> SomethingCompleted;
private void Submit_Click(object sender, EventArgs e)
{
SomethingCompleted?.Invoke(txtData.Text);
this.Close();
}
}
并像这样从 Form1 调用它。
private void btnOpenForm2_Click(object sender, EventArgs e)
{
using (var frm = new Form2())
{
frm.SomethingCompleted += text => {
this.txtData.Text = text;
};
frm.ShowDialog();
}
}
然后,当 Form2 关闭时,Form1 可以从 Form2 获取文本
谢谢你。
// In form 1
public static string Username = Me;
// In form 2's load block
string _UserName = Form1.Username;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.