1
votes

C# textbox sending text value. Situation, i got 2 forms. Form1 and Form2. Form1 got a textbox and form2 got a textbox and a button, i will put a text value on form2 textbox and when i click the form2 button the value of form2 textbox will be sent and change the form1 textbox value....Need help..

This is what ive done..im just gonna summarize it

Form1 got no codes just textbox1

This is the code in form2 button

  private void change_Click(object sender, EventArgs e)
        {
         form1 frm1 = new form();
         string test = textbox2.text
         frm1.textbox.text = test;



}

ive try some poping message box to check if the value pass...and so far the value was really pass but no changes in the UI

7
How are you opening each form? If you open one from the other with ShowDialog you can reference the owner form and cast it to the appropriate type (of Form 1) and access the text box if its modifiers are Public. - pinkfloydx33
Please sow us what you're doing so far. - Kiril
yeah. another question is whats the difference of Show() and showDialog()? when i tried to change form1 textbox and close form2 the only thing that was pass was the values but i cant see any changes in form1 textbox (physical). - user974015
@user974015 ShowDialog opens the form in an application Modal State meaning you cannot do anything on the parent form until the child is finished. Show just opens another form and you are able to work with both of them. - Mark Hall
You may have noticed that all of the answers below are complete guesses. This is because you haven't shown us what you've done so far so we cannot understand the context in which your problem occurs. If you are "passing the value properly but the UI didn't change at all", chances are you're instantiating a new form instead of using an existing one. In which case, you aren't "passing the value properly". - Simon Whitehead

7 Answers

2
votes

Assuming you create Form2 as a child of Form1 (from within Form1, do something like Form2 from = new Form2();, you can access any public property of the child form from within the parent. So, just make sure to set the accessibility of the TextBox to public, and do something like this:

var form = new Form2();
form.ShowDialog();
this.TextBox1.Text = form.TextBox1.Text;
1
votes

You can declare the textbox in Form1 to be public, then you can access it from form2 by going form1.textBoxName.propertyName

1
votes

You could use events for this:

Define an interface:

public interface ITextChange
{
    event EventHandler SomeTextChanged;
}

Then let you form with button implement this interface and fire the event on button click passing the value from the textbox as the first parameter:

public partial class Form1 : Form, ITextChange
{
    public event EventHandler SomeTextChanged = delegate { };

    public Form1 () {}

    private void button1_Click(object sender, EventArgs e)
    {
        SomeTextChanged(textBox1.Text, null);
    }
}

Pass an instance of this form to your second form like this:

public partial class Form2 : Form
{
    public Form2(ITextChange f)
    {
        InitializeComponent();
        f.SomeTextChanged += new EventHandler(f_SomeTextChanged);
    }

    void f_SomeTextChanged(object sender, EventArgs e)
    {
        textBox1.Text = sender.ToString();
    }
}

So, when you create your Form2, you need to pass an instanse of Form1:

Form2 f = new Form2(form1);

As soon as you press the button, the textbox on the second form will automatically get the value.

P.S.: for more info, please, see Events Tutorial

0
votes

You could rely on knowledge of Form1 in Form2 by making the TextBox public. But in my opinion the proper way to do it would to create a custom event handler, subscribe to it in Form2 and pass the text as a eventarg. Code adapted from this MSDN Article

Form1

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Form2 frm2 = new Form2();
            frm2.RaiseCustomEvent+=new CustomEventHandler(frm2_RaiseCustomEvent);
            frm2.Show(this);
        }

        void frm2_RaiseCustomEvent(object sender, CustomEventArgs a)
        {
            textBox1.Text = a.Message;
        }
    }
}

Form2

namespace WindowsFormsApplication1
{
    public delegate void CustomEventHandler(object sender, CustomEventArgs a);

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

        private void button1_Click(object sender, EventArgs e)
        {
            RaiseCustomEvent(this, new CustomEventArgs(textBox1.Text));
        }
    }

    public class CustomEventArgs : EventArgs
    {
        public CustomEventArgs(string s)
        {
            msg = s;
        }
        private string msg;
        public string Message
        {
            get { return msg; }
        }
    }
}
0
votes

you could use the .Tag property (look at my question here the simple way to do it is like this: you said that the textBox.text in form2 would replace the texBox.text in form1 right? do this in the form2

try
{
    private void change_Click(object sender, EventArgs e)
    {
         form2 frm2 = new form();
         frm2.Tag = this.textbox2.text;
         frm2.ShowDialog();
    }
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

then write this when you load your form1

string myText = (string)this.Tag;
   this.textbox1.text = myText;
0
votes

The reason your form1.textbox1 was not updated because you initialized a new instance of form1

form1 frm1 = new form();

So to update the form1 you have on screen, you need to get its instance injected into the form2. For instance, when you show form2, you set.

form2.Form1 = currentForm1Instance;

Hope this helps.

-1
votes

I also had the same doubt, So I searched on internet and found a good way to pass variable values in between forms in C#, It is simple that I expected. It is nothing, but to assign a variable in the first Form and you can access that variable from any form. I have created a video tutorial on 'How to pass values to a form'

Go to the below link to see the Video Tutorial.

Passing Textbox Text to another form in Visual C#