Changing a label's text in another form in C#? - c#

I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button's text to be transferred to the other form's label. I have tried
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
But it does not work. Is there an easy, straight forward way of doing this?

You need to expose your label or its property.
In form 2:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
Then you can do:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;

You could modify the constructor of Form2 like this:
public Form2(string labelText)
{
InitializeComponent();
this.labelX1.Text = labelText;
}
then create Form2 passing in the text:
Form2 frm2 = new Form2(this.button1.text);

Or you can do this >>
((Label)frm2.Controls["labelX1"]).Text = "test";

inside form2 write this
public void ChangeLabel(string s)
{
labelX1.Text = s;
}
then where you create Form 2 do this
form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);

Is there an easy, straight forward way of doing this?
Easiest way is to make labelX1 a public member of form2. The issue you're having is because from Form1 code form2.labelX1 isn't visible. In form2 designer you can go to properties of labelX1 and set it's visibility to public/internal.
Better approach would be to expose labelX1.Text as a property which can be set in code outside the class.

form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();

the only think you have to do is to put the label of the other form as public
for instance:
Form1:
public System.Windows.Forms.Label txtInfo;
then in Form2
Form1 frm =new Form1();
frm.txtInfo.text="....."//you have access because is public

You can make labelX1 public, and it will work, but there is a better way to do this:
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms

I changed my parent window property to the following code:
this.MdiParent.Controls["label1"].Text = "test";

If you are needing to access the form2 from elsewhere in your code (like a button press for instance) you will not be able to see the instance of the form you create. To solve that I create a public instance to hold a reference to it like:
public form2 form2_pub;
Then after you create it you assign the new one to your public instance:
form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2
Now you can reference form2_pub throughout your routines.
Works for me at least.
Remember, in your setter you can run whatever other code you want.
For instance, I use the following to show what I want on another form by just setting show_scanning to true:
public bool show_scanning //turns on the scanning screen
{
set
{
scanning_pnl.Visible = true;
notReady_pnl.Visible = false;
timer1.Enabled = true;
}
}

Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method,
Go to your form Form2.Designer.cs
private System.Windows.Forms.Label labelX1;
Change 'private' into 'public'.
Now the labelX1 is visible to outside.

The another approach is
Just change the modifier property of label or text to public and now it allows to access the content of form1 textbox to label on another form
So the code is
private void button1_click(){
Form2 obj1 =new Form2();
Obj1.show();
Obj1.label1.text="welcome"+textbox1.Text;
}

Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.

Related

How can I pass data of datagridview to textboxes in other Form?

Datagridview is located in Form2, TextBoxes in Form1.
Call the Form 2 from Form1 with Show(); where is located dataGridView and then pass this information to textboxes in Form1.
Code Sample in Form2 :
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Form1 exportar = new Form1();
exportar.textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
exportar.comboBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
exportar.textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
exportar.textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
exportar.textBox4.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
exportar.dateTimePicker1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
exportar.dateTimePicker2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[6].Value.ToString();
exportar.textBox7.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[7].Value.ToString();
exportar.textBox8.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[8].Value.ToString();
exportar.textBox9.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[9].Value.ToString();
exportar.textBox10.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[11].Value.ToString();
}
This did not work, but when I place exportar.Show() passed the information. The problem is that doubles the Form1.
You need a reference of Form1 in Form2. You can pass it in the constructor of Form2
private Form1 _form1;
public Form2 (Form1 form1)
{
_form1 = form1;
}
You create and open Form2 like this from within Form1:
var form2 = new Form2(this);
form2.ShowDialog(this);
In order to be able to access the controls of the other form, you must change their Modifer to Internal in the properties window.
then you can set the values like this:
var row = dataGridView1.CurrentRow; // This is "the row".
// No detour through the index is necessary.
_form1.textBox1.Text = row.Cells[0].Value.ToString();
_form1.comboBox1.Text = row.Cells[1].Value.ToString();
But things get simpler if you use data binding. See: A Detailed Data Binding Tutorial
1.Pass it as cunstrctor parameter:
public Form2(string text){
Textbox1. text = text;
}
and
Form2 f = new Form2("something to send to the form");
f.Show();
2.Create a public property for Form2:
public string TheText {get{return TextBox1.Text;}; set {textBox1.Text = value;};}
and then from first form:
Form2 f = new Form2();
f.TheText = "Some text";
f.Show();
Either pass the data in the constructor of your other form, if it's mandatory. Or make a public method available in your other form that allows you to set the data separately.
E.g.
public void setTextBoxData(String text) { ,etc, etc }
You can then call that method on your second form, passing the value you require from the first form.

