Windows窗体的数据处理中存在问题. [英] Having issues in data handling of windows forms.
问题描述
亲爱的所有人,
我是C#Windows编程的新手,在处理Windows窗体的数据时遇到问题.
这是场景.
我创建了一个Windows窗体(名为form1),在其中通过各种控件(例如文本框,列表框,组合框)进行输入,并尝试以另一种形式显示它,直到在其中显示一个Windows窗体的输入值为止.第二种形式工作正常,但是当我尝试以第三种形式访问form1的值以便基于form1值进行一些计算时,我无法访问它们.
在这种情况下,请帮助我如何访问第三种形式的form1的值.
以下是我如何执行此操作的代码
Dear All,
I am new in C# windows programming and having issues in handling data of windows forms.
Here is the scenario.
I have created one windows form (named as form1) in which i am taking input via various controls such as text boxes, list boxes, combo boxes and trying to show it in another form , till showing one windows form''s input value in second form is working fine but when I try to access values of form1 in third form in order to do some computation based on form1 values I am unable to access them.
Please help me in this scenario that how could access the values of form1 in the third form.
Following is the code that how i am doing this
namespace SmallTestApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
previewinput dos = new previewinput();
dos.Show();
dos.label2.Text = textBox1.Text;
}
public string getdataform3()
{
Form3 form3obj = new Form3();
form3obj.getvar = textBox1.Text;
return form3obj.getvar;
}
}
}
namespace SmallTestApplication
{
public partial class previewinput : Form
{
public previewinput()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 objform3 = new Form3();
objform3.Show();
}
}
}
namespace SmallTestApplication
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public string getvar;
private void button1_Click(object sender, EventArgs e)
{
Form1 objform1 = new Form1();
MessageBox.Show(objform1.textBox1.Text)
//MessageBox.Show(objform1.getdataform3());
//previewinput dos = new previewinput();
//MessageBox.Show(dos.label2.Text);
}
}
}
谢谢,
Fahad Bin Aziz.
Thanks,
Fahad Bin Aziz.
推荐答案
这是有关表单协作的流行问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].
—SA
这篇关于Windows窗体的数据处理中存在问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!