说明从其他形式获取价值. [英] Clarifcation on Getting Value from other forms.
本文介绍了说明从其他形式获取价值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我需要获取从Form2到Form1的值,
表格2在表格1中弹出.
目前我正在做的是
1.在Form2中创建公共静态变量.
2.在Form1中获取值,例如Form2.variablename.
这是最好的方法吗?
我们也可以这样做,
通过创建表单类的实例并进行访问,而无需创建静态变量.
最好的方法是什么?
问候,
Pal
Hi All,
I need to get values from Form2 to Form1,
Form 2 is pop up screen in form1.
Currently what I am doing is,
1. Creating public Static Variable in Form2.
2. getting Value in Form1 like Form2.variablename.
is this best way?
we can do like this also ,
without creating static variable, by creating instance for the form class and accessing.
Whats is the best ways to do this?
Regards,
Pal
推荐答案
否.不要使用静态-这是不必要和危险的,同样,不要使用变量,而要使用属性.
尝试始终避免使用静态属性-如果您不了解正在发生的情况,它们可能会导致讨厌的错误.
表格2:
No. Do not use static - it is unneccessary and dangerous, likewise, do not use a variable, use a property.
Try to avoid static properties at all times - they can cause nasty bugs if you do not realise what is happening.
Form2:
public string UserName
{
get { return userNameTextBox.Text}
set { userNameTextBox.Text = value; }
}
表格1:
Form1:
Form2 f = new Form2();
f.UserName = "JoeCool";
if (f.ShowDialog() == DialogResult.OK)
{
Console.WriteLine(f.UserName);
}
这篇关于说明从其他形式获取价值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文