473,411 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Update combobox from a dialogbox?

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
4 2275
vekipeki
229 Expert 100+
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
3,525 Expert 2GB
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
emhiil
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
229 Expert 100+
@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

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

Similar topics

1
by: thomaz | last post by:
When i make an update in my database, the modifications are showing in all TEXTBOX but i can´t see the new values in a combobox text. If i close the form and open it again the new values will...
1
by: foreman | last post by:
Hello Everybody: I am trying out DialogBox, and I have been trying to press the OK Button on the dialogbox hoping I can receive the OK DialogResult to Main Form. Below is my OK button click...
3
by: majid | last post by:
When i show a webform as a dialogbox using showModalDialog script any postback event in dialogbox cause a new window pops up . How can i control the postback?
2
by: cashdeskmac | last post by:
I am building an application and would like to give the user the chance to configure the look of the user interface. I am adding a ComboBox to a DialogBox and want to populate it with the...
0
by: Reena | last post by:
Hi, Working with ASP.NET 2.0 with Masterpages. Here is my datagrid... <asp:DataGrid ID="dgSelectedFields" runat="server" BorderStyle=None GridLines="None" CssClass="grdData"
1
by: fiaolle | last post by:
Hi I have tried to update my combobox with no luck. I add records to the table, the combobox has as Datasource, but I don't know how to update the combobox. I have tried updata the oledbadapter...
9
by: joemo2003 | last post by:
Please help. I created a custom dialogbox name "DialogBox". What i want is input values to the dialogbox, and those values appear on a textbox in Visio. After i creat the textbox and the dialogbox,...
5
by: njb35 | last post by:
Hi all I'm beginning my foray from VBA into VB 2005 Express, and enjoying some of the efficiencies it provides! I'm stuck with some dataset handling however that I _think_ can be automated but...
1
by: duwei | last post by:
I got a example, it GetBkColor from a HDC(the dc get from a DialogBox), but it BkColor always color white, dialogbox default BkColor is color like gray, how can I do to get the correct color for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.