1

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

user974015
  • 113
  • 2
  • 6
  • 14
  • 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 Nov 20 '12 at 01:14
  • 2
    Please sow us what you're doing so far. – Kiril Nov 20 '12 at 01:16
  • 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 Nov 20 '12 at 01:23
  • @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 Nov 20 '12 at 01:34
  • guys..i can pass the value properly... its just the UI didnt change at all... – user974015 Nov 20 '12 at 01:36
  • 3
    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 Nov 20 '12 at 01:37
  • As I suspected in my previous comment. You're instantiating a new `Form1` instead of using the existing one. You should read the answers below more thoroughly before thinking they aren't doing what you want. – Simon Whitehead Nov 20 '12 at 02:12
  • @user974015 if there is already a form1 open when you run the code that you posted, you are creating a seperate instance of form1 and not showing it. So you are changing it but in a different form1. Try adding a frm1.show to your change_Click Method to see what I mean – Mark Hall Nov 20 '12 at 02:14

6 Answers6

2

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;
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
1

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

Felix Guo
  • 2,700
  • 14
  • 20
  • These links might help: http://searchwindevelopment.techtarget.com/answer/In-C-how-can-I-change-the-properties-of-controls-on-another-form and http://www.dreamincode.net/forums/topic/181697-how-to-access-controls-of-another-form/ – Felix Guo Nov 20 '12 at 01:19
  • guys..i can pass the value properly... its just the UI didnt change at all...and its really a bad idea to make the controls public.. – user974015 Nov 20 '12 at 01:35
1

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

horgh
  • 17,918
  • 22
  • 68
  • 123
0

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; }
        }
    }
}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • @user974015 then you will need to show us your code, the code that I posted does work, it will transfer the text from form2 and update form1's textbox. If you are not able to update your textbox then we need to see the method you are using in order to help you. – Mark Hall Nov 20 '12 at 01:42
  • can u explain this in form2 ..the textbox1.text.. RaiseCustomEvent(this, new CustomEventArgs(textBox1.Text)); – user974015 Nov 20 '12 at 01:54
  • @user974015 What I have done is create a Custom Event args and associated it with a custom event. The line of code you are asking about raises the event and assigns the value of the Textbox's Text property to the custom eventarg then since form1 has subscribed to the event when it created frm2 it will then handle the event getting your Text out of the CustomEventArgs – Mark Hall Nov 20 '12 at 02:06
0

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;
Community
  • 1
  • 1
0

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.

Toan Nguyen
  • 11,263
  • 5
  • 43
  • 59