问题描述
一旦我关闭表格并重新打开,赢取表格上输入的值就会被删除。
如何保存输入的值
我尝试了什么:
private button1_click(object sender,EventArgs e)
{
Form2 F2 = new Form2();
F2.show();
}
The values entered on the win forms are deleting once i close the form and reopen it.
how i can the save the entered values
What I have tried:
private button1_click(object sender,EventArgs e)
{
Form2 F2 = new Form2();
F2.show();
}
推荐答案
private string userName = "OriginalGriff";
...
MyForm mf = new MyForm();
mf.UserName = userName;
if (mf.Show() == DialogResult.OK)
{
userName = mf.UserName;
}
您存储此类信息的确切位置取决于您 - 上面的示例只是在显示新表单的表单中将其保存在类级别 - 它取决于您可以使用的内容以及您希望数据的持久性!
Exactly where you store such info is up to you - the example above just holds it at class level in the Form that displays the new form - it depends on what is available to you and how persistent you want the data to be!
private Form2 f2;
private void Form1_Load(object sender, EventArgs e)
{
f2 = new Form2();
// subscribe to the Action/Event on Form2
f2.SendDataAction += SendDataAction;
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
f2.Show();
}
// Action/Event to "broadcast" data
private void SendDataAction(string tboxtxt, int cboxitmndx, string cbxitmtxt)
{
// handle the incoming data here
}
2。在第二个Form ...假设有一个TextBox'textBox1,一个Button'btnSendData和一个ComboBox'comboBox1:
2. In the second Form ... assume there's a TextBox 'textBox1, a Button 'btnSendData, and a ComboBox 'comboBox1 :
public Action<string,> SendDataAction {set; get; }
private void btnSendData_Click(object sender, EventArgs e)
{
if (SendDataAction != null)
{
SendDataAction(textBox1.Text, comboBox1.SelectedIndex, comboBox1.SelectedText);
}
}
这里会发生什么:
1.在Form2中一个代表(Action),它带有三个参数,字符串,整数和字符串,被定义为公共财产。
1.a.当单击'btnSendData时,如果Action不为null(有订阅者),则调用Action,传递TextBox和ComboBox控件中的值。
2在Form1中,你会注意到在创建了Form2'f2的实例后,我们可以通过使用+ =运算符传递对我们创建的'SendDataAction ...的方法的引用来订阅Form2上的Action / Event,我们在其InvocationList中添加了一个方法。
2.a.因此,我们在Form1中定义的'SendDataAction方法将通过Form2中的Button单击调用,并将接收数据。
请记住一个尺寸确实不适合所有:在其他情况下,当第一个(主要)表格中发生某些事件时,您可能希望按需检索第二个表格中的数据。这需要另一种策略。您可能希望强制第二个表格在用户关闭时通知第一个表格,并传递所输入的任何数据。
正式化转移的一个原因从一个上下文(表单)到另一个上下文的数据......如此处所示......是为了实现封装,以避免在第二个表单上发生的任何事情可能以某种方式搞砸第一个表单,并且,相反。
第二个表格不知道发送数据行动/事件有什么/多少订阅者:它只是要广播给所有人他们。
注意:'Action和'Func是用.NET 3.5添加到.NET的代理的替代编写方法(语法)。与所有代表一样,它们是多播的(支持多个订阅者)。
What happens here:
1. in Form2 a Delegate (Action) that takes three parameters, a string, an int, and a string, is defined as a Public Property.
1.a. when the 'btnSendData is clicked, if the Action is not null (has subscribers), the Action is invoked, passing the values from the TextBox and ComboBox Controls.
2. in Form1, you'll note that after creating the instance of Form2 'f2, then we can subscribe to the Action/Event on Form2 by passing a reference to the method we create 'SendDataAction ... by using the += operator, we add a method to its InvocationList.
2.a. so, the 'SendDataAction method we defined in Form1 will get invoked by the Button click in Form2, and will receive the data.
Keep in mind that "one size does not fit all:" in other circumstances you may wish to retrieve the data in the second Form "on demand" when some event occurs in the first (Main) form. That calls for another strategy. You may wish to force the second Form to notify the first Form when the user closes it, passing whatever data has been entered.
One reason for "formalizing" transfer of data from one context (Form) to another ... as shown here ... is to achieve "encapsulation," to avoid any possibility that anything that happens on the second Form can somehow "screw up" the first Form, and, the reverse.
The second Form "has no idea" what/how-many subscribers there are to the 'SendDataAction Action/Event: it's just going to "broadcast" to all of them.
Note: 'Action and 'Func are alternative ways of writing (syntax for) a Delegate added to .NET with .NET 3.5. As with all Delegates, they are multi-cast (support multiple subscribers).
这篇关于如何保存在文本框中输入的值,表单中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!