Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

WinForms: How to access textbox in another form.

Options
  • 15-01-2010 4:09pm
    #1
    Registered Users Posts: 2,234 ✭✭✭


    Right..

    'nuff time wasted trying to figure thsi one out.:mad:

    I have:

    A form, form1 containing txtName (is a text box)
    A form, form2 containing strName (is a String)
    --

    form1 is the main app form so has already been instantiated/loaded.
    form2 is a secondary window to pull up a contact list. I want to select a contact name then click an "ok" button. Upon clicking "ok" I want to set the textBox txtName in form1 to the selected contact(String) from form2.

    I've been googling this and come across suggestions about having to create new instances etc. but I need to access to current instance of form1.

    Any ideas..?


Comments

  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    I'd reckon you could either use a public property of some sort on the first Form, or use delegates as a message passing method. Some links that should get you there;
    http://www.dreamincode.net/forums/showtopic47527.htm
    http://www.codeproject.com/KB/cs/DelegatesAndEventsHowToo.aspx
    http://www.codeproject.com/KB/cs/events_made_simple.aspx


  • Registered Users Posts: 146 ✭✭waynewex


    You can use JavaScript.
    getElementById('nameofelement').value

    Then your form element could be:
    <textarea name="nameofelement" id="nameofelement"></textarea>


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Thanks,

    It seems like there's an awful amount of code to do achieve something so small.

    @waynewex.. Soryy, I forgot to say this is in c#.


  • Registered Users Posts: 515 ✭✭✭NeverSayDie


    techguy wrote: »
    Thanks,

    It seems like there's an awful amount of code to do achieve something so small.

    Shouldn't be too much, those articles I posted contain a lot of background material in case you're not familiar with events and delegates, actual code for this won't be too involved I'd say. It's an encapsulation thing basically - something belonging to the internal depths of Form1 wants to use something belonging to the internal depths of Form2, so it's best to keep a clean interaction between the two instead of just making internal stuff public. Also avoids coupling problems if Form1 later decides to get its stuff from someone other than Form2.


  • Registered Users Posts: 981 ✭✭✭fasty


    Is the 2nd form just going to be a modal dialogue?

    In any case, I appreciate you're probably pretty new to WinForms and the like, but it's not that much code.

    Try something like this...

    Main Form:
    namespace WinformPlaypen
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 dialog = new Form2();
                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    this.textBox1.Text = dialog.FormText;
                }
            }
        }
    }
    

    Dialog:
    namespace WinformPlaypen
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            public String FormText
            {
                get { return this.textBox1.Text; }
            }
        }
    }
    

    You just need to make sure you set the DialogResult properties of the OK and Cancel buttons on your popup window correctly.

    I used textboxes, but really you can use whatever control and datatypes you want in this fashion and access the data via properties when the user clicked OK.

    There are loads of other ways of doing this if it floats your boat. You could use events to tell the parent form that ok was clicked and put the new value in the eventargs, you can pass the data to the dialog itself as a reference so when ok is clicked, the parent form has the updated value and so on.


  • Advertisement
  • Registered Users Posts: 2,234 ✭✭✭techguy


    Hey fasty,

    Thanks for introducing me to dialogboxes..

    I checked out this article and have things working perfectly now.

    It seems I am pretty bad when I comes to WinForms.. We never really did much on this in college. I wonder are there any course I could do that would make me aware of all the features in Windows Forms programming..?

    Thanks for the help guys..


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    waynewex wrote: »
    You can use JavaScript.

    Not in winforms you can't.


  • Registered Users Posts: 981 ✭✭✭fasty


    techguy wrote: »
    Hey fasty,

    Thanks for introducing me to dialogboxes..

    I checked out this article and have things working perfectly now.

    It seems I am pretty bad when I comes to WinForms.. We never really did much on this in college. I wonder are there any course I could do that would make me aware of all the features in Windows Forms programming..?

    Thanks for the help guys..

    You could check out these instructional videos http://windowsclient.net/learn/videos.aspx and basically keep doing what you're doing and try stuff out and ask questions when you're stuck!


  • Moderators, Science, Health & Environment Moderators Posts: 8,954 Mod ✭✭✭✭mewso


    Been a while since I did any winforms stuff but the easiest way I did these kind of things was to pass a reference. In form 2 I would have:-

    private _parentForm;

    public void Form2(Form1 frm) {
    _parentForm = frm;
    }

    When opening this form from form1 I would have:-

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

    Now when the user clicks the ok button in form2 you can simply set the value directly using the _parentForm reference. You could also create a public method in form1 to set the value that form2 can call without directly accessing the textbox. I can't claim this to be a recommended method just that it works :)


Advertisement