Click to See Complete Forum and Search --> : ComboBox event issue


Scribe
October 10th, 2008, 03:26 PM
Hi guys,

I have a ComboBox which when entering focus I wish for it to populate itself with values from an SQL server.

At the moment I have:
private void supplierNameBox_Enter(object sender, EventArgs e)
{
SupplierName.Clear(); //This is a table
DbDataAdapter.SelectCommand = supplierNameSelectCommand;
DbDataAdapter.Fill(DbMenuDataSet, "Supplier");
}

However when my first click that brings the box into focus is the drop-down arrow, it somehow manages to duplicate the data entries, displaying them twice. Yet when i add a line such as:
MessageBox.Show("bla"); to the top of the function it works fine, except obviously I don't want a message box popping up!

Any help would be amazing guys.

darwen
October 11th, 2008, 04:23 AM
Firstly, check that the entries haven't been added in the IntializeComponent method of the form.

This can happen if you add entries to combo boxes/list boxes etc in the constructor of the form : when in the designer the constructor gets called and when you then save it also saves the contents of the combo boxes.

Are you sure you don't have duplicated entries in the database ? Or maybe your SQL statement for filling the combo box is returning duplicated results : a 'SELECT DISTINCT' should sort this out.

Maybe you need to call Items.Clear() on the combo box before filling it too.

Darwen.

Scribe
October 11th, 2008, 08:55 AM
I think my issue is that the clear() command is asynchronous. So I need some kind of flag however the Clearing and Cleared events don't seem to work properly.

eclipsed4utoo
October 11th, 2008, 09:53 AM
put this line of code at the beginning of the Enter event.

supplierNameBox.Items.Clear();

Scribe
October 11th, 2008, 10:42 AM
I've tried that and it prevents the box from displaying anything at all.

From what I can now see the table is populating with 4 values even though there are definitely only two entries on the database.

If I stop clearing the table and set constraints on the primary key, only the two values will show, but I cant enter the drop down box until I have clicked elsewhere on the ComboBox first.

I still need clear in order to refresh my data. However, when using clear() with the restraints, it completely ignores them and loads double values again. What's strange is when clicking on the ComboBox and running clear() once, it removes the two rows correctly. However entering the ComboBox by clicking immediately on the dropdown arrow forces the Enter event to run twice, the second time around clear doesn't seem to work at all and 4 values are loaded into the table.

EDIT: Ok I've realised the Fill() command does what Clear() does anyway. This fixes my duplicate issue. However the problem is that when I fill the box with new data it prevents the use of the drop down arrow until the cursor is active in the box first.

eclipsed4utoo
October 11th, 2008, 03:29 PM
why don't you populate the box in the "DropDown" event?

Scribe
October 12th, 2008, 02:58 PM
why don't you populate the box in the "DropDown" event?

The issue is that I also employ auto-complete, so users may not drop-down at all. Also having both Enter and DropDown events doesn't solve the issue. When clicking on the dropdown box before entering the box will populate then lock out the DropDown arrow and related events until you enter the box by clicking on the text field.