Filling a listbox from a textbox without using a button or click event in VB - vb.net

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

Related

Forcing a combo box to update its text field

I have a typical combobox with drop down items. An item in the dropdown consists of a string with a code then a space, then the description of the code. I'm trying to get the text of the combo box to show just the code after the selection has been made, but there is a race condition in which my the text field is not being updated by the combobox until AFTER I change it. How do I force the combobox to update itself, so I can change the text afterwards.
With code_combo_box
AddHandler .SelectedIndexChanged, AddressOf update_desc
End With
Private Sub update_desc()
If code_combo_box.SelectedIndex >= 0 Then
Dim temp_string As String() = code_combo_box.SelectedItem.split(" ")
code_combo_box.Text = temp_string(0)
End If
End Sub
The combobox gets updated to the selected item after update_desc is called wiping out my change.
I figured out the answer which is to flag the event as handled when I enter the update_desc subroutine. So I needed to catch the events when entering the routine with
update_desc(sender as object, e as system.eventargs)
and then inside the routine simply use
e.handled = true
and the event would not continue to fire after leaving the routine and my changes would remain.

How to create something similar to google style currency converter with VB.NET

Ok I have a bit of a weird question here. I have a program similar to a currency converter (it performs a mathematical function in order to produce a value to go in another textbox). What I want it to be able to do is identify the last textbox that you edited (there are 4) and then update the rest based on what you have inputted, the user then must be able to change a different textbox to change all of them.
If anyone can get me started on how to do it or even some sample code that would be much appreciated, thanks!
Sorry if I'm not making sense, just have a look at the google currency converter and think that with two more editable boxes.
This might be what you want if I understand you correctly.
In the form class, you have a variable called lastTextBoxChangedName which keeps track of which text box was the last to be edited.
Next there is an event handler which will fire when any of the four TextBoxes are changed. This merely updates lastTextBoxChangedName.
When you have finihed editing a textbox, and tab to the next one or click on something that causes a TextBox to lose input focus, the next event handler executes. This looks at lastTextBoxChangedName to see which was the last edited TextBox and you can insert your update code to replace the comments in the Select Case block.
Public Class Form1
Dim lastTextBoxChangedName As String
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged
lastTextBoxChangedName = sender.name
End Sub
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus, TextBox2.LostFocus, TextBox3.LostFocus, TextBox4.LostFocus
updateTextBoxes()
End Sub
Private Sub updateTextBoxes()
Select Case lastTextBoxChangedName
Case "TextBox1"
'do updates appropriate to textbox1 changed
Case "TextBox2"
'do updates appropriate to textbox2 changed
Case "TextBox3"
'do updates appropriate to textbox3 changed
Case "TextBox4"
'do updates appropriate to textbox4 changed
End Select
End Sub
End Class
However, if you already have separate event handlers for each TextBox, don't add that first event handler for TextBox_TextChanged, just add the line ..
lastTextBoxChangedName = sender.name
into each handler.

How to change the cursor from One textbox to another

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

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.

validate a textbox in vb.net

How could i validate a textbox in vb.net, so that it gives error message if i enter anything apart from alphabets
You can check the text string, i.e., textbox1.text, to make sure it has nothing besides alphabet characters in the .Leave event. This will catch an error when the user tabs to the next control, for example. You can do this using a regular expression (import System.Text.RegularExpressions for this example), or you can check the text "manually."
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
If Not Regex.Match(TextBox1.Text, "^[a-z]*$", RegexOptions.IgnoreCase).Success Then
MsgBox("Please enter alpha text only.")
TextBox1.Focus()
End If
End Sub
If you want to stop the user as soon as a non-alpha key is pressed, you can use the TextChanged event instead of the .Leave event.
CustomFieldValidator with a regex.
If it's a standard textbox in a WinForms app you can validate every typed character by handling the KeyPressed event and have the following code in the event handler:
e.Handled = Not Char.IsLetter(e.KeyChar)
The user could still use the mouse to paste something in there though so you might need to handle that as well.
Another option is to handle the Validating event and if the textbox contains any non alphabetic characters you set e.Cancel to true.