Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
private void SaveButton_Click(object sender, EventArgs e)
       {

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


Code Of ComplainAssignment Form with string:
C#
public partial class ComplainAssignment : Form
    {
        public ComplainAssignment(string strTextBox)
        {
            InitializeComponent();
            labelControl1.Text = strTextBox;
           
            
        }
Posted
Comments
Krunal Rohit 4-Mar-14 1:07am    
Try to out breakpoint.

-KR
[no name] 4-Mar-14 1:07am    
Search on google you will find many solutions

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...

C#
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...
C#
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...

C#
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
}
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
dan!sh 4-Mar-14 1:29am    
Make controls public! That is simply wrong way of doing things.
You can declare a variable in Form1 as
public static string cus_name = "";


then assign customer name to this static variable as cus_name = CustomerNametxt.Text;


then access customer name in ComplainAssignment form as below

string cus_name=Form1.cus_name
 
Share this answer
 
Hi just create one module page and declare public variable,for eg if u create variable in module
VB
Public form1var  As String = "" ' This is vb chnage to c#


u can use anywhere in whole project.

just pass data in Form1.cs
" form1var=ur data"

and get in ComplainAssignment.cs page as "form1var"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900