How to start with blank text boxes when the form loads - vb.net

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.

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.

ComboBox doesn't highlight when selected programmatically

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

winforms vb 2008 textbox validation issue

I have vb 2008 Winforms application.
In my Branch form I have 11 textbox fields and I want to validate 8 of them, either mandatory, or required format such as UK postcode, UK tel nos etc.
My issue now is that when the validation starts, it is validating the last text field first ( or seems to be)
here is my code
For Each oCtrl As Control In Me.Controls
If TypeOf oCtrl Is TextBox Then
oCtrl.Focus()
If Validate() = False Then
Exit Sub
End If
End If
Next
what's wrong please?
what's wrong please?
The controls collection isn't sorted or grouped. Your loop will access them in whatever order the collection has them.
Without more code it's hard to say how to fix it. However a tip may be in order. Use the same handler to handle the validate event for each textbox. This way you can keep the user at that textbox until the input is valid.
is it possible to add the items to the collection in the order of their tab indexes on form Shown event, how would I do that please?
A List(Of TextBox) and a custom sorter would probably be the way to go
Dim AllTB As New List(Of TextBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
AllTB.AddRange(Me.Controls.OfType(Of TextBox))
AllTB.Sort(Function(x, y) x.TabIndex.CompareTo(y.TabIndex))
End Sub
To loop through the textboxes use:
For Each tb As TextBox in AllTB
Because the TextBoxes are in the list by reference you can get or set any of the properties in the textboxes and any changes, will be reflected on your form. You could also use sequential names for the textboxes, tag property, etc. and sort by that.

VB.net strange textbox behavior

A customer of mine is having a problem with a text box. When he clicks on the string in the text box the cursor always jumps to the end of the string. This is a standard VB.net 2005 text box with multi-line true. On my development machine it works correctly. I click in the middle of a string and can edit where I click. Can anyone suggest what is wrong?
He has run the program both under terminal server and locally on his lap top and has the same problem.
TIA,
John
Is it possible to observe the user? For example, the user might be pressing shift-tab when they are past the text-box, and refering to that as "clicking" the text box.
You can always force behavior like:
Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.SelectionStart = 0
End Sub

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.