Listbox selected item to textbox in Vb.net - 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

Related

VB.Net - Entry in datagridview keeps duplicating on event

Here is my code, everytime I click the Button1, instead of refreshing the Datagridview, it only adds another entry that is a duplicate of the previous one. I know I'm missing something in my code that will clear the data in Datagrid before loading it again. Please help..
Private Function LoadData_UnitProcess()
Dim UP_SQL As String = "SELECT LotNum FROM Transactions WHERE StatusID=3 ORDER BY Process_EntryDate DESC"
Dim UP_Ad As OleDbDataAdapter = New OleDbDataAdapter(UP_SQL, strCon)
UP_Ad.Fill(UP_Ds, "Transactions")
UnitOnProcess_DG.DataSource = UP_Ds.Tables(0)
With UnitOnProcess_DG
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Lot #"
.Columns(0).Width = "363"
.AllowUserToAddRows = False
End With
LoadData_UnitProcess = ""
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
LoadData_UnitProcess()
End Sub
Private Sub Displayer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData_UnitProcess()
End Sub
You dataset UP_Ds is probably a property or field. You need to reset the dataset before filling it again, otherwise the content is appended to the previous.
UP_Ds.Reset()
UP_Ad.Fill(UP_Ds, "Transactions")
UnitOnProcess_DG.DataSource = UP_Ds.Tables(0)

Why i can't get select index of listbox in VB

When I add items to list box by collection items in properties, I can get selected index. But when I add items by coding, I can't get selected index. Data (added by coding) is from data table parameter.
Dim test As Integer = ListBox1.SelectedIndex
That I used this code to get select index.
ListBox1.Items.Add(dt.Rows(0)(0).ToString & "_" & dt.Rows(0)(1).ToString & "g")
This code is adding items.
you can try this if this is what you mean
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Label1.Text = ListBox1.SelectedItem
End Sub
returns the text
and this...
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Label1.Text = ListBox1.GetItemText(ListBox1.SelectedIndex)
End Sub
returns the index

HOw to set default value in text box VB+

I am working On VB and Arduino At same time. My main intention is to get data serially displayed on Textbox of VB
Now i wanted to how can assign string or Serial data out put to textbox.
I have google it but the sytax is not working
I created simple textbox AND assigned variable abcd , Now i wanted to display Abcd on text box.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = "ABCD"
End Sub
If i wanted to read serial data display it on textbox
Private Sub Current_Read_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open()
Dim str As String
str = SerialPort1.ReadExisting
Current_Read.Text = str
SerialPort1.Close()
End Sub
So why it not working
Your code doesn't result as you expected because the code which you are using will executed only when you type any thing in the textbox, while using this code you can't able to type anything in the textbx when you type any thing in it will result in "ABCD". to avoid this
Move this code TextBox1.Text = "ABCD" from TextBox1_TextChanged event to form_Load event. that is
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "ABCD"
End Sub
Try this for some reason. Add a button and double click the button so you can write your code to the event Button_Click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
Dim str As String
str = SerialPort1.ReadExisting
Current_Read.Text = str
SerialPort1.Close()
End Sub
run the application. and click the button

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.

visual basic List.box question

i have 1 text box and 1 listbox in my VB form.
i want to check duplicate item,compare with textbox.text1 and listbox.list item.
and if textbox.text1 value is '3333' and listbox.list multiple value is '1111' '2222' '3333' '4444'
so how to implement such like duplicate check routine?
so if duplicate detect compare with current text1 value and compare with one of listbox's
value is if detect,want to popup messagebox
thanks in advance
Assuming you are inserting strings into your ListBox you can do this:
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim x As String
For Each x In ListBox1.Items
If (x = TextBox1.Text) Then
MessageBox.Show("Error")
Return
End If
Next
ListBox1.Items.Add(TextBox1.Text)
End Sub
If it's another type of object that has a property called Value then you need a small change:
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim x As Foo
For Each x In ListBox1.Items
If (x.Value = TextBox1.Text) Then
MessageBox.Show("Error")
Return
End If
Next
ListBox1.Items.Add(TextBox1.Text)
End Sub
Assuming that the ListBox contains strings, you can use the Contains method of the Items collection to check for matches. Example (make a form with a ListBox called '_theListBox', a TextBox called '_theTextBox' and a Label called '_theLabel'):
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_theListBox.Items.AddRange(New String() {"aaaa", "bbbb", "cccc", "dddd"})
End Sub
Private Sub _theTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _theTextBox.TextChanged
If ListBoxContainsItem(_theListBox, _theTextBox.Text) Then
_theLabel.Text = "It's a match"
Else
_theLabel.Text = ""
End If
End Sub
Private Function ListBoxContainsItem(ByVal lb As ListBox, ByVal text As String) As Boolean
' check if the string is present in the list '
Return lb.Items.Contains(text)
End Function