传递的Windows之间的值形状C# [英] Passing Values Between Windows Forms c#
问题描述
我在努力解决如何窗体间传递值。我有四种形式,我想通过由登录检索到的信息
来的第四次也是最后的形式。
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.
这是我迄今。
在此功能:
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();
然后,我的的VoiceOver
的形式是这样的:
public frmVoiceOver(NewDataSet loginData)
{
InitializeComponent();
}
private void btnVoiceOverNo_Click(object sender, EventArgs e)
{
this.Close();
Form myFrm = new frmClipInformation();
myFrm.Show();
}
当我调试,我可以看到的数据是 loginData code>在第二个形式,但我似乎无法访问它在
btnVoiceOverNo_Click
事件。如何访问它,所以我可以将它传递到下一个形式?
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 code>进入里面的
frmVoiceOver 局部变量code>类,以便能够从其他方法来访问它。目前,它的作用范围是构造:
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:
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.
谷歌像 C#变量范围了解更多这个区域,你会遇到这个概念所有的时间。我AP preciate你是自学成才,所以我只是想加强的是: - )
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 :-)
这篇关于传递的Windows之间的值形状C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!