Sending variables from Form2 to Form1 [duplicate] - c#

This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 1 year ago.
I wanna know how I can send variables from Form2 to Form1. I have one textbox and button in Form1 and one textbox and button in Form2. My application starts at Form1, textbox1 is empty and by clicking button Form2 will appear. In Form2 I want to write number and by clicking on the button send it to Form1 textbox.
I was trying this code, but I dont know how to solve it.
Form1 code:
public static int number;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
Form2 code
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1.number = textBox1.Text;
this.Visible = false;
}
Now I have variable called number in Form1, which contains value of Form2 Textbox, right? But how do I say: textbox1.text(Form1) = number after that action? Do I need refresh Form1 somehow?
Thanks!

I'd say a nice easy way to do this kind of thing, is via making a public event:
In form two, add an event:
public partial Class Form2
{
public event Action<string> SomethingHappened;
...
We need to fire the event on Form2 - to notify subscribers:
//On Form2
private void button1_Click(object sender, EventArgs e)
{
if(SomethingHappened != null)
SomethingHappened (textBox1.Text);
}
Then, upon creation 'subscribe' the parent form Form1 to action on the sub-form:
Form2 form = new Form2();
//Here, we assign an event handler
form.SomethingHappened += (string valueFromForm2) =>
{
//Event handled on Form1
this.Number = valueFromForm2;
};

The setup sounds kinda like a settings dialog where you can't continue in Form1 until Form2 is closed.
If this is the case, then something more like his would be appropriate in Form1:
public partial class Form1 : Form
{
private int number = 411;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Enabled = false;
this.textBox1.Text = number.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this.number);
if (f2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.number = f2.Number;
this.textBox1.Text = this.number.ToString();
}
}
}
With Form2 looking something like:
public partial class Form2 : Form
{
public Form2(int number)
{
InitializeComponent();
this.textBox1.Text = number.ToString();
}
private int number = 0;
public int Number
{
get { return this.number; }
}
private void btnOK_Click(object sender, EventArgs e)
{
int value;
if (int.TryParse(this.textBox1.Text, out value))
{
this.number = value;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
else
{
MessageBox.Show(textBox1.Text, "Invalid Integer");
}
}
}

Related

Passing Values from one Form to another Form in a Button click

These are my 2 Forms.
These are the codes for Form 1-->
namespace Passing_Values
{
public partial class Form1 : Form
{
string a="preset value";
public Form1()
{
InitializeComponent();
}
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2().Show();
}
public void set(string p)
{
MessageBox.Show("This is Entered text in Form 2 " + p);
a = p;
MessageBox.Show("a=p done! and P is: " + p + "---and a is: " + a);
textBox1.Text = "Test 1";
textBox2.Text = a;
textBox3.Text = p;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(a);
}
}
}
These are the codes for Form 2-->
namespace Passing_Values
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
Form1 j = new Form1();
j.set(g);
}
}
}
See the picture.You can understand the design.
This is what I want to do. 1st I open Form2 using button in Form1. Then I enter a text and click the button("Display in Form1 Textbox"). When it's clicked that value should be seen in the 3 Textboxes in Form1.I used Message Boxes to see if the values are passing or not. Values get passed from Form2 to Form1. But those values are not displays in those 3 Textboxes but the passed values are displayed in Message Boxes. Reason for the 3 Text Boxes can be understood by looking at the code. So what's the error?
Actually I have an object to pass. So I did this
in form1-->
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}
in form2-->
public partial class Form2 : Form
{
Form1 a;
public Form2(Form1 b)
{
a = b;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
a.set(g);
this.Close();
}
}
I would simply pass it in the constructor.
So, the code for form2, will be:
public partial class Form2 : Form
{
string _input;
public Form2()
{
InitializeComponent();
}
public Form2(string input)
{
_input = input;
InitializeComponent();
this.label1.Text = _input;
}
}
And the call in Form1 will be:
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2(this.textBox1.Text.ToString());
fm2.Show();
}
public partial class Form1 : Form
{
Form2 fm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2();
fm2.Show();
fm2.button1.Click += new EventHandler(fm2button1_Click);
}
private void fm2button1_Click(object sender, EventArgs e)
{
textBox1.Text = fm2.textBox1.Text;
}
}
And code in form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
set modifier property of textbox1 and button1 to public
Place a static string in your Form2
public static string s = string.Empty;
and, in your Display in Form1 Textbox button click event, get the value from the textbox in your string s:
s = textBox1.Text;
Form1 f1 = new Form1();
f1.Show();
once, the Form1 is showed up again, then in the Form1_Load event, just pass your Form2's text value to your Form1's textboxes, the value of which was gotten by the variable s:
foreach (Control text in Controls)
{
if (text is TextBox)
{
((TextBox)text).Text = Form2.s;
}
}

