combo box steals focus

ppirates

Member
Joined
Jul 19, 2006
Messages
12
Programming Experience
10+
I have a databound combo box on my form, but when I select an item from it other than the default, it keeps the focus and won't let me do anything else. I can't even get the focus back by clicking on another control.

Any help with this would be fantastic.

Thanks
Colin
 
The combo box is databound, so there is no code for that.
The form_load only has the command to update the datatable, which was generated by the databinding for the combobox in the combobox properties.

No other code is invoked until I press the save button on the form. I can't even get this far, as the combo box holds focus forever once the selected value is changed in the combo box.

I am guessing that a property needs to be changed on the combo box itself, but I am not experienced enough to know what this would be.

Thanks
Colin
 
Here is the form_load code

Private Sub New_Cust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Acre_crmDataSet1.Employees' table. You can move, or remove it, as needed.
Me.EmployeesTableAdapter1.Fill(Me.Acre_crmDataSet1.Employees)
End Sub

When I added the combo box to the form, I changed the properties in the data section to a binding source EmployeesBindingSource which I had added to the form. I then set the data member to EmployeeName.

When the form loads, the combo box is populated with the data from the table, and works ok if left alone. It is when it takes focus on clicking on it that it holds focus and won't let go.

Incidentally, the EmployeesBindingSource has the properties...
Datasource = AcreCRM_dataset1
Datamember= Employees

Employees is my table in the database.

I hope this helps.
 
Very strange..... i can't find a reference for this happening anywhere and what you have done shouldn't create that kind of behaviour....Mmmmm
 
This is generally why I stay away from the data access threads, there are way too many variables. But since I had this exact same problem yesturday, I'll share my experience. Oh and by the way, I'm moving this thread to the VB.NET > Data Access category ;).

First about the combobox: usually the combobox will be bound to two different datasources; one will fill the list (properties include DataSource, DisplayMember, ValueMember, ...) and the other is where you want the selected value stored (the property in this case would be under DataBindings, usually SelectedValue). Instead of using bindings to get the list of items, you could manually add the list using the Items property.

Your description of the data model you're using is lacking so I'll use the standard Orders-Products model. You also didn't mention binding to any property under the (DataBindings) property of the combo so all this based on many assumptions (another reason for me to stay away from the data access area) :D.

In the example I'm giving, the form that the combobox is on would be a form for creating orders. The combobox would get the items for it's list from the Products table, but the information (the ProductID) is stored in the Orders table.
Using the Orders-Products model you would set the properties for the comboBox like this:
DataSource = ProductsTableBindingSource
DisplayMember = ProductName
ValueMember = ProductID
(DataBindings)
SelectedValue = OrdersBindingSource.ProductID

Now my problem was that I was using the Text property of the combobox to bind to the Orders table when I really should have been using the SelectedValue. The combobox was keeping the focus and the only way I could end the app was to press the Stop button in Visual Studio, even the close button on the form wasn't responding.

If this scenario doesn't fit your problem, please be more specific.
Good luck.
 
Paszt said:
Now my problem was that I was using the Text property of the combobox to bind to the Orders table when I really should have been using the SelectedValue.

which were you using: a dropdown or a dropdownlist styled combo?
 
Firstly paszt, thanks for a very comprehensive reply. I appreciate the time you spent explaining this.

Unfortunately, I am using the selectedvalue property on the combobox.

The form is to add a new customer to the system, and the employee is the employee that is the sales rep forthis customer.

I have one datasource for adding the Employee data to the combo box (The one that is currently causing the problem) and I have another query in the tableadapter that is writing the new customer record to the database.

If I don't bring focus to the combo box, I can quite happily press the save button on the form and the data goes onto the database.

Properties set on the combobox are....

datasource = EmployeesBindingSource
Displaymember = EmployeeName
items = (Collection)
SelectedValue = EmployeesBindingSource - EmployeeName

I am having the same problem as you were, in that the only way to close the form was to stop running.

Cheers
Colin
 
dont set datasource and items together. additionally, if youre using displaymember you must pick a value member, otherwise your selectedvalue will not contain anything..

for your combo in DROPDOWNLIST style, (i.e. cannot type free text in) you set:
datasource = some datatable with lookup values
displaymember = column in datasource containing displayed text
valuemember = column in datasource containing underlying value
selectedvalue = the databinding to the data source to be updated


suppose we have a dataset with people and a lookup table with Mr, Mrs, Miss as 1,2 3. these columns called DISP_TITLE and ID_TITLE respective:

combo1.DataSource = MyDataset.tbl_Title_Lookup
combo1.DisplayMmeber = "DISP_TITLE"
combo1.ValueMember = "ID_TITLE"

and set the databinding for SelectedValue in the properties window, to MyDataSet.tbl_People.Person_Title
 
Back
Top