send x value(number) and y value(number) and other letter value (numbers) to thire textboxes - vb.net

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

Related

How to check a text box for a certain character and replace with a different string in the same spot

I am working on a little morse code translation project and I cannot figure out how to detect when a certain key is in a textbox and replace it with the corresponding morse code dots and dashes in the correct spot.
For example if you type in "a b c" then i would like the program to check and put
".- -... -.-."
but it also needs to be dynamic so if you change up the order of your letters it can update the translation.
as of right now i have a key checking system where you can only type in one forward line and if you mess up you have to clear the whole box. thank you!
Here is a basic example of what I was suggesting in my comments above, i.e. using two separate TextBoxes and translating the whole text every time:
Private morseCodeDictionary As New Dictionary(Of Char, String) From {{"a"c, ".-"},
{"b"c, "-..."},
{"c"c, "-.-."}}
Private Sub inputTextBox_TextChanged(sender As Object, e As EventArgs) Handles inputTextBox.TextChanged
outputTextBox.Text = String.Join(" ",
inputTextBox.Text.
ToLower().
Select(Function(ch) morseCodeDictionary(ch)))
End Sub
Here's an implementation that doesn't use LINQ, so may be more understandable:
Private Sub inputTextBox_TextChanged(sender As Object, e As EventArgs) Handles inputTextBox.TextChanged
Dim characterStrings As New List(Of String)
For Each ch As Char In inputTextBox.Text
characterStrings.Add(morseCodeDictionary(ch))
Next
outputTextBox.Text = String.Join(" ", characterStrings)
End Sub

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!

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

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

How do I add string to an array x2 and the get it back?

Essentially what I am trying to do is write a program where you input the person's name and their date of birth, store them to an array (note sure if I have to do two separate arrays) and then by entering their name into another text box and clicking another button I can get the date of birth back.
I know I have to include an if-loop, I know how to declare an array.
I am guessing that I maybe need to use a select case.
Here is the code:
Public Class Form1
Dim Name(5) As String
Dim DOB(5) As String
Dim i As Integer
Private Sub btnNameEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNameEnter.Click
Name(i) = txtInputName.Text
DOB(i) = txtInputDOB.Text
If i = 6 Then
MsgBox("You cannot enter any more names.")
End If
For i = 1 To 5
Name(i) = i
txtInputName.Clear()
txtInputDOB.Clear()
Next i
End Sub
Private Sub btnFindDOB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindDOB.Click
Select Case Name(i)
Case Is Name(1)
MsgBox("Date of birth: " & DOB(1))
Case is Name(2)
MsgBox("Date of birth: " & DOB(2))
Case is Name(3)
MsgBox("Date of birth: " & DOB(3))
Case is Name(4)
MsgBox("Date of birth: " & DOB(4))
Case is Name(5)
MsgBox("Date of birth: " & DOB(5))
End Select
End Sub
End Class
I would show a picture of the program but visual basic keeps deleting it for some reason and doesn't let me re add the buttons.
Here is an image of the error list:
http://gyazo.com/2489a307f4a8e2d9ce65aa2ad79b04f1 )
Public Class Form1
Dim DOB(5) As String
Dim i As Integer
Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
Dim Name(5) As String
Dim Counter As Integer
txtNameInp.Text = Name(i)
txtDOBInput.Text = DOB(i)
Counter = 0
For i = 1 To 5
If Counter = 6 Then
MsgBox("You can only enter 5 names and DOBs")
End If
Name(i) = txtNameInp.Text
DOB(i) = txtDOBInput.Text
Counter = Counter + 1
Next i
End Sub
Private Sub btnFindDOB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindDOB.Click
Select Case Name(i)
Case Name(1)
MsgBox(DOB(1))
Case Name(2)
MsgBox(DOB(2))
Case Name(3)
MsgBox(DOB(3))
Case Name(4)
MsgBox(DOB(4))
Case Name(5)
MsgBox(DOB(5))
End Select
End Sub
End Class
Here's an error: http://gyazo.com/487290c3e523003fe58f82a15fdf6faa - this one occurs when I try and enter the second number
It is hard to be to precise using a picture of the errors instead of comments in the code indicating the location and text of the error, but from what I can see:
Forms already have a Name property and you are trying to redefine it via your array. use something like myNames instead.
You have a module level variable named i and a local one as well. Change the global one to something useful like curPosition. This will track with element you are currently storing to.
If you want to stop adding to the array at 6 you need to do so in code. Add Exit Sub after the MsgBox; you want to do this before you store anything to the arrays, not after. Maybe add a message saying - 'now I will tell you the DOB for a name`.
If you want to be clever (doubtful) have both a 'Guess Name' and Guess DOB button on the form and report the Name for a given DOB. Its basically the same code.
the button enter code will run every time they click so you do not need a loop. This is plain nonsense by the way: Name(i) = i you are overwriting everything with whatever i happens to be. Just add to myNames and myDOB using curPosition as the index and then increment it:
myNames(curPosition) = txtInputName.Text
The relational error is from this: Case Is Name(1)
The error is telling you that it expects a relational operator, which Is is not (Is is used with Objects, which a string is not). The code is also a bit wonky here because you are not controlling the value if i. To compare txtInputName:
Select Case txtInputName.Text
Case = Name(1) ' ie does txtInputName match the value in Name(1)?
...
- Rather than a case statement, you could use a For Loop here for less code.
Lets learn about classes. Rather than arrays which will dissociate related pieces of info from each other a class will keep the data together.
Public Class Person
Public property Name As String
Public Property DOB As DateTime ' I refuse to store a Date as string
Public Sub New(sName as String, dt as DateTime)
Name = sName
DOB = dt
End Sub
End Class
Next, a nifty list to hold some person objects. A list is like an array but smarter:
Private pList As New List(Of Person)
Add a person:
' ToDo: check that the date is valid using TryParse instead
' this exercise is left to the student
Dim p As New Person(txtInputName.Text, Datetime.Parse(txtInputDOB.Text))
plist.Add(p)
Find a person:
' case insensitive search
Dim search As String = txtNameToFind.Text.ToLowerInvariant
For Each p as Person In pList
If P.Name.TolowerInvariant = search Then
txtDOB.Text = p.DOB.ToString
Exit For
End If
Next
Try this:
Public Class Form1
Dim Name, DOB As List(Of String) 'Lists are better in this situation.
Private Sub btnNameEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNameEnter.Click
'You don't need to specify the index in lists.
Name.Add(txtInputName.Text)
DOB.Add(txtInputDOB.Text)
txtInputName.Clear()
txtInputDOB.Clear()
End Sub
Private Sub btnFindDOB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindDOB.Click
'You get the index of the name you are searching for and get DOB for this index.
MsgBox("Date of birth: " & DOB.Item(Name.IndexOf(txtInputName.Text)))
End Sub
End Class

