How to change the cursor from One textbox to another - vb.net

I have Two text Boxes named Text1,text2.Once I finished the value input into Text1 I would like to move the cursor automatically to Tex2.I don't like to put a maximum length for Text1,
The valued finished indication I would like in Text1 is Mouse click out of Text1.
Please help me to code it in vb.net

You need to set focus to the textbox2 on textbo1's lost focus event
Private Sub TextBox1_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox1.LostFocus
TextBox2.Focus()
End Sub
or in other ways :
in form load put the code like this :
TextBox1.TabIndex = 0
TextBox2.TabIndex = 1
Reference:
GotFocus :Occurs when the control receives focus (occur when Cursor inside the textbox)
LostFocus :Occurs when the control loses focus(occur when Cursor leaves thetext box)
then simply you can redirect to the textbox2 from textbox1 by clicking the tab key

Related

Filling a listbox from a textbox without using a button or click event in VB

I'm not sure if it is possible, but I would like to know if a listbox can be updated by adding or removing text by typing that text into a textbox without using a button event or some type of click event. I've tried using the text_changed event but it inserts the text as I type so I am unable to type an entire string and then move that into a listbox as a whole string.
What I am trying to do is scan a magnetic ID card through a reader and have it insert the data from that card into a listbox and when I scan the same card again, it will remove the data. This is for an employee logging system.
Thanks.
With the following code, if you type a string in TextBox1 and hit Enter, we check to see if the string already exists in ListBox1. If so, the string is removed from the ListBox, otherwise it is added. Then TextBox1 is cleared.
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = vbCr Then
If ListBox1.Items.Contains(TextBox1.Text) Then
ListBox1.Items.Remove(TextBox1.Text)
Else
ListBox1.Items.Add(TextBox1.Text)
End If
TextBox1.Clear()
e.Handled = True
End If
End Sub

How to remove highlight of combobox when an Item is selected in Vb?

In load page event:
I read some data from DB and then add this data to Combo box then select an item as default and enable of combo box changes to false.
When I load this page, the item witch selected highlights with blue color.
How to remove this highlight?
You can modify its SelectionLength property, which gets or sets how many characters have been selected (highlighted).
Just set it to 0 after you have selected the default item and you should be good to go:
ComboBox1.SelectionLength = 0
EDIT:
In your case this code is executed before the Load event has finished. Due to this the form has not been rendered yet, which is why it is not working for you.
The simple fix is to add this in the form's Shown event too:
Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
ComboBox1.SelectionLength = 0
End Sub
If you don't want the caret to be in the beginning you can also add this line to set it to the end of the text:
ComboBox1.SelectionStart = ComboBox1.Text.Length
The proposed solution does not work in VB.NET 2016
The easiest way to do is to pass focus to another element like a label in the SelectedIndexChanged event

Setting tab stop between two panels

