Listbox & reading multiple selections..

Listbox & reading multiple selections..

Anonymous
Not applicable
1,729 Views
6 Replies
Message 1 of 7

Listbox & reading multiple selections..

Anonymous
Not applicable

I'm sure the answer to this has been staring me in the face for the last few hours, but I haven't been able to see the solution (and searches haven't given me anything).

 

I have a listbox with multiselect (simple) enabled. 

 

How can I "see" the text of the values the users selects (if there is more than 1)..

 

vb.net if you can.

 

TIA

0 Likes
1,730 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

Here is a simple way if you just want the text

 

        For Each itm In ListBox1.SelectedItems
            MsgBox(itm)
        Next

 

0 Likes
Message 3 of 7

Anonymous
Not applicable

That was one of the first things I tried.  Unfortunately it just returns an error..

 

However I just figured out that because I am using a dataset in the listbox, I have to dig a few more layers into it to get what I need.

 

Thank you for the kick 🙂

0 Likes
Message 4 of 7

Anonymous
Not applicable

These are empty but you can try

 

 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 10
            Dim ds As New DataSet
            ds.DataSetName = "Dataset" & i
            ListBox1.Items.Add(ds)
        Next
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        For Each itm In ListBox1.SelectedItems

            If TypeOf itm Is DataSet Then
                Dim ds As DataSet = itm
                MsgBox(ds.DataSetName)
            End If

        Next

    End Sub

 

0 Likes
Message 5 of 7

Hallex
Advisor
Advisor

Can you show the code how you've do fill a list box with data?

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 6 of 7

Anonymous
Not applicable

Hello there

 

Maybe this wil help

 

C#

 

ListItem Item;
foreach (int Item in ListBox.Items) {
 if (Item.Selected == true) {
   ItemText.Text += Item.Text + " ";
   ItemValue.Text += Item.Value + " ";
 }
}

 

Vb.Net

 

 Dim Item As ListItem
  For Each Item in ListBox.Items
    If Item.Selected = True Then
      ItemText.Text &= Item.Text & " "
      ItemValue.Text &= Item.Value & " "
    End If
  Next

 

Kind regards,

 

Irvin
 

0 Likes
Message 7 of 7

Hallex
Advisor
Advisor

The question is about listbox when its items was pulled from dataset

if I understand this right

Perhaps, like this

-------------- in form declaration --------
Private System.Data.DataTable dtable;
.............


--------------on form Load event-----------

this.listBox1.Items.Clear();
this.listBox1.SelectionMode = SelectionMode.MultiSimple;
System.Data.DataSet dset = GetDataSetFromReader("C:\\Test\\blabla.txt", "Table");
dtable = dset.Tables["Table"];

this.listBox1.DisplayMember = dtable.Columns[0].ColumnName;
this.listBox1.ValueMember = dtable.Columns[0].ColumnName;
this.listBox1.DataSource = dtable;



--------------on button click event-----------
Private Sub Button1_Click(sender As Object, e As EventArgs)
	'just for debug only:
	'foreach  (DataRowView a in this.listBox1.SelectedItems)
	'     MessageBox.Show("Selected Listbox item: " + a.DataView.ToString());
	' fill list of string to store selected items
	Dim texts As New List(Of String)()
	Dim indices As ListBox.SelectedIndexCollection = Me.listBox1.SelectedIndices
	For Each i As Integer In indices
			'<-- get texts from 1st datatable column
		texts.Add(Me.dtable.Rows(i).ItemArray(0).ToString())
	Next
         'you can clear textboxes here before
	For Each ctl As Control In Me.Controls
		If TypeOf ctl Is TextBox Then
			Dim tb As TextBox = DirectCast(ctl, TextBox)
			Try
				' say you have 4 textboxes:
				If tb.Name = "textBox1" Then
					tb.Text = texts(0)
				End If
				If tb.Name = "textBox2" Then
					tb.Text = texts(1)
				End If
				If tb.Name = "textBox3" Then
					tb.Text = texts(2)
				End If
				If tb.Name = "textBox4" Then
					tb.Text = texts(3)
				End If
			Catch
				'empty catch block to bypass  if less then 4 items selected
			End Try
		End If
	Next

End Sub

----------------------create dataset from text file---------------------

Private Function GetDataSetFromReader(filename As String, usertable As String) As System.Data.DataSet
	Dim ds As New System.Data.DataSet()
	Dim filedir As String = (New System.IO.FileInfo(filename)).DirectoryName
	Dim srtconn As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source={0};" + "Extended Properties=""text;"";", filedir)
	Using conn As New OleDbConnection(srtconn)
		Try
			conn.Open()
			Dim da As New OleDbDataAdapter()

			Dim db As New OleDbCommandBuilder(da)
			Dim cmd As New OleDbCommand(String.Format("SELECT * FROM {0}", filename))
			cmd.Connection = conn
			da.SelectCommand = cmd
			Dim dataReader As OleDbDataReader = da.SelectCommand.ExecuteReader()
			Dim dt As New System.Data.DataTable()
			dt.TableName = usertable
			dt.Load(dataReader)
			ds.Tables.Add(dt)
			dataReader.Close()

			ds.AcceptChanges()
		Catch oex As OleDbException
			MessageBox.Show(oex.Message + vbLf + oex.StackTrace)
		Finally
			conn.Close()
		End Try
	End Using
	Return ds
End Function

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes