VB.NET Removing first line of a textbox - vb.net

How can i remove the first line of a textbox on a button press?

Try this :
Dim b As String() = Split(TextBox1.Text, vbNewLine)
TextBox1.Text = String.Join(vbNewLine, b, 1, b.Length - 1)
See String.Join for reference .

fairly easy:
Private Sub bttnFirstLine_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bttnFirstLine.Click
txtBox.Text = txtBox.Text.Substring(txtBox.Text.IndexOf(vbCrLf))
end sub
or
Private Sub bttnFirstLine_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bttnFirstLine.Click
dim myString = txtBox.text
txtBox.Text = myString.Substring(myString.IndexOf(vbCrLf))
end sub

Related

when my barcode scanner scan a barcode my textbox1 will get System.Windows.Forms.TextBox, Text: error message. can someone help me

When my barcode scanner scans a barcode, in my textbox, I will get
System.Windows.Forms.TextBox, Text: error message.
I have tried many ways to solve the problem but still no success. Can someone pls help me take a look ? Thanks
Below is my code
Public Class Form1
Dim dataIn As String
'Dim userText As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort.PortName = "COM1"
SerialPort.BaudRate = CInt("9600")
SerialPort.Parity = Parity.None
SerialPort.StopBits = StopBits.One
SerialPort.Handshake = Handshake.None
SerialPort.Open()
SerialPort.ReadTimeout = 200
If SerialPort.IsOpen Then
' TextBox1.Text = ""
End If
End Sub
Private Sub SerialPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
dataIn = SerialPort.ReadExisting
dataIn = TextBox1.Text
TextBox1.Text += SerialPort.ReadExisting().ToString()
SetText(TextBox1.ToString())
End Sub
Delegate Sub SetTextCallback(ByVal text As String)
Private Sub SetText(ByVal text As String)
If Me.TextBox1.InvokeRequired Then
Dim d As SetTextCallback = New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {text})
Else
Me.TextBox1.Text = text.ToString
End If
End Sub
You can't set all those properties and use the methods without an instance of SerialPort.
I am not familiar with serial port code but I suspect that the sender in the DataReceived event is the serial port. So just Cast sender to SerialPort. Call the ReadExisting method which returns a string and assign it to the .Text property of the TextBox.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sp As New SerialPort()
sp.PortName = "COM1"
sp.BaudRate = CInt("9600")
sp.Parity = Parity.None
sp.StopBits = StopBits.One
sp.Handshake = Handshake.None
sp.Open()
sp.ReadTimeout = 200
End Sub
Private Sub SerialPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
TextBox1.Text += DirectCast(sender, SerialPort).ReadExisting
End Sub
Somewhere you need to dispose the port also.
With the Bar Code Scanners I have used, there's been no need to monitor the serial port as the drivers handle that. Normally the scanner simply reads and decoded the bar code and presents it as text to UI as if typed by the user. However, assuming you serial port code is correct, try this (this is a copy of code I've used that also works for setting Text on a Combobox too, so you'll need to delete that. I left it in in case it was helpful with other controls you might be using):
Private Sub SerialPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
Dim ScannedText As String = DirectCast(sender, SerialPort).ReadExisting
SetText(TextBox1, ScannedText)
End Sub
Private Delegate Sub SetTextDelegate(ByVal obj As Object, NewText As String)
Private Sub SetTextSub(ByVal obj As Object, NewText As String)
Select Case obj.GetType.Name
Case "ComboBox"
CType(obj, ComboBox).Text = NewText
Case Else
CType(obj, TextBox).Text = NewText
End Select
End Sub
Private Sub SetText(ByVal obj As Object, NewText As String)
Dim del As SetTextDelegate
del = AddressOf SetTextSub
Dim parArray() As Object = {obj, NewText}
Select Case obj.GetType.Name
Case "ComboBox"
If CType(obj, ComboBox).InvokeRequired Then
CType(obj, ComboBox).Invoke(del, parArray)
Else
SetTextSub(obj, NewText)
End If
Case Else
If CType(obj, TextBox).InvokeRequired Then
CType(obj, TextBox).Invoke(del, parArray)
Else
SetTextSub(obj, NewText)
End If
End Select
End Sub

Call function on button click event