while clicking down arrow cursor need to come other control

i given code like this:
Private Sub txtemployeename_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtemployeename.KeyDown
keyval = e.KeyData
Dim keyData As System.Windows.Forms.Keys = e.KeyData
If keyData = Keys.Down Then
LstEmployee.Visible = True
LstEmployee.Focus()
End If
End Sub
while i am cliking down arrow first time that is not focusing to listbox,second time am clicking down arrow that is focusing..also once cursor come to tha list box,if i clik enter that should be displayed in text box..for that i given code like this..
Private Sub LstEmployee_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstEmployee.Enter
txtemployeename.Text = LstEmployee.SelectedItem
End Sub
but this is not working properly..for loading list box i given code like this:
Private Sub txtemployeename_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtemployeename.KeyPress
Dim s As String = txtemployeename.Text
LstEmployee.Visible = True
loadlistbox(LstEmployee, "select Ename from EmployeeMaster_tbl where Ename LIKE'%" & s & "%' ")
End Sub
You should rely on the KeyUp event rather than on the KeyDown one. Also for the ListBox you just need the SelectedIndexChanged event. Additionally, your code has quite a few errors (wrong query (-> you don't need to call your DB every time to order the items in the ListBox), relies on SelectedIndex rather than on SelectedItem...). Here you have an updated version:
Private Sub txtemployeename_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtemployeename.KeyUp
Dim s As String = txtemployeename.Text
LstEmployee.Visible = True
Dim list = LstEmployee.Items.Cast(Of String)()
Dim query = From item As String In list Where item.Length >= s.Length AndAlso item.ToLower().Substring(0, s.Length) = s.ToLower() Select item
If (query.Count > 0) Then
Dim newItems = New List(Of String)()
For Each result In query
newItems.Add(result)
Next
LstEmployee.Items.Clear()
For Each newItem In newItems
LstEmployee.Items.Add(newItem)
Next
End If
End Sub
Private Sub LstEmployee_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles LstEmployee.SelectedIndexChanged
txtemployeename.Text = LstEmployee.SelectedItem
End Sub
The code above checks for occurrences (i.e., if the whole string in the txtemployeename matches (caps do not matter) the starting substring of, at least, one element in LstEmployee) every time a new character is introduced in txtemployeename. The ListBox is updated with these ocurrences. txtemployeename displays the name of the selected item in LstEmployee.
I hope that this will be enough to help you to build the code required to deliver the exact functionalities you are after.
NOTE: bear in mind that this approach (deleting/adding Items) is incompatible with cases where the ListView is populated with a DataSource. If you rely on a DataSource you would have to update this code accordingly.
NOTE2: the proposed approach deals with the elements in the ListView. You have to introduce these elements at the start from whatever source you are using; this code only updates existing information (items in the ListBox). Also bear in mind that this code is expected to be corrected to match your exact requirements; for example: list has to be associated with the total number of items (the ones retrieved from your datasource at the start), not with the current ones (as displayed in the code; it just represents a simplified version of the problem): every time a new population occurs all the items (other than the target ones) are deleted and thus the ListBox does not represent a reliable source. Example to understand this: at the start, you have "aaaa", "bbbb", "cccc"; if you type "a", all the elements except "aaaa" would be deleted. If you type now "b" and consider the actual elements in the ListBox, no change would occur as far as the only element is "aaaa"; you would have to consider all the original elements (which, as suggested via comment, might be stored at the start in an array/list of strings).