I have two panels, each has several textboxes. What I want is something that seems very simple: user enter a textbox in this panel, then press Tab to jump to its 'linked' textbox in the other panel.
But, vb refuses to jump to other panel unless it finishes through all of the textboxes inside one panel, no matter what TapStop it is.
I tried to catch the tab key from a textbox, and send focus to the linked one without success: pressing Tab doesn't even fire the KeyDown nor KeyPress event.
I tried to set TabStop to the panels first, but that also fail.
So, the problem remain.. how to set tabstop.. or any similar means, to the textboxes between two panel so that when user press Tab in one panel, it will switch to the other one ?
I need the Tab key, not anyother key.
You'll have to set the TabStop property of each control on the form to False and then handle the tabbing yourself, which you can do like this:
Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles TextBox6.PreviewKeyDown,
TextBox5.PreviewKeyDown,
TextBox4.PreviewKeyDown,
TextBox3.PreviewKeyDown,
TextBox2.PreviewKeyDown,
TextBox1.PreviewKeyDown
If e.KeyCode = Keys.Tab Then
Dim controls As Control() = {TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2}
Dim currentControlIndex = Array.IndexOf(controls, ActiveControl)
Dim nextControl = controls(currentControlIndex + 1)
nextControl.Select()
End If
End Sub
Every control you want to be able to Tab from must be in the Handles clause and every control that you want to be able to Tab to or from must be in the array and in the order you want to Tab to them in. You should also repeat the first control at the end of the array again, in order to wrap back to the beginning from the end.
Also note that no control will be selected by default if none of them a Tab stops, in which case you must manually Select the control you want to have focus by default in the form's Shown event handler.
EDIT: Here is a more complete example:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'Manually focus the first control after the form is displayed.
Button1.Select()
End Sub
'Include all the controls that you want to behave as Tab stops in the Handles clause.
'The order is unimportant but ordering them you will Tab to them is not a bad idea.
Private Sub TextBoxes_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown,
TextBox1.PreviewKeyDown,
TextBox4.PreviewKeyDown,
TextBox2.PreviewKeyDown,
TextBox5.PreviewKeyDown,
TextBox3.PreviewKeyDown,
TextBox6.PreviewKeyDown,
Button2.PreviewKeyDown
If e.KeyCode = Keys.Tab Then
'This array must contain all controls to behave as Tab stops in order and the first must be repeated at the end.
Dim controls As Control() = {Button1, TextBox1, TextBox4, TextBox2, TextBox5, TextBox3, TextBox6, Button2, Button1}
'Find the currently active control in the array.
Dim currentControlIndex = Array.IndexOf(controls, ActiveControl)
'Get the next control in the manual tab order.
Dim nextControl = controls(currentControlIndex + 1)
'Focus that next control.
nextControl.Select()
End If
End Sub
That code works for the following form, where TextBox1, TextBox2 and TextBox3 are in Panel1 and TextBox4, TextBox5 and TextBox6 are in Panel2:

VB.net modify a textfile by using listbox and textbox

I want to be able to edit/delete/insert lines of text from a textfile by using listbox and textbox.
I want to display all the contents of the textfile per line in a listbox and when I click a line of text it will then display it in the textbox giving me the option to either edit or delete that line of text.
My insertion of text would be inserted after the last line of text being displayed in the listbox. Is this possible? I just want a starting point and I will continue it from there. Thanks in advance.
Here is an answer with a listview , button 9 is to fill Listview, listview click sends the text to textbox and button 10 saves it back to the listview
This is probably the simplest way to do what you want to achieve.
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
ListView1.HeaderStyle = ColumnHeaderStyle.None
ListView1.HideSelection = False
For i As Integer = 0 To 50
ListView1.Items.Add("Line number" & i.ToString)
Next
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.Click
TextBox8.Text = ListView1.SelectedItems(0).Text
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
ListView1.SelectedItems(0).Text = TextBox8.Text
End Sub
Probably a good starting point for you anyway and expand this code from here on.
Yes. It is possible. I would suggest to use already existing text editors rather than re-inventing the wheel. If you still want to go ahead and create a new one from start, then you can try the following.
Create a window form application in vb.net with ListBox control for showing the lines, a textbox control for entering the file name, a button to browse to the given file, a button on clicking on which the file content should get loaded. Refer file objects for this.
Utilise ContextMenu class in vb.net to allow right click to read selected listbox line and accordingly perform add/delete/edit operation by modifying the selected listitem value.

Transfer value on textbox to another textbox on a different form

I have a value in Form midPoint, on a Textbox. I want that value to transfer to a Label in Form rangeFinder when I hit the OK button.
Can this be done?
Off course it can be done .. Do this in your Button click event
rangefinder.label1.text = midpoint.textbox1.text
Try this:
Private Sub Ok_Click (sender As Object, e As EventArgs) Handles OK.Click
rangeFinder.Label1.Text = midPoint.Textbox1.Text
End Sub
Note that you may replace the form name with Me if you're executing this from any of the forms.
For example, if you are executing this from midPoint the sentence should be:
rangeFinder.Label1.Text = Me.Textbox1.Text
Add this code to your button OnClick() function:
rangeFinder.Label1.Text = TextBox1.Text
Your first form is named midPoint and your second form is named rangeFinder.Suppose the text box on the form midpoint is named midtext and the label on the form rangeFinder is named labelFind.
So you could do this on the click event of ok button:
rangeFinder.labelFind.Text = midpoint.midtext.text