Sending a value from opened form 2 to form 1

Sorry, I know this question, or similar has been asked frequently, but as I've gone through different threads, I just don't know how to apply it to my program.
Here's my situation:
In Form 1, I have a label. There's a button that opens Form 2, which has radiobuttons and a button. The button in Form 2 should send a string value from the radio button, to the label.Text in form 1. How can I go around in doing so?
So, below is what opened form 2.
private void selectkeyButton_Click(object sender, EventArgs e)
{
selectKeyboard sk = new selectKeyboard();
sk.ShowDialog();
}
And in Form 2, here's what i have so far:
public Form1 otherForm = new Form1();
string hotkey = "";
public void hotkeyChanged(object sender, EventArgs e)
{
RadioButton rr = (RadioButton)sender;
switch (rr.Name)
{
case ("buttonF1"):
hotkey = "F1 ";
break;
}
}
public void buttonConfirmKey_Click(object sender, EventArgs e)
{
hotkey = otherForm.keyLabel.Text;
this.Close();
}
Where I have public Form1 otherForm = new Form1();
and hotkey = otherForm.keyLabel.Text; I found it here.
And it doesn't seem to be working, as when I press the button on form2, the form closes but the label in form1 doesn't change...
any ideas?
thanks
There are different approaches to do this. You could go like this :
Solution one:
(Don't forget to set the modifier for you label1 in this case to Public. You can set this in the designer options > under Properties > design)
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
}
Form 2:
public partial class Form2 : Form
{
private readonly Form1 _parent;
public Form2(Form1 parent)
{
InitializeComponent();
_parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
_parent.label1.Text = textBox1.Text;
Close();
}
}
Solution 2
Instead of setting label1 to public, leave it on private (as default) but set the DialogResult property of button1 on form 2 to "DialogResult OK" (under Properties > Behavior)
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
DialogResult res = frm.ShowDialog();
if (res == DialogResult.OK)
{
label1.Text = frm.MyNewText;
}
}
}
Form 2:
public partial class Form2 : Form
{
public string MyNewText;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyNewText = textBox1.Text;
Close();
}
}
in the constructor of the forms you can get the values like this :
in form2 you should add a constructor like this :
public partial class Form2: Form
{
public string _newvalue
public Form2(string value)
{
InitializeComponent();
_newvalue=value
}
//you should assign the value to the label .
}
in form1 you should do this :
form2 new=form2("sampletext");
new.showdialog();
Solution 1:
In Form1:
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.ShowDialog();
}
Codes in Form2:
Form frm_;
public Form2(Form frm)
{
InitializeComponent();
frm_ = frm;
}
private void btnInForm2_Click(object sender, EventArgs e)
{
Label lbl = (Label)frm_.Controls.Find("lblInForm1", true)[0];
string PassVal="What you want";
lbl.Text = PassVal;
}
Solution 2:
in Form 1:
Form2 f = new Form2();
if (f.ShowDialog() == DialogResult.OK)
{
lblInForm1.Text = f.PassVal;
}
in Form 2:
internal string PassVal = "";
PassVal is a Field.

