I need to display the selecteditems in a label
I am using VB 2005
I set the selection mode to multi-extended
It did work selecting only one item with the following code:
me.xresultslabel.text= me.xlisttextbox.text.selectedItem.tostring
But when I tried to display more than one item using the following code:
me.resultlabel.text= me.xlisttextbox.text.selectedItems, I get the following string on the label:
system.windows.forms.listbox+selectedobjetcollections.
any help will be highly appreciate
Dim collection = Me.xlisttextbox.Text.SelectedItems
Dim builder As New StringBuilder()
For i As Integer = 0 To collection.Count - 1
If i > 0 Then builder.Append(", ")
builder.Append(collection(i))
Next
Me.resultlabel.Text = builder.ToString
Related
I have two listview controls, each having the same column headers. Listview1 contains the master data table and listview2 has a much smaller set of data that I need to add to the Master in Listview1. I can add this new data to the bottom of the main data in Listview1 using the AddRange option, but I need to add it to the top of the data in Listview1 but cannot see how.
If anyone can help I would appreciate it. Thank you in advance.
Thank you for your comments, which I have taken on board.
Let me add some more detail and some code. The data in Listview2 is a monthly csv file input which I read into the Listview2. The data is a set of details with the first field being the date, the most recent date at the top of data. I package this data as an array and attempt to load in into listview1, the master file, with the exact same fields and date order. Here is the code I have used to load the csv and the code to place the packaged data into Listview1. The only problem with this is, it places the data at the end of the current file in Listview1, so it would be out of sequence, so I am trying to place it at the top of the current file in Listview1.
' load the file into listview with this quick routine.
Dim CSVTest As List(Of String) = New List(Of String)
CSVTest = File.ReadAllLines(ImportDirname).ToList
Dim ColNames As List(Of ColumnHeader) = New List(Of ColumnHeader)
Dim ColumnArray() As String = CSVTest(0).Split(",")
For i = 0 To ColumnArray.Count - 1
ColNames.Add(New ColumnHeader)
ColNames(i).Name = ColumnArray(i)
ColNames(i).Text = ColumnArray(i)
Next
ListView2.Columns.AddRange(ColNames.ToArray)
'This adds the rest of the data from the file to the listview.
For I = 1 To CSVTest.Count - 1
Dim col() As String = CSVTest(I).Split(",")
Dim NewLVItem As ListViewItem = New ListViewItem(col(0))
NewLVItem.Name = col(0)
For j = 1 To col.Count - 1
NewLVItem.SubItems.Add(col(j))
Next
ListView2.Items.Add(NewLVItem)
Next (I)
' this adds the range to the bottom of the listview data
' - not what is required. I need it at the top !!!!!
Dim Items(ListView2.Items.Count - 1) As ListViewItem
For i As Integer = 0 To ListView2.Items.Count - 1
Items(i) = CType(ListView2.Items(i).Clone, ListViewItem)
Next
ListView1.Items.AddRange(Items)
Again if anyone can help I would be grateful.
Try Something Like This
With lst1. Items.**Insert**(0, "Sample", 0)
.SubItems.Add(2)
.SubItems.Add(3)
.SubItems.Add(4)
.SubItems.Add(5)
.SubItems.Add(6)
.SubItems.Add(listDownload.Count - 1)
End With
Hope this can help you
I want to know how I can add TextBox text to the start of some of items in my Listbox. For example when I type 'Game 1' in the TextBox it shows that at each timing like Game 1: 00:00:00 and if I type 'Game 2' It shows it like Game 2: 00:00:00 .
I am using VB.Net, any help would be appreicated
If you are using VB.net then let's just say you want to change the last Item of your Listbox then you could have the code below:
Dim itm As String = ListBox1.Items(ListBox1.Items.Count - 1)
ListBox1.Items(ListBox1.Items.Count - 1) = TextBox1.Text + itm
If you want the selected item from the Listbox is where the value of the Textbox will be added then you could use SelectedIndex property instead like:
Dim itm As String = ListBox1.Items(ListBox1.SelectedIndex)
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text + itm
I have an application in VB.Net that displays the results of a math operation to a listbox. For example I have the 1 + 1 = 2.
What I'm trying to do is to have the first 2 numbers (1 and 1) copied to 2 different textboxes when listbox item is selected.
Any help would be greatly appreciated.
Thank you.
My VB.Net is a bit rusty, but something like this should do it:
In the SelectedIndexChanged event put this:
'Make sure that we have a selected item before continuing
If listBox1.SelectedIndex = -1 Then
Return
End If
'Get the text of the selected item
Dim selectedtext As String = listBox1.Items(listBox1.SelectedIndex).ToString()
'Split the item by the + and the = into an array of strings
Dim parts As String() = selectedtext.Split("+"C, "="C)
'If we have more than 2 parts
If parts.Length > 2 Then
'Define a variable for each part
Dim part1 As String = parts(0).Trim()
Dim part2 As String = parts(1).Trim()
'Make text boxes set to part1 and part2. part1 = 1, part2 = 1
End If
I want to search for a small bit of text in my listbox, if it's in the listbox I want to select it, and then covert it to a string.
How do I do this?
Because I cannot find the good command to select something on a specific line!
Thanks
To select a ListBox item, set the ListBox's SelectedIndex property. So, for example:
Dim stringToFind As String = "someString"
For i As Integer = 0 To Me.MyListBox.Items.Count - 1
Dim itemAsString As String = Me.MyListBox.Items(i).ToString()
If itemAsString.Contains(stringToFind) Then
Me.MyLabel.Text = itemAsString
Me.MyListBox.SelectedIndex = i
Exit For 'If you're using a MultiSelect ListBox, you can add to Me.MyListBox.SelectedIndices and remove this line.
End If
Next
I have list of columns in DataTable to be added in list view. I have specified to the listview of columns in the order to appear and Datas as well.
EmailAddress Subject RecievedDate
cd#cd.in Hello 02/06/2011 23:00
This the format to appear.Please anyone can help on this
EDIT:
Code so far:
For i = 0 To objDataTable.Rows.Count drow = objDataTable.Rows(i)
Dim lvwItem As ListViewItem = New lvwItem(drow("SenderEmail"))
'lvwItem.SubItems.Add(drow("SenderEmail"))
lvwItem.SubItems.Add(drow("EmailSubject"))
lvwItem.SubItems.Add(drow("RecievedDate").ToString())
lvwItem.SubItems.Add(drow("AssignedTo").ToString())
LOV.Items.Add(lvwItem)
Next
Your code sample looks almost correct. One error is this line:
Dim lvwItem As ListViewItem = New lvwItem(drow("SenderEmail"))
This should be:
Dim lvwItem As ListViewItem = New ListViewItem(drow("SenderEmail").ToString())
Apart from that you need to make sure your listview is in details view and you actually have the columns you require (otherwise nothing will be shown when in details view):
With listview1
.View = View.Details
.Columns.Add("Email Address")
.Columns.Add("Subject")
.Columns.Add("Recived Date")
'etc
End With
One other small issue is This line:
For i = 0 To objDataTable.Rows.Count
Should be
For i = 0 To objDataTable.Rows.Count - 1