Connecting Tech Pros Worldwide Forums | Help | Site Map

Update combobox from a dialogbox?

Newbie
 
Join Date: Jan 2009
Posts: 3
#1: Jan 30 '09
Hi! I have just started programming C# and I have a problem.

On my main form, I have a combobox filled with names of people. To add more people to the combobox I want to open up a dialog box and fill in the person's name in a text field and when I hit the save button it should update the combobox on my main form. How do I do this?

/ Emil

vekipeki's Avatar
Expert
 
Join Date: Nov 2007
Posts: 231
#2: Jan 30 '09

re: Update combobox from a dialogbox?


Where exactly are you stuck?

To solve your problem you will need to understand these topics:
  1. How to add items to a combo box: ComboBox.Items property
  2. How to create custom events to pass the text from the dialog, back to the main form: Creating custom events
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,782
#3: Jan 30 '09

re: Update combobox from a dialogbox?


While I agree with vekipeki that the completely correct way to do it would be to pass the name as the arguments of an event... its always good to realize there is more than one way to accomplish any task.

Two other important ideas to learn is that (1) Since your main form would be be the 'parent' of a second form it basically knows how to talk to it's 'child'. It knows all its properties, or at least how to check them. and (2) just because you Close() a form doesn't mean it is gone from memory if it hasn't fallen out of scope. In other words as long as you are still using it in your method it still exists even if it is not shown.

Form1 creates and opens Form2 expecting a DialogResponse, which will come from the user clicking [OK] after entering their name.

Now, just because you don't see the form2 after the user clicks [OK], doesn't mean its totally dead yet. You can still ask it for information such as the tbNameTextBox.Text property.

I leave it to you to work out how to assign the DialogResponse.Ok to your button, and format the rest of the code. The fun is in the figuring out and learning.

I would suggest you try to accomplish this task using both methods, doubling your arsenal of coding techniques. Hollar if you get stuck.
Newbie
 
Join Date: Jan 2009
Posts: 3
#4: Jan 31 '09

re: Update combobox from a dialogbox?


I am totally confused, I do not understand a thing of custom events.

But I got this to work, just do not know if it is the right way.

Button1 on Form1:
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.    Form2 f2 = new Form2();
  4.    f2.ShowDialog();
  5.  
  6.    if (f2.DialogResult == DialogResult.OK)
  7.    {
  8.       MessageBox.Show("It works!");
  9.    }
  10. }
Button1 on Form2:
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.    DialogResult = DialogResult.OK;
  4. }
Och för att få med textbox1.Text from Form2 behöver jag använda custom events?
vekipeki's Avatar
Expert
 
Join Date: Nov 2007
Posts: 231
#5: Jan 31 '09

re: Update combobox from a dialogbox?


Quote:

Originally Posted by tlhintoq View Post

just because you Close() a form doesn't mean it is gone from memory

Good point - when you open a form using ShowDialog(), it is not actually closed (and therefore not disposed) when you try to close it. In fact, you should dispose it manually when you don't need it anymore, to make sure that all handles are released as soon as possible.

Adding custom events to modal dialog boxes for basic user input is rarely necessary (on the other hand, thinking twice before posting is often necessary - my fault) :).

For example, you could add a property to your dialog (Form2) in order to encapsulate your data a bit (your TextBox control should remain private):

Expand|Select|Wrap|Line Numbers
  1. public string EnteredText
  2. {
  3.     get { return textBox1.Text; }
  4. }
  5.  
And then access that property just before disposing the dialog. Common way to dispose a dialog immediately after use is using the "using pattern":

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     using (Form2 f2 = new Form2())
  4.     {
  5.         if (f2.ShowDialog() == DialogResult.OK)
  6.         {
  7.             MessageBox.Show("Entered text was: " + f2.EnteredText);
  8.         }
  9.     }
  10. }
The "using" construct not only makes a smaller scope for f2, but also makes sure that the form is closed and disposed after the construct.
Reply