Send BackColor value to another form [duplicate]

I want to pass values between two Forms (c#). How can I do it?
I have two forms: Form1 and Form2.
Form1 contains one button. When I click on that button, Form2 should open and Form1 should be in inactive mode (i.e not selectable).
Form2 contains one text box and one submit button. When I type any message in Form2's text box and click the submit button, the Form2 should close and Form1 should highlight with the submitted value.
How can i do it? Can somebody help me to do this with a simple example.
There are several solutions to this but this is the pattern I tend to use.
// Form 1
// inside the button click event
using(Form2 form2 = new Form2())
{
if(form2.ShowDialog() == DialogResult.OK)
{
someControlOnForm1.Text = form2.TheValue;
}
}
And...
// Inside Form2
// Create a public property to serve the value
public string TheValue
{
get { return someTextBoxOnForm2.Text; }
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(textBox1.Text);
frm2.Show();
}
public Form2(string qs)
{
InitializeComponent();
textBox1.Text = qs;
}
Define a property
public static class ControlID {
public static string TextData { get; set; }
}
In the Form1
private void button1_Click(object sender, EventArgs e)
{
ControlID.TextData = txtTextData.Text;
}
Getting the data in Form1 and Form2
private void button1_Click(object sender, EventArgs e)
{
string text= ControlID.TextData;
}
After a series of struggle for passing the data from one form to another i finally found a stable answer. It works like charm.
All you need to do is declare a variable as public static datatype 'variableName' in one form and assign the value to this variable which you want to pass to another form and call this variable in another form using directly the form name (Don't create object of this form as static variables can be accessed directly) and access this variable value.
Example of such is,
Form1
public static int quantity;
quantity=TextBox1.text; \\Value which you want to pass
Form2
TextBox2.Text=Form1.quantity;\\ Data will be placed in TextBox2
Declare a public string in form1
public string getdata;
In button of form1
form2 frm= new form2();
this.hide();
form2.show();
To send data to form1 you can try any event and code following in that event
form1 frm= new form1();
form1.getdata="some string to be sent to form1";
Now after closing of form2 and opening of form1, you can use returned data in getdata string.
I've worked on various winform projects and as the applications gets more complex (more dialogs and interactions between them) then i've started to use some eventing system to help me out, because management of opening and closing windows manually will be hard to maintain and develope further.
I've used CAB for my applications, it has an eventing system but it might be an overkill in your case :) You could write your own events for simpler applications
Form1 Code :
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
MessageBox.Show("Form1 Message :"+Form2.t.Text); //can put label also in form 1 to show the value got from form2
}
Form2 Code :
public Form2()
{
InitializeComponent();
t = textBox1; //Initialize with static textbox
}
public static TextBox t=new TextBox(); //make static to get the same value as inserted
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
It Works!
declare string in form1
public string TextBoxString;
in form1 click event add
private void button1_Click(object sender, EventArgs e)
{
Form1 newform = new Form1();
newform = this;
this.Hide();
MySecform = new Form2(ref newform);
MySecform.Show();
}
in form2 constructer
public Form2(ref Form1 form1handel)
{
firstformRef = form1handel;
InitializeComponent();
}
in form2 crate variable Form1 firstformRef;
private void Submitt_Click(object sender, EventArgs e)
{
firstformRef.TextBoxString = textBox1.Text;
this.Close();
firstformRef.Show();
}
In this code, you pass a text to Form2. Form2 shows that text in textBox1.
User types new text into textBox1 and presses the submit button.
Form1 grabs that text and shows it in a textbox on Form1.
public class Form2 : Form
{
private string oldText;
public Form2(string newText):this()
{
oldText = newText;
btnSubmit.DialogResult = DialogResult.OK;
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = oldText;
}
public string getText()
{
return textBox1.Text;
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
}
And this is Form1 code:
public class Form1:Form
{
using (Form2 dialogForm = new Form2("old text to show in Form2"))
{
DialogResult dr = dialogForm.ShowDialog(this);
if (dr == DialogResult.OK)
{
tbSubmittedText = dialogForm.getText();
}
dialogForm.Close();
}
}
Ok so Form1 has a textbox, first of all you have to set this Form1 textbox to public in textbox property.
Code Form1:
Public button1_click()
{
Form2 secondForm = new Form2(this);
secondForm.Show();
}
Pass Form1 as this in the constructor.
Code Form2:
Private Form1 _firstForm;
Public Form2(Form1 firstForm)
{
_firstForm = firstForm:
}
Public button_click()
{
_firstForm.textBox.text=label1.text;
This.Close();
}
you can pass as parameter the textbox of the Form1, like this:
On Form 1 buttom handler:
private void button2_Click(object sender, EventArgs e)
{
Form2 newWindow = new Form2(textBoxForReturnValue);
newWindow.Show();
}
On the Form 2
public static TextBox textBox2; // class atribute
public Form2(TextBox textBoxForReturnValue)
{
textBox2= textBoxForReturnValue;
}
private void btnClose_Click(object sender, EventArgs e)
{
textBox2.Text = dataGridView1.CurrentCell.Value.ToString().Trim();
this.Close();
}
Constructors are the best ways to pass data between forms or Gui Objects you can do this.
In the form1 click button you should have:
Form1.Enable = false;
Form2 f = new Form2();
f.ShowDialog();
In form 2, when the user clicks the button it should have a code like this or similar:
this.Close();
Form1 form = new Form1(textBox1.Text)
form.Show();
Once inside the form load of form 1 you can add code to do anything as you get the values from constructor.
How to pass the values from form to another form
1.) Goto Form2 then Double click it. At the code type this.
public Form2(string v)
{
InitializeComponent();
textBox1.Text = v;
}
2.) Goto Form1 then Double click it. At the code type this.
//At your command button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2(textBox1.Text);
F2.Show();
}
This is very simple.
suppose you have 2 window form Form1 and Form2 and you want to send record of textbox1 from Form1 to Form2 and display this record in label1 of Form2;
then in Form2 create a label which name is label1 and go to the property of label1 and set 'Modifiers'=public and in Form one create a textBox with id textBox1 and a button of name submit then write the following code on button click event
button1_Click(object sender, EventArgs e)
{
Form2 obj=new Form2();
obj.label1.text=textBox1.text.ToString();
obj.show();
}
thats it...
for this way you can bind dataset record to another form's datagridview......
You can make use of a different approach if you like.
Using System.Action (Here you simply pass the main forms function as the parameter to the child form like a callback function)
OpenForms Method ( You directly call one of your open forms)
Using System.Action
You can think of it as a callback function passed to the child form.
// -------- IN THE MAIN FORM --------
// CALLING THE CHILD FORM IN YOUR CODE LOOKS LIKE THIS
Options frmOptions = new Options(UpdateSettings);
frmOptions.Show();
// YOUR FUNCTION IN THE MAIN FORM TO BE EXECUTED
public void UpdateSettings(string data)
{
// DO YOUR STUFF HERE
}
// -------- IN THE CHILD FORM --------
Action<string> UpdateSettings = null;
// IN THE CHILD FORMS CONSTRUCTOR
public Options(Action<string> UpdateSettings)
{
InitializeComponent();
this.UpdateSettings = UpdateSettings;
}
private void btnUpdate_Click(object sender, EventArgs e)
{
// CALLING THE CALLBACK FUNCTION
if (UpdateSettings != null)
UpdateSettings("some data");
}
OpenForms Method
This method is easy (2 lines). But only works with forms that are open.
All you need to do is add these two lines where ever you want to pass some data.
Main frmMain = (Main)Application.OpenForms["Main"];
frmMain.UpdateSettings("Some data");
I provided my answer to a similar question here
You can use this;
Form1 button1 click
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
this.Hide();
frm2.Show();
}
And add this to Form2
public string info = "";
Form2 button1 click
private void button1_Click(object sender, EventArgs e)
{
info = textBox1.Text;
this.Hide();
BeginInvoke(new MethodInvoker(() =>
{
Gogo();
}));
}
public void Gogo()
{
Form1 frm = new Form1();
frm.Show();
frm.Text = info;
}
if you change Modifiers Property of a control in a Form to Public, another Forms can access to that control.
f.e. :
Form2 frm;
private void Form1_Load(object sender, EventArgs e)
{
frm = new Form2();
frm.Show();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(frm.txtUserName.Text);
//txtUserName is a TextBox with Modifiers=Public
}
// In form 1
public static string Username = Me;
// In form 2's load block
string _UserName = Form1.Username;
the tag Properties receive object value
( C# send value to another form )
private void btn_Send_Click(object sender, EventArgs e)
{
Form frm = new formToSend();
frm.tag = obj;
frm.ShowDialog();
}
Receive value that sent from previous form ( frm )
Ex: sent data is string ( we need to type casting first, because tag value is an object )
public Receive_Form()
{
InitializeComponent();
MessageBox.Show((string)this.Tag);
}
How about using a public Event
I would do it like this.
public class Form2
{
public event Action<string> SomethingCompleted;
private void Submit_Click(object sender, EventArgs e)
{
SomethingCompleted?.Invoke(txtData.Text);
this.Close();
}
}
and call it from Form1 like this.
private void btnOpenForm2_Click(object sender, EventArgs e)
{
using (var frm = new Form2())
{
frm.SomethingCompleted += text => {
this.txtData.Text = text;
};
frm.ShowDialog();
}
}
Then, Form1 could get a text from Form2 when Form2 is closed
Thank you.

How to get string data in main form from second from, when button on second form is clicked in C# .net?

At first I thought that it won't be a problem for me, but now I can't figure it out. So,
when I click Button1 in main form, form2 opens. Form2 is simple numeric keyboard, that user can enter some data. On form2 is also Save. When user clicks it, entered value should pass to main form and from that moment some event must happen in main form, which contains data from form2. Could you please give me some example or any kind of help? Thanks!
// code from main form to create form2
private void button1_Click(object sender, EventArgs e)
{
// Create a new instance of the Form2 class
Form2 settingsForm = new Form2();
// Show the settings form
settingsForm.Show();
string val = settingsForm.ReturnValue1;
MessageBox.Show(val);
}
//button save on form2
private void button13_Click(object sender, EventArgs e)
{
this.ReturnValue1 = "Something";
this.ReturnValue2 = DateTime.Now.ToString(); //example
this.Close();
//after this, some event should happen in main form !
}
There is a lot of solutions to do what you want; but I think one of these will resolve your problem.
1- Simple and easy: use public properties in Form2, initialize them when buttonSave get clicked, and access them in Form1:
Form2:
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
YourDate = "something";
Close();
}
public object YourDate { get; private set; }
}
Form1:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
var f2 = new Form2();
f2.ShowDialog();
var data = f2.YourDate;
}
}
2- A better way, is using events which is more flexible and professional programming friendly:
Form2:
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
}
// create an event of Action<T> which T is your data-type. e.g. in this example I use an object.
public event Action<object> SaveClicked;
// create an event invocator, to invoke event whenever you want
protected virtual void OnSaveClicked(object data){
var handler = SaveClicked;
if (handler != null)
handler(data);
}
private void button1_Click(object sender, EventArgs e){
// prepare your data here, -object, or string, or int, or whatever it is
var data = PrepareYourDataHere;
// invoke the event
OnSaveClicked(data);
Close();
}
}
Form1:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
// create an instance of Form2
var f2 = new Form2();
// add an event listener to SaveClicked event -which we have declared it in Form2
f2.SaveClicked += f2_SaveClicked;
f2.Show();
// or: f2.ShowDialog();
}
void f2_SaveClicked(object obj) {
// get data and use it here...
// any data which you pass in Form2.OnSaveClicked method, will be accessible here
}
}
UPDATE:
If you want to fire some events in form1, just after form2 closed, you can simply add a listener to Form2.FormClosed event:
// code from main form to create form2
private void button1_Click(object sender, EventArgs e) {
// Create a new instance of the Form2 class
Form2 settingsForm = new Form2();
settingsForm.FormClosed += SettingFormClosed;
// Show the settings form
settingsForm.Show();
string val = settingsForm.ReturnValue1;
MessageBox.Show(val);
}
void SettingFormClosed(object sender, FormClosedEventArgs e) {
// this method will be called automatically when form2 closed
}
here a sample how you can achieve this
//here I suppose that form1 is the mainform
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void UpdateMainForm(string updatedString)
{
//here you can update and invoke methods
//Once called you could raise events in your mainform
}
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
form2.ShowDialog();
}
}
}
Form2
public partial class Form2 : Form
{
private Form1 _mainForm1;
public Form2(Form1 mainForm1)
{
InitializeComponent();
_mainForm1 = mainForm1;
}
private void button1_Click(object sender, EventArgs e)
{
_mainForm1.UpdateMainForm( DateTime.Now.ToString());
}
}

