How to Update Textbox in Form1 from Form2

How to update textbox in form1 from form2?

To get the BASICs working, do the following.. Create a new project. Nothing of your current code, windows, etc... The default project will create a form "Form1" leave it alone for now.

Add a NEW form to the project, it will default to "Form2"... Put a single textbox on it and a single button on it. For grins, and clarification to differentiate between the object names, change the name of the controls on Form2 to "txtOnForm2", and "btnOnForm2" (case sensitive for my sample, and readability vs "txtonform2" all lower case). Now, on the form, right-click and click "View Code". It will bring you to the other "Partial class" declaration where your constructors are found. Add the following, dont worry about compile errors as the other half will be when we put code into Form1 next...

// specifically typecasting the TYPE of form being passed in, 
// not just a generic form. We need to see the exposed elements
Form1 CalledFrom;
// Ensure to do the : this() to make sure default behavior
// to initialize the controls on the form are created too.
public Form2(Form1 viaParameter) : this()
{
CalledFrom = viaParameter;
}

private void btnOnForm2_Click(object sender, EventArgs e)
{
CalledFrom.ValuesByProperty = this.txtOnForm2.Text;
MessageBox.Show( "Check form 1 textbox" );

string GettingBack = CalledFrom.ValuesByProperty;
MessageBox.Show( GettingBack );

CalledFrom.SetViaMethod( "forced value, not from textbox" );
MessageBox.Show( "Check form 1 textbox" );

GettingBack = CalledFrom.GetViaMethod();
MessageBox.Show( GettingBack );
}

Save and close the Form2 designer and code window.

Now, open Form1. Put a single textbox and a single button on it. The default names for the controls will be "textbox1" and "button1" respectively. Leave it as is. Double click on the button (on form1). It will bring up a snippet for code for that button. Paste the following

private void button1_Click(object sender, EventArgs e)
{
Form2 oFrm = new Form2(this);
oFrm.Show();
}

public string ValuesByProperty
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}

public void SetViaMethod(string newValue)
{ this.textBox1.Text = newValue; }

public string GetViaMethod()
{ return this.textBox1.Text; }

Now, save the forms and run them. Click button on first form, calls the second with the already created instance of itself, not a new SECOND instance of itself. The second form will be displayed. Shift the windows so you can see both.

Enter some text in the textbox second window and click the button... follow the back/forth of what is coming/going.

Accessing a textbox value from form1 in form2

What I did was create a new project and add a second form then added a textbox to both forms, with a button on Form1 to push the value of its text box to Form2.

To achieve this, create a Property on Form2 and set it from Form1. Like this:

Form1

public partial class Form1 : Form
{
Form2 frm2;
public Form1()
{
InitializeComponent();
frm2 = new Form2();
frm2.Show(this);
}

private void button1_Click(object sender, EventArgs e)
{
frm2.ModifyTextBoxValue = textBox1.Text;
}
}

Form2

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

public string ModifyTextBoxValue
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
}

Done this way, the same property can also be used to pull data from Form2 if desired.

Update Textbox from another form

By using delegate I had solve this problem:

Form1 add:

public delegate void UpdateSMOStatus(string line, string group, string stationType);
public UpdateSMOStatus updateSMOStatus;

showConnectionStatus function changed to:

public void showConnectionStatus()
{
updateSMOStatus(configElement[0], configElement[1], configElement[2] + "," + configElement[3]);
}

Form2 function would look like:

private void btnGetStation_Click(object sender, EventArgs e)
{
Program.form.updateSMOStatus = new Terminal.UpdateSMOStatus(updateSMOStatus);
Program.form.showConnectionStatus();

}

public void updateSMOStatus(string line, string group, string stationType)
{
txtLineName.Text = line;
txtGroupName.Text = group;
txtStationType.Text = stationType;
}

Changing TextBox Text of Form1 From Form2

Pass the original instance of Form1 to the constructor of Form2, like this:

Public Class Form2 Inherits Form
Dim theForm1 As Form1
Public Sub New(form1 As Form1)
theForm1 = form1
End Sub

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
' Call instance of Form1 passed in to change the values here
theForm1.ChangeValues()
End Sub
End Class

Now in Form1, when you create the Form2 instance you need to pass the instance of Form1, like this:

Dim frmForm2 As New Form2(Me) 
frmForm2.ShowDialog()

Note: Me is a reference to the current class, Form1 in this case.

How to update contents of rich text box in form1 with values coming from form2 without closing form2?

Please changes the field names as you required. Also following code will update the rich text box value concurrently when textfield value in form2 is changed. You may want to do minor changes to trigger it on button change event.

Add the following method to your From1

private void SetChildFromValueToParent(object obj, EventArgs args)
{
//Read the child form's control value and set it to parent form field
txtBox.Text = ((TextBox)(obj)).Value.ToString();
}

Add the following logic to your Form1 button click which opens the Form2

private void button1_Click(object sender, EventArgs e)
{
ChildForm childForm = new ChildForm();

//Find the textbox control in the child form
Control[] controls = childForm.Controls.Find("textBox", true);

if (null != controls[0])
{
//Bind the method in the parent form to child form text control's TextChanged event
controls[0].TextChanged += new System.EventHandler(SetChildFromValueToParent);
}

childForm.ShowDialog();
}

EDIT - Getting value on Button Click

   private void SetChildFromValueToParent(object obj, EventArgs args)
{
//Read the child form's control value and set it to parent form field
Form2 from2 = new Form2();
string richTextBox.Text = ((TextBox)form2.Controls["textBox1"]).Text;
}

Pass data from form2 method to form1 textbox

U can simply ref Form1 in the Constructor in Form2

public Form2(Form1 frmMain){
InitializeComponents();
_FrmMain = frmMain;
}

simply when initializing Form2:

Form2 frm2 = new Form2(this);
frm2.Show();

and now in Form2 you should be able to just change everything by properties:

private void BtnSend_Clicked(object sender, EventArgs e)
{
_FrmMain.Foo = Bar;
}


Related Topics



Leave a reply



Submit