在 Windows 窗体 C# 之间传递值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17836398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing Values Between Windows Forms c#
提问by Trevor Daniel
I am struggling to work out how to pass values between forms. I have four forms and I want to pass the information retrieved by the Login
to the fourth and final form.
我正在努力研究如何在表单之间传递值。我有四个表单,我想将检索到的信息传递Login
给第四个也是最后一个表单。
This is what I have so far.
这是我到目前为止。
In this function:
在这个函数中:
private void btnLogin_Click(object sender, EventArgs e)
I have deserialized the data I want like this:
我已经反序列化了我想要的数据:
NewDataSet resultingMessage = (NewDataSet)serializer.Deserialize(rdr);
Then, when I call the next form I have done this:
然后,当我调用下一个表单时,我已经这样做了:
Form myFrm = new frmVoiceOver(resultingMessage);
myFrm.Show();
Then, my VoiceOver
form looks like this:
然后,我的VoiceOver
表单如下所示:
public frmVoiceOver(NewDataSet loginData)
{
InitializeComponent();
}
private void btnVoiceOverNo_Click(object sender, EventArgs e)
{
this.Close();
Form myFrm = new frmClipInformation();
myFrm.Show();
}
When I debug, I can see the data is in loginData
in the second form, but I cannot seem to access it in the btnVoiceOverNo_Click
event. How do I access it so I can pass it to the next form?
当我调试时,我可以看到数据在loginData
第二种形式中,但我似乎无法在btnVoiceOverNo_Click
事件中访问它。我如何访问它以便我可以将它传递给下一个表单?
采纳答案by Adam Houldsworth
You need to put loginData
into a local variable inside the frmVoiceOver
class to be able to access it from other methods. Currently it is scoped to the constructor:
您需要loginData
在frmVoiceOver
类中放入一个局部变量才能从其他方法访问它。目前它的范围是构造函数:
class frmVoiceOver : Form
{
private NewDataSet _loginData;
public frmVoiceOver(NewDataSet loginData)
{
_loginData = loginData;
InitializeComponent();
}
private void btnVoiceOverNo_Click(object sender, EventArgs e)
{
// Use _loginData here.
this.Close();
Form myFrm = new frmClipInformation();
myFrm.Show();
}
}
Also, if the two forms are in the same process you likely don't need to serialize the data and can simply pass it as a standard reference to the form's constructor.
此外,如果两个表单在同一个进程中,您可能不需要序列化数据,只需将其作为标准引用传递给表单的构造函数。
Google something like "C# variable scope" to understand more in this area as you will encounter the concept all the time. I appreciate you are self-taught so I'm just trying to bolster that :-)
谷歌类似“ C# 变量作用域”之类的东西来了解更多这方面的内容,因为你会一直遇到这个概念。我很感激你是自学成才的,所以我只是想加强这一点:-)
回答by Sarath Avanavu
In various situations we may need to pass values from one form to another form when some event occurs. Here is a simple example of how you can implement this feature.
在各种情况下,当某些事件发生时,我们可能需要将值从一种形式传递到另一种形式。这是一个如何实现此功能的简单示例。
Consider you have two forms Form1
and Form2
in which Form2 is the child of Form1. Both of the forms have two textboxes in which whenever the text gets changed in the textbox of Form2
, textbox of Form1
gets updated.
假设您有两个窗体Form1
,Form2
其中 Form2 是 Form1 的子级。这两种形式都有两个文本框,其中每当 的文本框中的文本发生更改时, 的文本Form2
框Form1
就会更新。
Following is the code of Form1
以下是Form1的代码
private void btnShowForm2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.UpdateTextBox += new EventHandler<TextChangeEventArgs>(txtBox_TextChanged);
form2.ShowDialog();
}
private void txtBox_TextChanged(object sender, TextChangeEventArgs e)
{
textBox1.Text = e.prpStrDataToPass;
}
Following is the code of Form2
以下是Form2的代码
public event EventHandler<TextChangeEventArgs> UpdateTextBox;
private string strText;
public string prpStrText
{
get { return strText; }
set
{
if (strText != value)
{
strText = value;
OnTextBoxTextChanged(new TextChangeEventArgs(strText));
}
}
}
private void textBox_Form2_TextChanged(object sender, EventArgs e)
{
prpStrText = txtBox_Form2.Text;
}
protected virtual void OnTextBoxTextChanged(TextChangeEventArgs e)
{
EventHandler<TextChangeEventArgs> eventHandler = UpdateTextBox;
if (eventHandler != null)
{
eventHandler(this, e);
}
}
In order to pass the values we should store your data in a class which is derived from EventArgs
为了传递值,我们应该将您的数据存储在从 EventArgs 派生的类中
public class TextChangeEventArgs : EventArgs
{
private string strDataToPass;
public TextChangeEventArgs(string _text)
{
this.strDataToPass = _text;
}
public string prpStrDataToPass
{
get { return strDataToPass; }
}
}
Now whenever text changes in Form2, the same text gets updated in textbox of Form1.
现在,每当 Form2 中的文本更改时,Form1 的文本框中都会更新相同的文本。