Get data from one textbox on form1 from another textbox on form2

I have two forms form1 and form2. I want to get the text from the textbox of form2 when a button is clicked on form1. I am using on form1:
private void but_Click(object sender, EventArgs e)
{
Form2 f2=new Form2();
txtonform1=f2.fo;
}
and on form2 I have this method to return the text from the textbox:
public string fo
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
Now the problem is that it returns null. Whats the problem I am new to c# can anybody help me please!
You have to work with one single form, otherwise you create new instance every time:
Form2 f2 = new Form2();
private void but1_Click(object sender, EventArgs e)
{
f2.fo=txtonform1.Text;
}
private void but2_Click(object sender, EventArgs e)
{
MessageBox.Show(f2.fo);
}
You are creating a new form instance here:
Form2 f2=new Form2();
And your fo property return this new form's textBox1, so you textBox1 doesn't contain any text and you are getting null.
I guess you are displaying form2 from Form1, if it's correct just define a one Form2 intance in class level:
public partial class Form1 : Form
{
Form2 f2 = new Form2();
}
And when you want to show it use this:
f2.Show(this);
When you want to change your TextBox value now you can use:
txtonform1.Text = f2.fo;
But to do this make sure you change your textBox1.Text in form2.
You should keep the reference of form2 which is/was already displayed, in form1 and then use the same variable to access the value.
I don't know how form2 was created and shown but assuming it's created and shown by some button click on form1 then form1 class will look something like,
private Form f2 = null;
private void buttonShowForm2_Click(object sender, EventArgs e)
{
if(f2 == null)
f2 = new form2();
f2.Show();
}
private void but_Click(object sender, EventArgs e)
{
if(f2 == null) //If this form was not already displayed display it to get the input from user
buttonShowForm2_Click(null, null);
else
txtonform1=f2.fo;
}
FIRST SOLUTION:
1.) Goto Form2 then Double click it. At the code type this.
public Form2(string sTEXT)
{
InitializeComponent();
textBox1.Text = sTEXT;
}
2.) Goto Form1 then Double click it. At the code type this.
//At your command button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 sForm = new Form2(textBox1.Text);
sForm.Show();
}
SECOND SOLUTION:
1.) Goto Form1 then Double Click it. At the code type this.
public string CaptionText
{
get {return textBox1.Text;}
set { textBox1.Text = value;}
}
note: the value of your textbox1.text = sayre;
2.) Goto Form2 then Double click it. At the code type this.
// At your command button In Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 sForm1 = new Form1();
textBox1.Text = sForm1.CaptionText;
}

Categories