how to change a control property from another form

I have two forms in a windows form application. lets call them "first form" and "second form".
i want by clikcing on a button on second form, change the property of one of the controls of the first form. i've defined an event for this. by that i mean when i click on the second form's button, a method within the first form is called. here's the method:
// changes the visibility of the specified control
public void change_visibility()
{
this.new_customer_label.Visible = true;
}
but when i set a breakpoint on this method and check the value after it is executed. the property has't changed. what is wrong? thanks in advance
note: on the second form button's click event, i also close the form.
So firstly, Open up Form1.designer.cs and change the control to public
Form1 this will open Form 2.
Form2 frm2 = new Form2();
frm2.Owner = this;
frm2.Show();
Form2 this will change the property of a control in Form 1
(this.Owner as Form1).label1.Visible = true;
Here is an example of what you can do:
class Form1 : Form {
private Label labelInForm1;
public string LabelText {
get { return labelInForm1.Text; }
set { labelInForm1.Text = value; }
}
}
class Form2 : Form {
Form1 form1; // Set by the property
private Form1 Form1 {
get { return form1; }
set { form1 = value; }
}
private ChangeVisibility()
{
Form1.labelInForm1.Visible = true;
}
}
"note: on the second form button's click event, i also close the form."
Then it would probably be a better design to display the second form with ShowDialog() instead of Show(). Something like:
Form2 f2 = new Form2();
f2.ShowDialog(); // code STOPS here until "f2" is closed
this.new_customer_label.Visible = true;
By default, the designer generates code in the 'Form1.Designer.cs' class. In there, you can see that all the controls are set private, change them to public and then try again...
As you will search this problem on internet you will find different solutions but I think the best solution is to make controls public then you can access these controls from any form.
Follow these instructions.
Open your desire form whose control properties you want to access
Open Form.Designer.cs
Change the desire control class to public
Go to the main form where you want to access or change property
Write this code
Form Form2 objForm=new Form2();
Now access your control property here
objForm.new_customer_label.Visible=true;
I hope this will be helpful to you!!!

