如何在Windows窗体中将数据从一种形式传输到另一种形式? [英] How to transfer data from one form to another form in windows forms?

查看:105
本文介绍了如何在Windows窗体中将数据从一种形式传输到另一种形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是Windows窗体的新手!

我创建了2个表单'Form1.cs'和'ComplainAssignment.cs'

''ComplainAssignment.cs'上的标签显示来自'Form1.cs'的文本TextBox控件名称'CustomerNametxt'

我想将'Form1'数据传递给'ComplainAssignment'表单,我是使用构造函数方法传递数据。

但'ComplainAssignment.cs'上的'LabelControl1'没有将'Form1.cs'TextBox中的Text显示到Label Control。

请帮助!

谢谢!



保存代码按钮:



  private   void  SaveButton_Click( object  sender,EventArgs e)
{

ComplainAssignment frm = new ComplainAssignment(Customer_Nametxt.Text);
frm.Show();
}





ComplainAssignment代码表格及字符串:

  public   partial   class  ComplainAssignment:表格
{
public ComplainAssignment( string strTextBox)
{
InitializeComponent();
labelControl1.Text = strTextBox;


}

解决方案

参考



在表单之间传递数据 [ ^ ]

如何在窗口应用程序的c#中将数据从一种形式传输到另一种形式。 [ ^ ]

http://www.dotnetfunda.com/forums/show/9754/how-to-pass-data-from-one-windows-form-to-another [ ^ ]


假设您有表格A和B; A是主窗体,当您单击A中的某个按钮时,您希望显示窗体B.方便的方法是以对话方式显示B,这样可以防止您在应用程序中的任何其他位置单击,直到它关闭。 br />


在B上你需要一个公共财产; public修饰符表示其他类可以访问该属性以读取或修改它。 B中有这样的东西...



  private   string  Btext =  String  .Empty; 

public string _Btext
{
get { return Btext;}
set {Btext = value ;}
}









假设你在B中有一个文本框(textbox1)和一个按钮(button1)触发...

  private   void  button1_Click( object  sender,EventArgs e)
{
_Btext = textbox1.Text; // 为项目提供文本框值
.Close(); // 关闭B表格
}





所以现在你在B中有一个公共财产,所以创建B实例的任何其他类都可以访问该值。





现在,在A上我们有一个显示B的按钮(buttonDisplayB),让你获取它的价值......



  private   void  buttonDisplayB_Click( object  sender,EventArgs e)
{
B formB = new B(); // 创建表单B的实例
formB.ShowDialog(); // 以对话框形式显示B,因此您可以输入文本并使用按钮关闭

// 如果您使用button1关闭UI上的B,您将拥有公共属性中可用的值

string valueFromB = formB._Btext;
// 现在你有A的B值
}


制作表单的对象并访问所有控件。







另一个是在第一页上制作公共变量并在第二页上访问它。

Hi,
I am new to windows forms!
I have created 2 forms 'Form1.cs' and 'ComplainAssignment.cs'
A Label on 'ComplainAssignment.cs' to show the Text from 'Form1.cs' TextBox control name 'CustomerNametxt'
I want to pass 'Form1' data to 'ComplainAssignment' form, I am using constructor approach to pass data.
But the 'LabelControl1' on 'ComplainAssignment.cs' not displaying the Text from 'Form1.cs' TextBox to the Label Control.
Kindly Help!
Thank You!

Code of Save Button:

private void SaveButton_Click(object sender, EventArgs e)
       {

           ComplainAssignment frm = new ComplainAssignment(Customer_Nametxt.Text);
           frm.Show();
       }



Code Of ComplainAssignment Form with string:

public partial class ComplainAssignment : Form
    {
        public ComplainAssignment(string strTextBox)
        {
            InitializeComponent();
            labelControl1.Text = strTextBox;
           
            
        }

解决方案

refer

Passing Data Between Forms[^]
How to transfer data from one form to another form in c# in window application.[^]
http://www.dotnetfunda.com/forums/show/9754/how-to-pass-data-from-one-windows-form-to-another[^]


Let's say you have forms A and B; A is the main form, and you want form B to display when you click some button in A. Convenient way to do so is having B show in dialog fashion, which prevents you from clicking anywhere else in the application until it is closed.

On B you need a "public" property; the public modifier means that other classes may access that property to read it or modify it. So have in B something like...

private string Btext = String.Empty;

public string _Btext
{
    get {return Btext;}
    set {Btext = value;}
}





And suppose you have a textbox (textbox1) and a button (button1) in B that triggers...

private void button1_Click(object sender, EventArgs e)
{
   _Btext = textbox1.Text; //gives the textbox value to the property
   this.Close(); //closes B form
}



So now you have a public property in B, so any other class that creates an instance of B will be able to access that value.


Now, on A let's have a button (buttonDisplayB) which displays B and let's you fetch its value...

private void buttonDisplayB_Click(object sender, EventArgs e)
{
  B formB = new B(); //Creates an instance of form B
  formB.ShowDialog(); //Displays B in dialog form, so you can enter text and close with button

  //if you close B on UI with button1, you'll have the value available in the public property
  
  string valueFromB = formB._Btext;
  //now you have B's value in A
}


make object of the form and access the all controls.

and

another is make a public variable on first page and access this on second form.


这篇关于如何在Windows窗体中将数据从一种形式传输到另一种形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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