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