how to remove a item in listbox in vb - vb.net

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.

Related

How to add text after every textbox line in VB.NET

I've searched everywhere, I can't find a way.
I want to find a way to add text after every textbox line but I can't find a way to do it.
I have a textbox1 with:
example1
example2
example3
And so on...
and another textbox2 with #gmail.com
I want the textbox2 to be added to the end of every line in textbox1 like:
example1#gmail.com
example2#gmail.com
example3#gmail.com
And so on...
Any way to do it? Thanks in advance.
This solution is concise, and removes empty lines.
Private Function appendTextToOtherTextLines(textToAppend As String, otherText As String) As String
Return String.Join(Environment.NewLine, otherText.
Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries).
Select(Function(s) s & textToAppend))
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox3.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Here's your example working
And if you had an empty line, it is removed in the resulting string
Of course, you could overwrite the original textbox instead, but careful not to click the button twice!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Other option is an event handler which will make this happen automatically when pressing enter at the end of a new line. This is only useful if you are actively entering the lines manually.
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 2) & TextBox2.Text & Environment.NewLine
TextBox1.SelectionStart = TextBox1.Text.Length
End If
End Sub
(this option requires some discipline when pressing enter)
This is full working code.Enjoy it!!!!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Me.TextBox1.Lines.Length = 0 Then
MsgBox("Please enter some data in textbox1")
Exit Sub
End If
'---------------------------
Dim b(Me.TextBox1.Lines.Length - 1) As String
Dim i As Integer = 0
For Each a As String In Me.TextBox1.Lines
b(i) = a + Me.TextBox2.Text
i = i + 1
Next
'-----------------
Me.TextBox1.Clear()
Me.TextBox1.Lines = b
End Sub

vb.net Find and REMOVE a line in a textbox

I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class

Listbox selected item to textbox in Vb.net

Sometimes simple code will make us big confusion. I researched this questions in internet from past few hours.
I have a listbox, data will load from Access table. When i selected an item in listbox, i wanted to show that item in a textbox.
i have tried some code as below, nothing worked for me:
Private Sub listBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
For i As Integer = 0 To listBox1.SelectedItems.Count - 1
textBox1.Text &= DirectCast(listBox1.SelectedItems(i), DataRowView)(1).ToString & vbCrLf
Next
End Sub
Private Sub listBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
textBox1.Text = listBox1.SelectedItem.Value
End Sub
Here is the code that will give values to listbox from access table
Dim dataAdapter As New OleDbDataAdapter("select * from eval", connection)
Dim ds As DataSet = New DataSet
dataAdapter.Fill(ds, "eval")
listBox1.DataSource = ds
listBox1.DisplayMember = "eval"
listBox1.ValueMember = "eval.eval"
textbox1.Text = listBox1.GetItemText(listBox1.SelectedItem);
Here is a simple example of what you can do. Lets say you have a list box with 4 items: VB.Net,C#,Java and Python. First, make the listbox SelectionMode = MultiSimple. If I understood you correctly, you want all the items that have been selected in the listbox to be transfered to a text box. You can do that by wrtining this code:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox1.Clear()
For Each Item As Object In ListBox1.SelectedItems
TextBox1.AppendText(Item.ToString + Environment.NewLine)
Next
End Sub
Here are the results :
(Sorry for the bad quality)
The listBox1.SelectedItem will give you back a System.Data.Datarow, because that is what you put in there. If you want to display the same text in the textbox that is displayed for the listbox, you can simply use this:
Private Sub listBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
textBox1.Text = listBox1.text
End Sub
If you want a value from another field (you do not show your fields so lets pretend there is one called "FirstName") you could do this:
Private Sub listBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
Dim DR as datarow = listBox1.SelectedItem
textBox1.Text = DR("FirstName")
End Sub

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

VB.NET Removing first line of a textbox

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