Urgh, I have spent the last couple of hours on this now. I normally end up finding the answer from a bit of Googling, but not with this one. Bit of a headache.
My questions:
How can I catch when a user clicks the dropdown arrow on a combobox and prevent the dropdown list from being displayed.
How can I then clear and populate the dropdown list and display it programmatically?
I have one agent program remotely connected to a server over the internet. When you click the dropdown arrow on the agent, it queries the server to determine what needs to be in the dropdown list. It then displays the dropdown list. The comboboxes act as filters for the subsequent comboboxes on the GUI. A delay in displaying the dropdown list is perfectly acceptable while retrieving the data. Initially querying all the possibly entries in the dropdown list is not an option because there are so many! Needs to be comboboxes compared to listboxes as the user may also type an entry that is not in the list.
Hopefully this will clarify what I am doing:
GUI on the agent:
ComboBox1 - displays the countries
ComboBox2 - displays the cities - dropdown list determined by ComboBox1 selected item
ComboBox3 - displays the towns - dropdown list determined by ComboBox2 selected item
ComboBox4 - displays the streets - dropdown list determined by ComboBox3 selected item
Instead of populating the drop down when the user clicks the drop down button I would suggest that you populate and enable the following combo box when the value of the previous combo box changes. If populating the combo box is slow the delay is much more pleasant after the user has selected a value than before the user is going to select a value.
Assuming you are using Windows Forms here is a handler for the first combo box:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox2.Enabled = True
' Fill ComboBox2 based on ComboBox1.SelectedItem
ComboBox2.Items.Clear()
ComboBox2.Items.Add("Foo")
ComboBox2.Items.Add("Bar")
End Sub
Note that ComboBox2 to ComboBox4 are disabled intially and only are enabled when they are filled with data.
Related
I have a feeling this is a very simple thing that I'm overlooking.
I have two ComboBoxes that allow users to search for/select the record that they want to view. One is filled with Customer Names and the other is filled with Customer Numbers, so the user can look for a particular record by either selecting the Name or Number.
Each ComboBox is filled by a Data Table returned from a SQL Server database.
Each ComboBox has DropDownStyle set to DropDown, AutoCompleteMode set to SuggestAppend and AutoCompleteSource set to ListItems.
The user can either select by clicking the DropDown arrow and then clicking on the item they was or they can begin by typing and the ComboBox narrows the number of items in the list based on the characters the user is typing.
Using the mouse to click on the item in the list that they want works fine...it fires off a routine to retrieve the selected item from the database.
However, when the user types in the desired selection and presses ENTER, nothing happens. They must click the DropDown arrow and click on the item in order for the program to pull the appropriate record.
How do I get the ComboBox to pull the appropriate record when the user hits enter?
I'm using Visual Basic.
From the sounds of it, you need three events.
You need to use a timer to know when the user has stopped typing. To do that, you need one event would be when that field they're typing in has it's value change (<control's name>.TextChanged). That would start/restart a timer ticking (so that the user has a couple seconds to pause before the next event fires).
The next event would be the Tick event for that timer. That event would stop the timer, and then give focus to the right field so that when the user hits ENTER, they're not hitting ENTER in the field they've been typing in. You'll need to write a function to look up the right item in the ComboBox and call that.
Then you'd have a third event, either KeyPress, KeyDown, or KeyUp on the ComboBox itself. I'd lean towards the KeyUp to avoid issues if the user holds ENTER for whatever reason. That'd be what selects the item.
As a final FYI, I'm assuming you're using Visual Studio to write your code. If not, you should, and if you are/once you are, you can select the field you want to work with in the drop-down at the top left of the editor and then look at the associated events in the top right drop-down.
Thank you to JMichael for getting me on the right track with this one. I'm posting my solution here just in case it helps someone who has a similar question in the future:
The code that I added to the ComboBox's SelectionChangeCommitted event needed also to be added to the ComboBox's KeyUp event:
Private Sub cboPolicySearch_KeyUp(sended as Object, e As KeyEventArgs) Handles cboPolicySearch.KeyUp
If e.KeyCode = Keys.Enter Then
GetSelectedPolicySearchRecord()
e.Handled = True
End If
End Sub 'cboPolicySearch_KeyUp
The GetSelectedPolicySearchRecord() sub contained all of the information I needed to call my SQL Stored Procedure to select the data for the record that the user selected in the ComboBox.
Previously, this was only being called from the ComboBox's "SelectionChangeCommitted" event which is executed when the user clicks the drop down and then clicks a policy number from the drop down list.
I needed to add the same call to the GetSelectedPolicySearchRecord in the ComboBox's "KeyUp" event for when the user presses enter.
My situation is quite simple, a user is presented with a drop down menu (combo box) of customers, if the customer needed to fill the form is not present in the database already the user can click an "Add Customer" button which opens up a pop up form to enter the customer details and insert the new entry into the customers table.
I'm nit picking now but, if the user now wants to select the newly entered customer they must re-select the customer from the combo box which will now present a new entry, is there a way of automatically making the combo box default to the newly inserted customer with an "On Close" event on the pop up form?
This is the "On Close" code I already have which refreshes the customer combo box to add the new entry, is there anything I can add which will make it so the box defaults to the new customer entered?
Private Sub Form_Close()
If CurrentProject.AllForms("edit appointments").IsLoaded Then
Forms![edit appointments]!customerCombo.Requery
End If
If CurrentProject.AllForms("edit purchases").IsLoaded Then
Forms![edit purchases]!customerCombo.Requery
End If
End Sub
I figured it out.
Forms![edit appointments]!customerCombo = DLast("ID", "customer")
This takes the last entry in the "customers" table and sets the value of the combo box to the value contained in the field "ID".
vb.net 2012
I have two comboboxes with a DropDownStyle of DropDownList. I am selecting one of the ComboBoxes when the form loads, cboMyBox.Select(). My problem is that when I select (or focus, or selectall) the combobox programmatically it doesn't show the dotted line highlight. When I tab between controls the dotted line highlight shows up just fine. I am filling the combobox with data before selecting it.
How can I get the dotted line highlight to show up when I select the control in the code?
Combobox selected in code but no highlight
Tab to next control and highlight shows
Ctrl+Tab back to initial combobox, hightlight shows
I have done some experiments and it seems that once you have tabbed to any control like a combo box or a text box and later use the Select() or Focus() method, the select box (dotted rectangle) appears in all the combo boxes; even if you didn't tab to all of them previously.
The trick is to tab to some controls with SendKeys when the form opens.
Private Sub Form1_Shown(sender as Object, e as EventArgs) _
Handles Form1.Shown
' Select the control preceding a combo box in the tab order.
textBox1.Select()
SendKeys.SendWait("{Tab}")
SendKeys.SendWait("{Tab}")
' Select the fist control to be selected when form opens.
btnFocus.Select()
End Sub
It works only if the TAB key is sent twice (don't ask me why, maybe a timing issue).
I have a combobox that is of type dropdown, i.e. allows user to either select something from the list or to type in a new value. Based on this combo value, other values are selected in the form automatically as they are databound.
Customer_ID is the field that needs to be picked or typed in, and based on it Customer first name and second name are updated in the form automatically. This works fine if I pick some value from dropdown, lets say Customer Id = 1111.
Now in place of picking, if i type in this value and press tab, no udpate happens on the Name fields. Please suggest what am I missing here.
I think the best you are going to do with the standard ComboBox is to handle the Leave event, or possibly the TextChanged event. I did some experimenting and typing in a value does not add to the list, it just changes the Text property. It doesn't even change the value of the item you typed over. And, it doesn't fire SelectedItem or IndexChanged events.
So if you handle the Leave event, you have a place to do your lookup when someone tabs off the ComboBox. But the item won't get added to the list.
If you need functionality not provided in the default control, you could redesign your UI with perhaps a TextBox and Button that add an item. Or, you could look for third party implementations of ComboBox controls that would have more functionality.
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
Dim theCB As ComboBox = DirectCast(sender, ComboBox)
DoLookup(theCB.Text)
End Sub
I want to know what control to be use on my project is it Listview or listbox???. I have a comboBox control on my project what I want to do is when I selected 1 item on my combobox it will automatically add on listbox or listview and when I selected more than 1 item I want to add it on listbox or listview on newline...
Is it simple, please help me to do that in listbox or listview..thanks!
Listbox > Is for viewing too but user can select it
Listview > Is for viewing only, user cannot select also it viewing by five view directly cause it's for viewing only
If your project wanted the list to be viewing from what have been select by Combobox, then you just pick List View, but if you want for viewing also user can select it, better use listbox, so it's up to you.
Also you can know how the Tools work by focus your mouse cursor to the tool, then it will pop up an tooltip that write what the tool for.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ListView1.Items.Add(ComboBox1.SelectedIndex)
End Sub
That is the code to viewing in listview for what you select in combobox
For clearing all the item in the Listview or listbox, just write to your form_load
Listview.items.clear
Why i said in form load, cause the list just for viewing, of course every time the form begin to run, it will need blank list, so the best is put in form load
UPDATE
To remove the selected index in listbox
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
ListView Items can be selected a couple of ways In the Properties window for the ListBox the activation property allows an item to be activated by one click or two clicks. Here is an example of how the selected items can be used
If Me.ListView1.SelectedItems.Count = (1) Then
'Declare the selected item
Dim lviSelectedItem As ListViewItem = Me.listView1.SelectedItems(0)
'Now you can use it
lblLabel1.Text = lviSelecetedItem.Text
Else
lblLabel2.Text = "You can make Items selectable by switching CheckBoxes property to True in the Properties windows and using CheckBox event handlers"
End If