<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Listbox &amp;amp; reading multiple selections.. in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3045332#M60650</link>
    <description>&lt;P&gt;That was one of the first things I tried.&amp;nbsp; Unfortunately it just returns an error..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the kick &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 02 Jun 2011 21:14:28 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2011-06-02T21:14:28Z</dc:date>
    <item>
      <title>Listbox &amp; reading multiple selections..</title>
      <link>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3045166#M60647</link>
      <description>&lt;P&gt;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).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a listbox with multiselect (simple) enabled.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I "see" the text of the values the users selects (if there is more than 1)..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;vb.net if you can.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIA&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jun 2011 19:52:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3045166#M60647</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-06-02T19:52:37Z</dc:date>
    </item>
    <item>
      <title>Re: Listbox &amp; reading multiple selections..</title>
      <link>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3045324#M60649</link>
      <description>&lt;P&gt;Here is a simple way if you just want the text&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        For Each itm In ListBox1.SelectedItems
            MsgBox(itm)
        Next&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jun 2011 21:02:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3045324#M60649</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-06-02T21:02:16Z</dc:date>
    </item>
    <item>
      <title>Re: Listbox &amp; reading multiple selections..</title>
      <link>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3045332#M60650</link>
      <description>&lt;P&gt;That was one of the first things I tried.&amp;nbsp; Unfortunately it just returns an error..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the kick &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jun 2011 21:14:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3045332#M60650</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-06-02T21:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: Listbox &amp; reading multiple selections..</title>
      <link>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3045344#M60651</link>
      <description>&lt;P&gt;These are empty but you can try&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    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" &amp;amp; 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&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Jun 2011 21:32:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3045344#M60651</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-06-02T21:32:50Z</dc:date>
    </item>
    <item>
      <title>Re: Listbox &amp; reading multiple selections..</title>
      <link>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3047000#M60652</link>
      <description>&lt;P&gt;Can you show the code how you've do&amp;nbsp;fill a list box with data?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#800000" face="arial,helvetica,sans-serif"&gt;~'J'~&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jun 2011 11:44:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3047000#M60652</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2011-06-04T11:44:34Z</dc:date>
    </item>
    <item>
      <title>Re: Listbox &amp; reading multiple selections..</title>
      <link>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3047562#M60653</link>
      <description>&lt;P&gt;Hello there&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe this wil help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;C#&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;ListItem Item;&lt;BR /&gt;foreach (int Item in ListBox.Items) {&lt;BR /&gt;&amp;nbsp;if (Item.Selected == true) {&lt;BR /&gt;&amp;nbsp; &amp;nbsp;ItemText.Text += Item.Text + " ";&lt;BR /&gt;&amp;nbsp; &amp;nbsp;ItemValue.Text += Item.Value + " ";&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;Vb.Net&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Dim Item As ListItem&lt;BR /&gt;&amp;nbsp; For Each Item in ListBox.Items&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; If Item.Selected = True Then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ItemText.Text &amp;amp;= Item.Text &amp;amp; " "&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ItemValue.Text &amp;amp;= Item.Value &amp;amp; " "&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End If&lt;BR /&gt;&amp;nbsp; Next&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Irvin&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jun 2011 07:15:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3047562#M60653</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2011-06-06T07:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: Listbox &amp; reading multiple selections..</title>
      <link>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3048616#M60654</link>
      <description>&lt;P&gt;The question is about listbox when its&amp;nbsp;items was pulled from dataset&lt;/P&gt;&lt;P&gt;if I understand this right&lt;/P&gt;&lt;P&gt;Perhaps, like this&lt;/P&gt;&lt;PRE&gt;-------------- 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
			'&amp;lt;-- 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&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jun 2011 21:08:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/listbox-amp-reading-multiple-selections/m-p/3048616#M60654</guid>
      <dc:creator>Hallex</dc:creator>
      <dc:date>2011-06-06T21:08:25Z</dc:date>
    </item>
  </channel>
</rss>

