如何保存在文本框中输入的值,表单中的列表框 [英] How do I save the values entered on the text box, list boxes in the forms

查看:86
本文介绍了如何保存在文本框中输入的值,表单中的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦我关闭表格并重新打开,赢取表格上输入的值就会被删除。

如何保存输入的值



我尝试了什么:



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();
}

推荐答案

除非您专门将表单数据保存在某处,否则不会保留表单数据,并在重新显示表单时将其还原,就像创建FileOpen对话框时一样:上次使用的文件名不是' t显示,但文件夹保持与上次相同。那是因为文件名没有保留,但是文件夹是。



如果你希望表单显示现有数据,那么你必须传递数据(或者每次创建表单实例时让它知道要使用哪些数据的标记。这可以通过表单构造函数或表单中的属性来完成。

Form data isn't preserved unless you specifically save it somewhere, and restore it when the form is re-displayed, just as it isn't when you create a FileOpen dialog: the filename you used last time isn't displayed, but the folder remains the same as last time. That's because the file name isn't preserved, but the folder is.

If you want your forms to show existing data, then you have to pass it the data (or a token which lets it know which data to use) each time you create the form instance. This can be done via the Form Constructor, or via properties in the form.
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!


在您的代码示例中,您在按钮中创建表单的实例单击EventHandler。一旦退出EventHandler代码,实例(对象)将不复存在;你没有参考它。



假设你希望用户在表格上输入一些信息,然后你想从另一个表格中获取那些信息,那么有几种策略,OriginalGriff在这些策略中有三篇很好的系列文章:[ ^ ]。



战略的一个例子...我希望能补充OriginalGriff概述的内容如下:



1.在主窗体中:
In your code example, you create an instance of a Form in a Button Click EventHandler. That "instance" (object) will cease to exist once the EventHandler code is exited; you will have no reference to it.

Assuming you want the user to enter some information on a Form, and then you want to get that information from another Form, there are several strategies, and OriginalGriff has an excellent series of three articles here on some of those strategies: [^].

An example of a strategy ... which I hope will complement the ones outlined by OriginalGriff follows:

1. in the Main Form:
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).


这篇关于如何保存在文本框中输入的值,表单中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