By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,516 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,516 IT Pros & Developers. It's quick & easy.

Update combobox from a dialogbox?

P: 4
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
Jan 30 '09 #1
Share this Question
Share on Google+
4 Replies


vekipeki
Expert 100+
P: 229
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
Jan 30 '09 #2

tlhintoq
Expert 2.5K+
P: 3,525
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.
Jan 30 '09 #3

P: 4
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?
Jan 31 '09 #4

vekipeki
Expert 100+
P: 229
@tlhintoq
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.
Jan 31 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.