将信息从一种形式传递到另一种形式 [英] Passing info from one form to another
问题描述
大家希望有人可以提供帮助。我在form1上有一个文本框,它自动从csv文件(电子邮件地址)填充。我需要将相同的文本框信息传递给另一个表单。然而,在网上阅读所有我能找到的是,你有
使用按钮点击事件 - 我不想这样做。
试图玩游戏但是,对于Load事件,文本框仍为空。每个表单下面的示例代码。课程会更好,因为我可能需要在全新的表格上再次使用这些信息
提前致谢
// Form One
public static string passingText;
private void Form1_Load(object sender,EventArgs e)
{
passingText = EmailtoTest.Text;
}
// Form2
private void Xerox_Load(对象发送者,EventArgs e)
{
EmailPublic.Text = Form1.passingText ;
}
嗨LloydyJ89,
这是因为
Form_Load()事件是 在表单首次加载时执行
,现在,我担心这个值of
" EmailtoTest .Text"是空的,因为你没有写一些东西到文本框,所以
" passingText" 也是空的。
你可以设置一个断点到该行来观察该值。
为了解决这个问题,我建议如下三个解决方案:
Button_Click 事件而不是
Form1_Load 事件,用于在Form1中设置 " passingText" 的值。
2,
如果你不想在界面上添加一个按钮,你也可以使用
textbox_Leave 事件:private void Form1_Load( object sender,EventArgs e)
{
// not here
// passingText = EmailtoTest.Text;
}
public static string passingText;
private void textBox1_Leave(object sender,EventArgs e)
{
//但这里
passingText = EmailtoTest.Text;
}
3,为
" passingText" 在
Form1_Load 事件(n
recommended ):
In Form1:
public static string passingText;
private void Form1_Load(object sender,EventArgs e)
{
passingText =" some records";
}
问候,
Stanly
Hi Guys hoping someone can help. I have a textbox on form1 which is automatically populated from a csv file (email address). I need to pass the same textbox information into another form. However reading online all i can find is that to do this you have to use a button click event - which i do not want to do.
Tried to play around with the Load event however the textbox remains empty. Sample code below from each form. Would a class be better as i might need to use the information again on a brand new form
Thanks in advance
//Form One public static string passingText; private void Form1_Load(object sender, EventArgs e) { passingText = EmailtoTest.Text; } //Form2 private void Xerox_Load(object sender, EventArgs e) { EmailPublic.Text = Form1.passingText; }
Hi LloydyJ89,
That is because the Form_Load() event isexecuted once when the form is first loaded, and now, I'm afraid the value of "EmailtoTest.Text" is empty since you did not write some thing to the textbox, so that the "passingText" is empty too.
You can set a breakpoint to the line to watch the value.
To solve this problem, I suggest three solutions as below:
1, Just as you searched, try to use a Button_Click event instead of Form1_Load event to set the value for the "passingText" in Form1.
2, If you do not want to add a button to the interface, you can also use textbox_Leave event:
private void Form1_Load(object sender, EventArgs e) { //not here //passingText = EmailtoTest.Text; } public static string passingText; private void textBox1_Leave(object sender, EventArgs e) { //but here passingText = EmailtoTest.Text; }
3, Give a fixed value for the "passingText" in the Form1_Load event(not recommended):
In Form1:
public static string passingText; private void Form1_Load(object sender, EventArgs e) { passingText = "some records"; }
Regards,
Stanly
这篇关于将信息从一种形式传递到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!