tags:

views:

50

answers:

4

Dear reader,

I have a Windows form that's generated using code (including buttons and what not). On it, amongst other things, there is a text box and a button. Clicking the button opens a new Windows form which resembles an Outlook contact list. It's basically a data grid view with a few options for filtering. The idea is that the user selects a row in this home-made contact book and hits a button. After hitting that button, the (second) form should close and the email address the user selects should be displayed in the text box on the first form.

I cannot use static forms for this purpose, so is there any way to let the first form know the user has selected something on the second firm? Can you do this with events, or is there another way? Mind that I hardly know anything about delegates and forms yet.

Please advise.


Edit 1 = SOLVED

I can return the email address from the second form to the first form now, but that brings me to another question. I am generating controls and in that process I'm also generating the MouseClick eventhandler, in which the previous procedure for selecting a contact is put.

But how do I, after returning the email address in the MouseClick eventhandler, insert that information into a generated text box? Code to illustrate:

        btn.MouseClick += new MouseEventHandler(btn_MouseClick);

That line is put somewhere in the GenerateControls() method.

    void btnContacts_MouseClick(object sender, MouseEventArgs e)
    {
        using (frmContactList f = new frmContactList())
        {
            if (f.ShowDialog(fPrompt) == DialogResult.Cancel)
            {                    
                var address = f.ContactItem;
                MessageBox.Show(address.Email1Address.ToString());
            }                
        }
    }

That appears separately in the class. So how do I put the email address into a text box I previously generated?

+2  A: 

Forms in .Net are normal classes that inherit from a Form class.

You should add a property to the second (popup) form that gets the selected email address.

You should show the popup by calling ShowDialog.
This is a blocking call that will show the second form as a modal dialog.
The call only finishes after the second form closes, so the next line of code will run after the user closes the popup.

You can then check the property in the second form to find out what the user selected.

For example: (In the first form)

using(ContactSelector popup = new ContactSelector(...)) {
    if (popup.ShowDialog(this) == DialogResult.Cancel)
        return;
    var selectedAddress = popup.SelectedAddress;
    //Do something
}
SLaks
Thank you very much for your answer, I'm going to try it out now.
Kevin van Zanten
It doesn't seem to work as I expected. I can access all the properties but I can't get them to show. I must also admit that I don't understand your method completely (I'm still studying). If you would be able to point out where I'm going wrong I'd greatly appreciate it.Also, is it possible to post bits of code in the comment area? I want to show you what I have but it's not formatting correctly.
Kevin van Zanten
Please edit your question to include your code.
SLaks
No longer necessary! I got it to work now. I can now return the email address I set as a property in the second form. Thank you very much, I'm sure this will help me immensely with my future projects.
Kevin van Zanten
A: 

In response to my first edit, this is how I solved it. If anyone knows how to make it more elegant, please let me know.

    void btnContacts_MouseClick(object sender, MouseEventArgs e)
    {            
        using (frmContactList f = new frmContactList())
        {
            if (f.ShowDialog(fPrompt) == DialogResult.Cancel)
            {
                var contact = f.ContactItem;
                TextBox tbx = ((Button)sender).Parent.Controls[0] as TextBox;
                tbx.Text = contact.Email1Address;
            }
        }
    }
Kevin van Zanten
A: 

You should keep a reference to your generated TextBox in a variable (private field in your class) and use this instead of looking it up in the Controls array. This way your code would still work even if you some time in the future change the location it has in the array, and you would get a compiler message if you removed that field, but forgot to remove the code that used it.

Tor Livar
I normally would, but it's completely generated and it's possible that there will be multiple TextBoxes created, so I can't store them.
Kevin van Zanten
A: 

If the second form is modal, I would recommend that rather than having the first form create an instance of the second form and use ShowModal on it, you should have a Shared/static function in the second form's class which will create an instance of the second form, ShowModal it, copy the appropriate data somewhere, dispose the form, and finally return the appropriate data. If you use that approach, make the second form's constructor Protected, since the form should only be created using the shared function.

supercat