[英]Accessing properties from another form
I have a form with some elements in it. 我有一个包含一些元素的表单。 I want to access the data from that form in my main one.
我想从我的主表单中访问该表单中的数据。 I tried doing the following:
我尝试过以下操作:
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.emailServer = textBox1.Text;
Properties.Settings.Default.emailServerPort = (int)numericUpDown1.Value;
Properties.Settings.Default.emailServerSsl = (radioButton1.Checked == true) ? true : false;
Properties.Settings.Default.Save();
this.Close();
}
I can load it perfectly fine in the existing form using this code: 我可以使用以下代码在现有表单中完美地加载它:
private void EmailSettings_Load(object sender, EventArgs e)
{
textBox1.Text = Properties.Settings.Default.emailServer;
numericUpDown1.Value = Properties.Settings.Default.emailServerPort;
if (Properties.Settings.Default.emailServerSsl == true)
{
radioButton1.Checked = true;
}
else
{
radioButton1.Checked = false;
}
}
HOWEVER, my issue is that I can't access Properties
from my main form. 但是,我的问题是我无法从我的主表单访问
Properties
。 So I still can't access the data from that form. 所以我仍然无法访问该表单中的数据。 How can I access the data either from properties in my main form or from another form?
如何从主表单或其他表单中的属性访问数据?
You have changed settings .You can access to settings from every where from your project . 您已更改设置。您可以从项目的每个位置访问设置。 Here you have set settings in form2 then you can access it from main form
您在此处设置了form2中的设置,然后您可以从主窗体中访问它
var setting=Properties.Settings.Default.YourSetting;
If namespace in form2 and main form are different You must write like 如果form2和main形式的命名空间不同你必须写得像
var setting=namespaceOfForm2.Properties.Settings.Default.YourSetting;
Also you can below solution 你也可以在解决方案之下
On the second form create a property 在第二个表单上创建一个属性
public yourType yourProperty { get;set;}
Store your data to it 将数据存储到它
yourProperty=yourData;
// set this.DialogResult property if you successfully stored data
this.DialogResult=DialogResult.OK;
And from main form get it 从主要形式得到它
Form2 f=new Form2();
if(f.ShowDialog==DialogResult.OK)
{
var yourData=f.yourProperty;
}
Example: 例:
On form2: 在form2上:
public string Name { get; set;}
public void methodThatYouSetData()
{
Name=txtbx.Text;
this.DialogResult=DialogResult.OK;
}
On main form: 在主要表格上:
Form2 f=new Form2();
if(f.ShowDialog==DialogResult.OK)
{
string Name=f.Name;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.