Windows 应用程序 (C#.Net)- 保留文本框值 [英] Windows Application (C#.Net)- Retain TextBox Values
问题描述
这是我的场景:(Windows 应用程序/C#.Net)
Here is my Scenario: (Windows Application/C#.Net)
我有一个 Form1,它正在使用选择为 Case 1 和 On Button Click it 的选项进行一些计算,将所有文本框值推送到 Form2.这工作正常.
I have a Form1 in that is doing some calculations with the option selected as Case 1 and On Button Click it Pushes all the textbox values to Form2. This is working fine.
现在,如果我再次进入 Form 1 并将选项更改为 case 2 并计算并按下 Button 它会删除之前添加的值.
Now if I come to Form 1 again and change the option to case 2 and calculate and press Button it erases the values that were added earlier.
我想保留这些值并将这些值添加到 Form2 中的另一组文本框.
I want to retain those values and add this values to another set of textboxes that are in Form2.
代码:在点击添加按钮后的表格 1 中:
Code: In Form 1 After Clicking ADD button:
private void button1_Click(object sender, EventArgs e)
{
Form2 eigthForm = new Form2();
if(comboBox1.Text == "Case 1")
{
eigthForm.text2.Text = textBox1.Text;
eigthForm.text3.Text = textBox8.Text;
}
if (comboBox1.Text == "Case 2")
{
eigthForm.text4.Text = textBox1.Text;
eigthForm.text5.Text = textBox8.Text;
}
推荐答案
好的,我明白你现在在问什么.您可以使用单例模式作为一种方式如下
Ok I understand what you are asking now. You can use the Singleton pattern as one way as follows
Form1 按钮
private void button1_Click(object sender, EventArgs e)
{
Form2.Instance.AddText1and2("Hello", "World");
}
private void button2_Click(object sender, EventArgs e)
{
Form2.Instance.AddText3("Foo");
}
Form2 我们将改为单例模式
Form2 we will change to Singleton pattern
private static volatile Form2 instance = null;
private static object lockThis = new object();
private Form2()
{
InitializeComponent();
}
public static Form2 Instance
{
get
{
if (instance == null)
{
lock(lockThis)
{
if (instance == null)
{
instance = new Form2();
}
}
}
return instance;
}
}
现在我们将文本放置在正确位置的方法
Now our methods to place text in the correct places
public void AddText1and2(string txt1, string txt2)
{
textBox1.Text = txt1;
textBox2.Text = txt2;
this.Show();
}
public void AddText3(string txt3)
{
textBox3.Text = txt3;
this.Show();
}
如果您希望能够关闭窗口并重新打开它以保存值,您需要使用表单关闭事件
If you want to be able to close the window and reopen it saving values you need to use the form closing event
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
您必须阻止实际关闭.这将处置该对象,而我们不希望那样.
You have to prevent the actual close. This will dispose the object and we don't want that.
这会将您的所有设置保留在表格 2 中.
This will keep all your settings in form 2.
如果你不想做上面的事情,你可以在你的项目中添加一个新类,做这样的事情
if you don't want to do the above you can add a new class to your project doing something like this
class Global
{
public static string Value1 { get; set; }
public static string Value2 { get; set; }
}
在 Form1 中
if(comboBox1.Text == "Case 1")
{
Global.Value1 = textBox1.Text;
Global.Value2 = textBox8.Text;
}
在 Form2 加载事件中
in Form2 load event
text2.Text = Global.Value1;
text3.Text = Global.Value2;
<小时>
你还应该修改你的 if 逻辑如果它是案例 1",则组合框永远不会 ==案例 2",但您还在运行下一个if语句
You should also revise your if logic The combobox will never == "Case 2" if it's "Case 1" but you Are still running the next if statement
你应该使用这样的东西
if(comboBox1.Text.Equals("Case 1"))
{
}
else if (comboBox1.Text.Equals("Case 2"))
{
}
或
switch (comboBox1.Text)
{
case "Case 1":
//do stuff
break;
case "Case 2":
//do stuff
break;
}
这篇关于Windows 应用程序 (C#.Net)- 保留文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!