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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

WinForms: How to access textbox in another form.

  • 15-01-2010 3:09pm
    #1
    Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    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, Registered Users 2 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




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

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


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


    Thanks,

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

    @Markus Whispering Munchies.. Soryy, I forgot to say this is in c#.


  • Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 2,236 ✭✭✭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, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    You can use JavaScript.

    Not in winforms you can't.


  • Registered Users, Registered Users 2 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: 9,035 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