I have a combo box on a form (MainForm) that's bound to a column in a database table. I want this combo box to get it's list of selections from a second table (LocationsTable). Items are added or deleted from LocationsTable via a second form (frmLocations).

If I have MainForm open and I then open frmLocations to say, add a new location, the LocationsTable is updated properly, but when I close frmLocations, the Combo Box on the MainForm doesn't reflect the updated selections. frmLocations is modal and I added a "Done" button to close it.

Since I am teaching myself C# (sorry, can't afford to pay for college or a course), I'd like to figure this out on my own, but I have wrestled with it for 7 straight hours so far! It would help if someone can tell me if I am thinking about this correctly, and if not, how to solve this. Here's what I THINK:

I think I should add code behind the "Done" button on frmLocations that will refresh the combo box on the MainForm. If that's correct, I could use a hint on what code to use. If that's NOT correct, then I am completely lost on this concept and would appreciate some help!

Recommended Answers

All 3 Replies

I think I should add code behind the "Done" button on frmLocations that will refresh the combo box on the MainForm.

I think you're on the correct path with this theory :)

Basically, what you need to do is isolate the code that you're using to populate your comboBox in the first place into a code-behind procedure and include a Clear() style statement at the top of the population. Then when you use your "done" button on the other form it needs to call to the population procedure.

Basically by doing this, every time the combo-box is populated it first clears old values and then populates the current values from the table. When called from the "done" button it will do the same, repopulating the box with the updated info.

The Clear() statement should be something like comboBox1.Items.Clear() or similar (I haven't opened my VS C# yet today so can't be 100% sure there).

Hope that helps :) Please remember to mark the thread solved once your issue is resolved.

I agree with Lusipher that you need to look through your code and find where the list is being filled initially. Once you have that code you can use it to fill the list any time you need to refresh it, and putting it in a method makes it more easily reusable.
However, if the form you use to add a new item to the database is modal you can use the DialogResult property of the form to determine if the list needs to be updated and perform the update on form1. This removes the need to obtain a reference to Form1 within Form2.
ie:

Form2 frm = new Form2();
DialogResult result = frm.ShowDialog();
if(result==DialogResult.OK)
{
    //refresh list
}

You can set the result in Form2 using this.DialogResult = DialogResult.OK (or any of the DialogResult values).

Sorry it took so long to respond. Much thanks to you both; my understanding is increased, and my issue is resolved as well!
Ryshad, that code worked; I studied it thoroughly to make sure I understood it clearly.
What I wound up doing was, I set the DialogResult property of the button used to close Form2 to "OK".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.