vb.net string lookup in listbox with output as different part of the string - vb.net

Last Name Finder
what I'm trying to do is create a list of all peoples surnames where the 1st name = #name
although this isn't the exact purpose its easier to try and explain the actual content. i'm aware it may be easier to use a data source, but I want to try and avoid that where possible.
Form_Load
ListBox1.Items.Add("Dean Smith")
ListBox1.Items.Add("John Jones")
ListBox1.Items.Add("David Johnson")
ListBox1.Items.Add("Samantha Thompson")
ListBox1.Items.Add("Claire Frost")
ListBox1.Items.Add("John Brown")
and then some sort of string manipulation to do the following on button_click
if textbox1.text contains "John" then
listbox2.items.add(Jones)
listbox2.items.add(Brown)
else messagebox.show("No matches found")
end if
Thanks for any input.

What I've understood is that you are willing to use your textbox as a filter to the listbox. And this can be achieved by running the query to your db on every TextChanged().
So to give you some guidelines, you can proceed like this;
private Names As List(Of String)
Private Sub Form2_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Names = New List(Of String)
With Names
.Add("Dean Smith")
.Add("John Jones")
.Add("John Brown")
End With
For Each Name As String In Names
ListBox1.Items.Add(Name)
Next
End sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ListBox1.Items.Clear()
For Each s As String In Names
If s.Substring(0, TextBox1.Text.Length).ToLower = TextBox1.Text.ToLower Then
ListBox1.Items.Add(s)
End If
Next
End Sub
I hope this solution might help you achieve what you wanted. You can also apply direct filter to the List by using a delegate function, which will avoid you from looping again on TextChange().

Related

Barcode scanning to a listbox checking for duplicates