Writing to a textbox on a separate form (C#)

Suppose I have two forms: Form1 and Form2. Form2 has a text control named text1
In VB6 I could write to Form 2's textbox
control from Form1 by using: Form2.Text1.text = "Some text here"
How can I make this work in C#? I have tried everything!
What I have tried:
Form2 Frm2 = new Form2();
Frm2.show();
Frm2.Activate(); // Trying everything to make sure it sees the form (it does).
Frm2.Text1 (Doesn't find the control)...
ANSWER:
I ended up making a public routine in Form2, and then just calling this routine from form1. In this public routine of Form2 I would then call the textbox!
I believe all form elements are private by default. So you're going to have to switch their definition to public (in your .designer.cs) or make a public getter and setter for each textbox you want to access.
You need to keep a reference to the second form in the first form.
// form1 code
// variables
Form2 myForm2;
// Form1_Loaded event handler
myForm2 = new Form2();
myForm2.Show();
// place where you want to change text on form2 from within form1
myForm2.Text1.Text = "Some text here";
You have to have a reference to the instance of Form2 in order to write to it. Pass the reference to the instance of Form2 to Form1 and then manipulate it like you are expecting. You can pass this instance in via the constructor or you can pass it in later. You can also store the instance reference in a central location. Just be sure that you don't create a new instance of Form2 when you try to access the instance. For example, if you put the following code in your form:
Form2 myInstance = new Form2();
then you will be creating a new instance of Form2, which probably isn't what you want.
My goal was add text to a Text Box on another form. I had Form1 and Form2. Form2 has a text box control named Text1. In order to make this work, I created a Sub Routine:
public Void WriteToText(string sData)
{
// Here is where I wrote to my textbox
Text1.text = sData;
}
Form 1 code:
Form2 Frm2 = new Form2();
Frm2.WriteToText("My Data");

Adding text to RichTextBox in new form in

I have two forms, let's call them Main and Form2.
Main form consists of a button and Form2 consists of a RichTextBox.
What I want my program to do is when I click button in the main form it calls function in class DoSomeWork.Do(). Function Do() gets some text from file, it has to open Form2 and paste this text to that RichTextBox.
Problem is that I don't know how to "access" this RichTextBox and paste text to it.
Thanks in advance.
you can create a property on form 2
public string RichTextboxText
{
get
{
return this.RichTextBox1.Text
}
set
{
this.RichTextBox1.Text = value;
}
}
Then create a new form:
Form2 f2 = new Form2() { RichtTextBoxText = "I like big butts"; }
f2.Show();
Something like this should works
[edit]
just like to add that in this way you can also get the value back to from one.
in form one at any random point you can do:
string RichtEditTextFromForm2 = f2.RichTextBoxText;
given f2 is still active atleast
In Form2 you add a method
public void InsertText(string text)
{
richTextBox1.Text = text;
}
to use the method you open Form2 like this:
Form2 f2 = new Form2();
f2.InsertText("hello world");
f2.Show();
You can pass text values through Constructor.
Eg:
create parameterised constructor for Form2
Public Form2(string str)
{
this.Value=str;
InitializeComponent();
}
NOTE: Value is a public string in form Form2 .and you can set this value to richTextBox in form loading.
richTextBox1.Text=Value;

Fill Identity Of WinForms Into Textboxes

I have Three Forms as below:
Form1
Form2
Form3
The Form1 and Form3 are communicating with Form2. I want If Form1 visited to Form2 than
Form1 Should leave it’s identity to Form2’s textbox1. If Form3 visited to Form2 than Form3
should leave it’s Identity to Form2’s textbox2.
Is it possible?.
Here is Visted Means as below.
By Form1
Form2 f2= new Form2();
f2.Show();
By Form3
Form2 f2= new Form2();
f2.Show();
And Identity Like "Form1" and "Form3"
I believe you are asking for the form that opens Form2 to set some specific value to the textbox. You could have a property on Form2 that sets the textbox text.
public partial class Form2 : Form
{
/*.......*/
public string Identity
{
set
{
textbox1.Text = value;
}
}
}
Then in Form1 or Form3:
Form2 form2 = new Form2();
form2.Identity = this.Name;
form2.Show();
The best thing to do is overload the Show method for each of your forms to accept something like a "previous form" parameter that will serve as a reference to the caller (you could do this either as a Form object, whose properties you could then access, or simply as a String). Then, wherever you show a second form from an existing form, you pass information about the existing form into the Show method for the second form. The second form's Show method would take care of updating its textbox with the name of the form that displayed it.
For example, each form would contain an overloaded Show method:
public void Show(Form previousForm)
{
//Set the textbox on this form to contain the name of the calling form
myTextBox.Text = previousForm.Name;
//Call the base class's method to show this form
base.Show();
}
And you would show one form from another by calling its overloaded Show method:
private void ShowOtherFormButton(object sender, EventArgs e)
{
//Create a new instance of the form you want to display
Form2 myOtherForm = new Form2();
//Show the other form, passing the calling form as a parameter
myOtherForm.Show(this);
}
This way, no form is responsible for or even allowed to update controls contained by another form. You don't have to expose any additional properties or remember to call any additional methods on the forms you want to show—it's built right into the Show method that you have to call anyway.
Alternatively, you could overload each form's constructor in the same way, but that won't work if you want to be able to move between existing instances of each form. You'll only be able to specify information about the previous form when you're creating a new instance of each form.
You will need a reference to the caller. You can do this by overriding the constructor of the Form.
public partial class Form2 : Form
{
Form _caller;//Caller form
//Default constructor
public Form1()
{
InitializeComponent();
}
public Form1(Form caller)
{
InitializeComponent();
this._caller = caller;
textbox2.Text = caller.Name;
}
}
All you have to do now is call Form2 using the new constructor:
Form2 form2Instance = new Form2(this);
form2Instance.Show();
Whichever form will call Form2, will now have it's name written on textbox2 of Form2
You need to pass data between the window forms(ie.,the name of the form in this scenario)so that you can get it in Form2.
Refer the lint to pass data between window forms: http://www.vbdotnetheaven.com/UploadFile/thiagu304/passdata12262006073406AM/passdata.aspx

Categories

Resources