Listbox problems

Wizard83

Member
Joined
Mar 20, 2005
Messages
13
Programming Experience
Beginner
Hi there, i am having a problem on a simple part of coding again
this time its regarding listbox

i would like to use a list box to populate a set of records and from the records users is capable of clicking on it..once clicked it will open a rich textbox which the location of the file is stored in the database

I have solve the populating data records in the listbox however i am still stuck on how to call the rich textbox out

This is my codes
<There are 2 records in my database one is the TutoName and the second is the FileLocation(to store the location of the rtf file)>


Dim ds As DataSet = New DataSet
Me.OleDbDataAdapter1.Fill(ds, "Sickness")
Dim dv As DataView = ds.Tables("Sickness").DefaultView
ListBox1.DataSource = dv
ListBox1.DisplayMember = "TutoName"
 
add in :
ListBox1..valuesouce = "filelocation"

Then in the on index change event of the listbox load the file from listbox1.selectedvalue (the dir/file)

TPM
 
VB.NET:
Sub listbox1_selectedindexchange(Byval sender as object, byval e as eventargs) handles listbox1.selectedindexchanged
Dim Reader as new io.streamreader (listbox1.selectedvalue)
yourtextbox.text = reader.readtoend
reader.close
end sub
 
Hi there sorry to disturb you again anyway there is some probems with the code. As there is no valuesource in the listbox
and also the code generated an error at the reader which is underline below


<My code>
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As DataSet = New DataSet
Me.OleDbDataAdapter1.Fill(ds, "Sickness")
Dim dv As DataView = ds.Tables("Sickness").DefaultView
ListBox1.DataSource = dv
ListBox1.DisplayMember = "TutoName"
ListBox1.ValueMember = "FileLocation"
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

Dim Reader As New IO.StreamReader(ListBox1.SelectedValue)
lblFileName1.Text = Reader.ReadToEnd
Reader.Close()

End Sub


Thankz in advance


 
"filelocation" is the name of the column in you database that you stored the .rtf filelocation in. You'll probably need to change it to whatever you've named the column. Also don't forget to select the column into you datatable.
 
Yup the filelocation is the name of my column in my database

Hmm i think i messed up my codes care to show me
how to solve my problem from the start

sorry i am a slow learner :eek:
 
I found another method to do it but i need to declare 2 extra textbox 4 it to store temp data to it. Is there any better ways to do it ?

My codes

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim odCmd As OleDbCommand
Dim odR As OleDbDataReader
Dim strSQL As String
strSQL = "SELECT TutoName, FileLocation FROM Tutorial"
odCmd =
New OleDbCommand(strSQL, cnn)
cnn.Open()
odR = odCmd.ExecuteReader(CommandBehavior.CloseConnection)
Do While odR.Read
ListBox1.Items.Add(odR.Item("TutoName"))
Loop
cnn.Close()
End Sub

Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim odCmd As OleDbCommand
Dim odR As OleDbDataReader
Dim strSQL As String
TextBox2.Text = ListBox1.SelectedItem
strSQL = "SELECT FileLocation FROM Tutorial WHERE TutoName='" & TextBox2.Text & "'"
odCmd =
New OleDbCommand(strSQL, cnn)
cnn.Open()
odR = odCmd.ExecuteReader(CommandBehavior.CloseConnection)
Do While odR.Read
Me.lblFileName1.Text = odR.GetString(0)
RichTextBox1.LoadFile(lblFileName1.Text)
Loop
cnn.Close()
End Sub
 
Back
Top