Good day!
I want to add some strings from a barcode scanner, captured in a text box, to a list box, and, before adding it, to check if the specific string hasn't been already added. So I have a text box called txtWO which captures what the reader scans and a list box called lstScanBOM to which I add the text box string if the item is not already added. The problem is, that whatever I do, only after the specific string is added twice the checking for duplicate entry starts to work. In other words I scan the same string twice, it added it, and then when I scan the third time only it throws the message with the error saying it is a duplicate. I don't understand why is doing this. The code is below:
Private Sub frmValidareFIP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If txtWO.Focused = False Then
txtWO.Select()
End If
End Sub
Private Sub AddUnique(StringToAdd As String)
If lstScanBom.Items.Contains(StringToAdd) = True Then
MsgBox("Articol duplicat!", vbOKOnly)
Else
'it does not exist, so add it..
lstScanBom.Items.Add(StringToAdd)
End If
End Sub
Private Sub txtWO_KeyDown(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWO.KeyDown
If e.KeyCode = Keys.Enter Then
Dim barcode As String
barcode = txtWO.Text
AddUnique(barcode)
txtWO.Clear()
txtWO.Focus()
End If
End Sub
IMO Try listing the data outside of the ListBox. I can't see why it isn't working, maybe we need a third pair of eyes to see it!?
Try adding a list (of string) as a Private within the form, populate this as your user scans, and check the duplicate there..
This is definately not the best solution, but I'm sure it will help!
Private List_Barcodes As List(Of String)
Private Sub frmValidareFIP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
List_Barcodes = New List(Of String)
'You can also populate this list on load, if you have a stored cahce of previous scanned barcodes?
'List_Barcodes.Add("0123456")
'List_Barcodes.Add("4567890")
'...etc
If txtWO.Focused = False Then
txtWO.Select()
End If
End Sub
Private Sub AddUnique(StringToAdd As String)
If List_Barcodes.Contains(StringToAdd) Then
MsgBox("Articol duplicat!", vbOKOnly)
Else
'Place into dynamic list
List_Barcodes.Add(StringToAdd)
'and Place into your listbox
lstScanBom.Items.Add(StringToAdd)
End If
End Sub
Private Sub txtWO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWO.KeyDown
If e.KeyCode = Keys.Enter Then
Dim barcode As String
barcode = txtWO.Text
AddUnique(barcode)
txtWO.Clear()
txtWO.Focus()
End If
End Sub
Your barcode reader is returning <carriage return><line feed> as enter. Your code catches the enter key (carriage return = 13), but leaves the line feed (10) character. So the next time you scan something it will start with a line feed. The two strings in your example are different because the first is "58335001" and the second is "<line feed>58335001". The third one is "<line feed>58335001", which is a duplicate of the second.
One way to fix this is to trim your string.
Private Sub txtWO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWO.KeyDown
If e.KeyCode = Keys.Enter Then
Dim barcode As String
'Add the .Trim() to remove the leading <line feed> character
barcode = txtWO.Text.Trim()
AddUnique(barcode)
txtWO.Clear()
txtWO.Focus()
End If
End Sub
The most simple decision is ONLY to make your textBox control txtWO NOT multiline
And that's enough! Your code will work correctly!

How to replace a word in a textbox with another word

I'm writing an article rewriter in VB.NET and I am having a problem in replacing some words with another word.
is there a way i can replace the words directly while the user is typing.
while texting it , i typed "what is love, we always look at it"
and it displayed what is love we frequently look at it
instead of
what is affection we frequently see it
Here is my code:
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
If RichTextBox1.Text.Contains("always") Then
RichTextBox2.Text = RichTextBox1.Text.Replace("always", "frequently")
End If
If RichTextBox1.Text.Contains("love") Then
RichTextBox2.Text = RichTextBox1.Text.Replace("love", "affection")
End If
If RichTextBox1.Text.Contains("look") Then
RichTextBox2.Text = RichTextBox1.Text.Replace("look", "see")
End If 'RichTextBox2.Text = RichTextBox1.Text
End Sub
If I understand the problem correctly, you want to change the text as it's being typed in. You don't want to use the text changed event as it won't occur immediately on typing. Use the keyup event instead.
Private Sub RichTextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp
Dim wordToFind As String = "findword"
Dim replaceWord As String = "replaceword"
Richtextbox2.rtf = RichTextBox1.replace(wordToFind, replaceWord)
End If
End Sub

send x value(number) and y value(number) and other letter value (numbers) to thire textboxes

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim reader As New IO.StreamReader(OpenFileDialog1.FileName)
TextBox1.Text = reader.ReadToEnd
End Sub
End Class
the program will be like this when press open the open file diloge well open the file from the computer and the well show in textbox1 . what I want to do is to send xvalue(the number after(x)) is send to textbox(x) and send yvalue(the number after(y)) is send to textbox(y) and each letter value well send value (number) well send like this and this code is for open the file I think the array must used in this case but I dont know how use the arry
If I've understood you correctly, I think the following will work:
Dim input As String="N14G73X315.2Y83.7I40.0J6.4A0.0H3K75T11F5M0C0.0"
Dim matches = System.Text.RegularExpressions.Regex.Matches(input, "\w[0-9.]+")
Dim parts = Matches.Cast(Of System.Text.RegularExpressions.Match).Select(Function(p) New With {.Key=p.Value.Substring(0,1), .Value=p.Value.Substring(1)}).ToDictionary(Function(p) p.Key, Function(p) p.Value)
txtX.text = parts("X")
txtY.text = parts("Y")
etc
We use a regular expression to split the string into groups of a single letter followed by a number including an optional decimal point. We then use Linq to turn this into a dictionary of numbers (well string representations of numbers) keyed on their corresponding letter from the input. You can then pull these from the dictionary to set each text field
You could also use capture groups on the regular expression instead of splitting the output in the linq statement

Validate only newly typed text

I'm making simple application. There is a textbox and a ListBox. When user type something in the textbox, that text add to the ListBox split by space after some validation process. I done it. Here is my code.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'split by space
Dim arrText() As String = Split(TextBox1.Text, " ")
ListBox1.Items.Clear()
'ValidateText is a function
For i = 0 To UBound(arrText)
ListBox1.Items.Add(ValidateText(arrText(i)))
Next i
End Sub
But I want to upgrade it because the validation process take more time. When user type something in the textbox need to do the same process but for only newly typed text. (From the cursor position forward to the end of the text) already validated text doesn’t need to validate again. I think someone can help.
Note: user can be also copy & paste words in the textbox
Thank in advance
I have found a solution thanks to lapheal who member in msdn forum
Private validatedDic As New Dictionary(Of String, String) 'or Dictionary(Of String, Object)?
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'split by space
Dim arrText() As String = Split(TextBox3.Text, " ")
ListBox1.Items.Clear()
'ValidateText is a function
For i = 0 To UBound(arrText)
Dim text As String = String.Empty
If Not validatedDic.TryGetValue(arrText(i), text) Then
text = ValidateText(arrText(i))
validatedDic(arrText(i)) = text
End If
ListBox1.Items.Add(text)
Next i
End Sub

visual basic :Help removing items from listbox

I have to make a listbox with a few(8) names in it & double clicking on a name in the listbox will removed the name from it.
I have already add the names into the form using the listbox.items.add method & would display the names in it.
Then I enter the coding for 8 names in double_click procedure(listbox) using the "listbox.items.remove" method.
However, when i try double clicking on a name in the listbox, it would remove all the names instead.
What coding do i need? help appreciated!
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Clear()
ListBox1.Items.Add("1")
ListBox1.Items.Add("2")
ListBox1.Items.Add("3")
ListBox1.Items.Add("4")
ListBox1.Items.Add("5")
ListBox1.Items.Add("6")
ListBox1.Items.Add("7")
ListBox1.Items.Add("8")
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim i As Integer = ListBox1.SelectedIndex
If i >= 0 And i < ListBox1.Items.Count Then
ListBox1.Items.RemoveAt(i)
End If
End Sub
End Class
if you are looking at dynamic deletion of items, i think you should check out Jquery,Ajax,DOM
there are a couple of nice tutorials that would help you with that. i just came across this one and found it interesting
http://www.satya-weblog.com/2010/02/add-input-fields-dynamically-to-form-using-javascript.html