How do I call this vb.net function on the button click event?
Private Sub GridView_UDGReport_DataBound1(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
For rowIndex As Integer = GridView_UDGReport.Rows.Count - 2 To 0 Step -1
Dim gviewRow As GridViewRow = GridView_UDGReport.Rows(rowIndex)
Dim gviewPreviousRow As GridViewRow = GridView_UDGReport.Rows(rowIndex + 1)
For cellCount As Integer = 0 To gviewRow.Cells.Count - 1
If gviewRow.Cells(cellCount).Text = gviewPreviousRow.Cells(cellCount).Text Then
If gviewPreviousRow.Cells(cellCount).RowSpan < 2 Then
gviewRow.Cells(cellCount).RowSpan = 2
Else
gviewRow.Cells(cellCount).RowSpan = gviewPreviousRow.Cells(cellCount).RowSpan + 1
End If
gviewPreviousRow.Cells(cellCount).Visible = False
End If
Next
Next
End Sub
Since you are not using the parameters anyways, you can simply call the method with Nothing as parameter.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GridView_UDGReport_DataBound1(Nothing, Nothing)
End Sub
Append the first line so that the sub handles more than one event, as follows:
Private Sub GridView_UDGReport_DataBound1(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound, Button1.Click
Alternatively, if you need your Click event to run some other code in addition to calling this sub, do this:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'do something
GridView_UDGReport_DataBound1(sender, e)
'do something else
End Sub

how to remove a item in listbox in vb

the string is looks like 11,33,44
i made a split into three strings into 3 textboxes, and then when i do ListBox1.Items.Remove(ListBox1.SelectedItem) it doesn't work.
it says ss.Split(",") Object reference not set to an instance of an object.
here is my code
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim ss As String = ListBox1.SelectedItem
Dim aryTextFile(2) As String
aryTextFile = ss.Split(",")
TextBox1.Text = (aryTextFile(0))
TextBox2.Text = (aryTextFile(1))
TextBox3.Text = (aryTextFile(2))
ss = String.Join(",", aryTextFile)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text + "," + TextBox2.Text + "," + TextBox3.Text)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
When you remove an item from the ListBox by pressing the Button2, the SelectedIndexChanged of the ListBox1 is being called. There, the selected item will be nothing, so to solve this, add the following lines inside the SelectedIndexChanged event before assigning the string variable.
If ListBox1.SelectedItem Is Nothing Then
Exit Sub
End If
Try this:
listbox.selecteditem.remove()
It will remove the selected item in the listbox.

How to populate ListBox from Dictionary Values?

Net
I am attempting to create a function that will allow a user to input text into a RTB and if that text exists in a Dictionary as a Key then a listbox is populated by all the values of the dictionary whose key they are related to , each value populates the listbox in a new line.
the 1st line is highlighted and the user can press the enter button and replace the text in the RTB with the highlighted text .
I'm new to VB so I do not know much .
this is what I have so far.
Public Class Oxnay
Private Sub Oxnay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Tsort()
End Sub
Private TDictionary As Dictionary(Of String, String())
Public Sub Tsort()
TDictionary = New Dictionary(Of String, String())
TDictionary.Add("ape", {"pl", "tz", "xu"})
TDictionary.Add("lor", {"tv", "px"})
End Sub
Private Sub RichtextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
Dim lastword As String = RichTextBox1.Text.Split(" ").Last
If RichTextBox1.ContainsKey(lastword) Then
'display each string of the dictionary array related to lastword in different lines
'highlight first line
'Some[Code]
Else
ListBox1.Text = ""
End If
End Sub
End Class
For the first "lookup" part, try something like:
Private Sub RichtextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
Dim lastword As String = RichTextBox1.Text.Trim.Split(" ").Last
ListBox1.Items.Clear()
If Not IsNothing(TDictionary) AndAlso TDictionary.ContainsKey(lastword) Then
ListBox1.Items.AddRange(TDictionary(lastword))
End If
End Sub
Then to replace the currently selected text with the selection from the ListBox:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If ListBox1.SelectedIndex <> -1 Then
If RichTextBox1.SelectedText <> "" Then
RichTextBox1.SelectedText = ListBox1.SelectedItem.ToString
End If
End If
End Sub

Removing item from listbox

I have a form that has 9 texbox that when I click on a certain button it adds whatever is in it to a listbox, I also have a remove button that removes an item from listbox, is there a way I can remove the item and clear the textbox it came from?
Remove item from ListBox:
ListBox1.Items.Remove(sItemtext)
ListBox1.Items.RemoveAt(indexItem)
Clear the TextBox:
TextBox1.Text = ""
Try this ..
Public Class Form1
Dim oLB As TextBox
Dim aList As New List(Of TextBox)
Sub GetLB(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
oLB = CType(sender, TextBox)
End Sub
Private Sub btnMoveToList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveToList.Click
ListBox1.Items.Add(oLB.Text)
aList.Add(oLB)
End Sub
Private Sub btnRemoveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveItem.Click
Dim n As Integer = ListBox1.SelectedIndex
aList(n).Text = ""
ListBox1.Items.RemoveAt(n)
aList.RemoveAt(n)
End Sub
End Class