ComboBox doesn't highlight when selected programmatically - vb.net

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).

Related

Visual Basic Can't Type in Listbox

I am using Visual Studio and working in a Windows Form Application. I have added a Listbox on my form. The problem I am having is that when I run my application I can't add any text or even select the Listbox to put my cursor in.
You have to add values or action to listbox before run it,if you run it without value or action absolutely that will give you nothing.
To add a value in listbox,right click on listbox then select "edit items...".
Or you can make a action like : add text to listbox from textbox,do looping,or another...
From the design window from theToolbox add a TextBox and a Button to your form. (The ListBox is already there, right?) Enter the following code to your form in the code window.
'This is the code that runs when the users clicks the button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'This is the code that copies the text in the text box to the list box
ListBox1.Items.Add(TextBox1.Text)
'This code clears the text in the text box
TextBox1.Text = ""
End Sub
The text in the gray window that appears in light gray and is preceded with an apostrophe are comments; not part of the code.
Now run the program and type in the text box. Click the button and the text you typed will appear in the list box. Now that you have had this brief introduction to Visual Basic . NET try searching for a beginners tutorial. Come back when you have some code that you wrote but doesn't turn out as you thought.

How to start with blank text boxes when the form loads

I have a unit converter written in Visual Basic, using Visual Studio 2012. My frmMain_Load event handler is posted below. I am using text boxes, and combo boxes. I have textchanged, and SelectedIndexChanged events set up for both sides of the converter. My problem is that when the form loads it triggers these events, therefore, causing the program to convert empty strings that returns a zero in the text box at the start of the program. I would rather have blank text boxes. Any help or opinions would be greatly appreciated on this matter. Thanks in advance.
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Show() ' Creates the onscreen controls so the focus can be set
PopCombo() ' This procedure populates the combo boxes
cboUnitType.SelectedIndex = 0 ' Sets the default selection on the main combo box
txtUnit1.Focus() ' Sets the focus on the first text box
End Sub
Instead of using SelectedIndexChanged, you should use SelectionChangeCommitted in order to respond only if the user is really the one who changed the selections.
And about the textbox event, I would go with Mr. CoDeXeR's option.

Showing List of combobox while getting focus (vb.net)

When we click on drop down combobox control in our windows form, it shows the list automatically.
But when we press tab and navigate to that control from keyboard it doesn't shows the list automatically. So in other to show the list automatically on receiving focus what should be done?
Set the DroppedDown property of the combobox equal to true.
Private Sub myComboBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myComboBox.GotFocus
myComboBox.DroppedDown= true
End Sub
I would like to mention on thing here.
I used Enter event to show Drop down list with DroppedDown=true,
But When I type something in text area of combobox and if i leave the area to next control, entered text is lost.
My combobox is databound.

Clean way to display/hide a bunch of buttons based on a ComboBox selection

I'm writing a standalone application in VB.NET using Visual Studio 2005.
I want to display/hide a bunch of Buttons based on the selected value of a ComboBox. Each selection would have a different set of Buttons to display, and I'd like to have them arranged in a nice grid.
Driving a TabControl with the ComboBox value would be the kind of behavior I want, but I don't want it to look like a TabControl to the user because it might be confusing.
Is there a way to do this?
Basically, I'd like Selection1 of the ComboBox to show Buttons 1-4, Selection2 to show Buttons 5-11, Selection3 to show (maybe) Buttons 1, 3, 5, 6, and 8, etc., have them arranged nicely, and have the GUI show only the ComboBox and the buttons.
Thanks in advance as always!
Use a Panel control (or multiple if the items aren't grouped right next to each other) and set the visibility accordingly.
(Added)
You CAN stack panels on top of each other, so that the buttons all look like they're in the same location. but it becomes a maintenance nightmare and I don't recommend it.,
Hack warning - the following is a hack, but it works.
Another option is to use a tab control, but hide the tab buttons. (You can do this by positioning a panel over the buttons, but you have to be careful of letting the user resize the form.) Then you set the TabIndex based on the drop-down changing.
Edit again - added per comment
If you use the hack, you can add this into the ComboBox's selected index changed event....
(code may be wrong, as I'm not at my dev pc and can't check, but you get the idea)
TabControl1.SelectedIndex = ComboBox1.SelectedIndex
You could put all of your buttons on a panel on your form. Then when the SelectedIndex event of the combobox fires, you can loop through the buttons on the panel and turn them on and off based on their Tag property.
For this example you would set the Tag property of each button equal to the combobox index or indexes that you want it to turn on for. If you want it visible for more than one combo selection just comma seperate the index values in the tag property.
You don't have to key off of the combobox index. You could use the selected text for example. If you did that, just put the texts to show the button for in the tag property and change the code from ComboBox1.SelectedIndex.ToString to ComboBox1.SelectedText.
The buttons will turn on and off where they are placed at design time, but you could add some code here to arrange them dynamically as well so that all the visible buttons are neatly arranged.
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
For Each ctrl As Control In Me.Panel1.Controls
If TypeOf ctrl Is Button Then
If Array.IndexOf(Split(ctrl.Tag, ","), ComboBox1.SelectedIndex.ToString) > -1 Then
ctrl.Visible = True
Else
ctrl.Visible = False
End If
End If
Next
End Sub
Maybe using a FlowLayoutPanel will help you display the buttons.
You can use a jagged array to define which buttons belong to which combo-box item.

Intercepting combobox dropdown

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.