how to read input from barcode scanner and only display required content? - vb.net

Example: barcode content is 1|123456|78910 but i want to retrieve only 123456
Here is my code: Currently my code retrieve all the barcode content.
Private Sub TextVendorID_TextChanged(sender As Object, e As EventArgs) Handles
TextVendorID.TextChanged
Dim s As String = TextVendorID.Text
s = Mid(s, 3, 5)
End Sub

Try this:
This code will do the job, assuming that all barcodes have three parts separated by a "|" character.
The length of the vendor ID (I assume it is the middle string between the "|" character) is not relevant.
Your input string will be split into an array based on the "|" character. strSplit(0) will = 1, strSplit(1) will = 123456 and strSplit(2) will = 78910. The pipes ("|") will be dropped.
Private Sub TextVendorID_TextChanged(sender As Object, e As EventArgs) Handles TextVendorID.TextChanged
Try
Dim strSplit() As String = TextVendorID.Text.Split("|")
If strSplit.Count > 2 Then
TextVendorID.Text = strSplit(1)
End If
Catch ex As Exception
End Try
End Sub

Related

Get all characters to the left of a specific character in further increasing depth

I have the following string:
folder1\folder2\folder3\folder4
I need to cut the string at each slash, so I end up with:
folder
folder1\folder2
folder1\folder2\folder3
folder1\folder2\folder3\folder4
I tried using substring and Split("\"c)(0) and that gives me folder but how I do now get folder1\folder2, etc.?
I took #Andrew Morton's comment and add the For loop to get the results you pictured. The For loop will work for any length path.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s = "folder1\folder2\folder3\folder4"
Dim folders = s.Split("\"c)
Dim sb As New StringBuilder
For i = 0 To folders.Length - 1
sb.AppendLine(String.Join("\", folders.Take(i + 1)))
Next
TextBox1.Text = sb.ToString
End Sub
Result displayed in TextBox1:
folder1
folder1\folder2
folder1\folder2\folder3
folder1\folder2\folder3\folder4

Importing data from text file in VB.NET

So, I am trying to develop a book database. I have created a table with 11 columns which populates a DGV, where only 6 columns are showed. The full data of each book is shown in a lower part of the form, where I have textboxes, which are bounded (BindingSource) to the table, that change as I move in the DGV.
Now, what I want to do is to have the posibility to export/import data from a file.
I have accomplished the exporting part with the following code:
Private Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click
Dim txt As String = String.Empty
For Each row As DataGridViewRow In DbdocsDataGridView.Rows
For Each cell As DataGridViewCell In row.Cells
'Add the Data rows.
txt += CStr(cell.Value & ","c)
Next
'Add new line.
txt += vbCr & vbLf
Next
Dim folderPath As String = "C:\CSV\"
File.WriteAllText(folderPath & "DataGridViewExport.txt", txt)
End Sub
However, I can't manage to import from the txt. What I've tried is this: https://1bestcsharp.blogspot.com/2018/04/vb.net-import-txt-file-text-to-datagridview.html
It works perfectly if you code the table and it populates de DGV without problem. I can't see how should I adapt that code to my need.
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
DbdocsDataGridView.DataSource = table
Dim filas() As String
Dim valores() As String
filas = File.ReadAllLines("C:\CSV\DataGridViewExport.txt")
For i As Integer = 0 To filas.Length - 1 Step +1
valores = filas(i).ToString().Split(",")
Dim row(valores.Length - 1) As String
For j As Integer = 0 To valores.Length - 1 Step +1
row(j) = valores(j).Trim()
Next j
table.Rows.Add(row)
Next i
End Sub
That is what I've tried so far, but I always have an exception arising.
Thanks in advance to anyone who can give me an insight about this.
The DataTable class has built-in methods to load/save data from/to XML called ReadXml and WriteXml. Take a look at example which uses the overload to preserve the schema:
Private ReadOnly dataGridViewExportPath As String = IO.Path.Combine("C:\", "CSV", "DataGridViewExport.txt")
Private Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click
table.WriteXml(path, XmlWriteMode.WriteSchema)
End Sub
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
table.ReadXml(path)
End Sub
While users can manually edit the XML file that is generated from WriteXML, I would certainly not suggest it.
This is code which writes to a text file:
speichern means to save.
Note that I use a # in my example for formatting reasons. When reading in again, you can find the # and then you know that and that line is coming...
And I used Private ReadOnly Deu As New System.Globalization.CultureInfo("de-DE") to format the Date into German format, but you can decide yourself if you want that. 🙂
Note that I'm using a FileDialog that I downloaded from Visual Studio's own NuGet package manager.
Private Sub Button_speichern_Click(sender As Object, e As EventArgs) Handles Button_speichern.Click
speichern()
End Sub
Private Sub speichern()
'-------------------------------------------------------------------------------------------------------------
' User can choose where to save the text file and the program will save it .
'-------------------------------------------------------------------------------------------------------------
Dim Path As String
Using SFD1 As New CommonSaveFileDialog
SFD1.Title = "store data in a text file"
SFD1.Filters.Add(New CommonFileDialogFilter("Textdatei", ".txt"))
SFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
If SFD1.ShowDialog() = CommonFileDialogResult.Ok Then
Path = SFD1.FileName & ".txt"
Else
Return
End If
End Using
Using textfile As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(Path, True, System.Text.Encoding.UTF8)
textfile.WriteLine("Timestamp of this file [dd.mm.yyyy hh:mm:ss]: " & Date.Now.ToString("G", Deu) & NewLine & NewLine) 'supposed to be line break + 2 blank lines :-)
textfile.WriteLine("your text")
textfile.WriteLine("#") 'Marker
textfile.Close()
End Using
End Sub
Private Sub FormMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
speichern()
End Sub
And this is to read from a text file:
'read all Text
Dim RAT() As String = System.IO.File.ReadAllLines(Pfad, System.Text.Encoding.UTF8)
If RAT.Length = 0 Then Return Nothing
For i As Integer = 0 To RAT.Length - 1 Step 1
If RAT(i) = "#" OrElse RAT(i) = "" Then Continue For
'do your work here with (RAT(i))
Next

Detect Unprintable ASCII Characters in String

I'm using Visual Basic 2010 Express. I need to parse a string with unprintable characters in it. I need to detect ASCII 4 (End of Trans).
A scanner dumps data into a TextBox in my app. In a loop, I am using:
If Chr(MyString.Chars(counter)) = 4 Then
MsgBox("Found")
End If
This is not the correct syntax but should convey what I'm trying to do.
After the scanner dumps the data into a textbox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "Some chars coming in from " & Chr(4) & " a scanner"
End Sub
Try something like this:
Dim MyString As String = TextBox1.Text
If MyString.Contains(Chr(4)) Then
MessageBox.Show("Found")
End If
Or even something like this:
Dim MyString As String = TextBox1.Text
Dim counter As Integer = 26
If MyString.Chars(counter) = Chr(4) Then
MessageBox.Show("Found")
End If

I want to replace some characters from a string. I used the Replace command. But this is not working

I am writing some code in VB.Net to subtract one string from another string, but this is not working. in output nothing is changed in the target string. But there is no error message. Please help. Thanks.
If RadioButton1.Checked Then
TextBox1.Text = ""
positive = (TextBoxp1.Text + TextBoxp2.Text + TextBoxp3.Text)
negative = (TextBoxn1.Text + TextBoxn2.Text + TextBoxn3.Text)
findstring = Replace(positive, negative, "")
TextBox1.Text = findstring
End If
The concatenation symbol in vb.net is the ampersand (&). You may get unexpected results it you use the plus sign and the strings contain numbers. Parenthesis are not necessary to evaluate an expression except to establish order of calculation when it conflicts with order of precedence.
You are using the vb.net Strings.Replace method. I would use the .net String.Replace method because it is easier to move between .net languages when you get used to using .net methods instead of vb specific methods.
This method takes the original string in this case negative and looks for the entire positive string. If it finds the entire string it replaces it with the empty string.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim positive = "b" & "cd" & "ef"
Dim negative = "abc" & "def" & "ghi"
TextBox1.Text = negative.Replace(positive, "")
'Result is aghi
End Sub
If you are trying to remove individual letters from a string then you will have to use a loop. Luckily for us a String is an array of Char.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim positive = "ceg"
Dim negative = "abcdefg"
For Each ch As Char In positive
negative = negative.Replace(ch, "")
Next
TextBox1.Text = negative
'Result abdf
End Sub
You are making this way too complicated. If what you want is to remove a substring from within a string use replace like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If rdbtnRemove.Checked Then
txtResultString.Text = Replace(txtLargeString.Text, txtSearchString.Text, "")
End If
End Sub
All you need is two radio buttons, three text boxes and a button. If you enter 1121221114141 in the txtLargeString text box, 2122 in the txtSearchString text box and execute the code, the result is 111114141 which is the result of removing the txtSearchString input from the txtLargeString input.
Or if as #Mary suggested you want to use the more modern version of replace use this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If rdbtnRemove.Checked Then
txtResultString.Text = txtLargeString.Text.Replace(txtSearchString.Text, "")
End If
End Sub

Eliminate duplicate from a Multi-Line string

I have a text-box control called Textbox1 which contains the following items (in a Multi-line string):
22,23
57,58
20,21
51,52
57,58
20,21
21,22
25,26
35,36
41,42
50,51
22,23
23,24
37,38
44,45
45,46
67,68
72,73
78,79
How do I remove 2-digit duplicates? Instead of 20,21, it does not appear twice and once. If the combination of 2 numbers still exists in another line, then this combination appears once. and so on.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lines() As String = TextBox1.Text.Split(New String() {vbCrLf, vbCr, vbLf}, StringSplitOptions.None)
Dim repeatedElement As String = ""
'First have to sort array
System.Array.Sort(lines)
For Each Line As String In lines
If Not (String.Compare(Line, repeatedElement) = 0) Then
TextBox2.Text += Line & vbCrLf
End If
repeatedElement = Line
Next
End Sub
Here textBox1 is for source text and textBox2 is for result.