yeh tru there is but i changed my idea now i want to....
how do i read 4 textfiles and then put he names of the files in to a combo box so when one of them is chosen it write the text into a list box
So by that you mean you want to read 4 text files and put the data into one combo box? And then once the combobox has an item selected you want that text to be written into a listbox?
So your text file so have something like
VB.NET:
Name\this will be the message of soem sort
Another Name\you must make sure to use some kind of seperator
Right?
It's all the same concept though, read up on how to use a StreamReader. Basically first you need to read the text files in one by one into a combobox, simply do a loop while the reader is open and read each line into an array or arraylist, even a list if you want, some kind of collection. Then after you read all of the files into the array or of the sort, just set the combobox source to that array. i used a dictionary instead, but i converted the keys back to a array. after all the values are read into an array set the combox.datasource to that array and the values will be in the combobox. then make a event handler for the selected index change and once it changes select the corresponding text for that person.
Heres some example code.
''File:Test.txt 'Contains
'Greg/this is the message that will be shown for greg
'Mark/who is the polo?
''File:Test2.txt 'contains
'kreg/this is the messagbe shown for greg
'Mfrk/who is tholo?
Public Class Form1
Inherits System.Windows.Forms.Form
Dim nameMessages As New Dictionary(Of String, String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim reader As New System.IO.StreamReader("C:\Test.txt")
While Not reader.EndOfStream
Dim bufferLineParts() As String = reader.ReadLine.Split("/"c)
If bufferLineParts.Length = 2 Then
nameMessages.Add(bufferLineParts(0), bufferLineParts(1))
End If
End While
reader.Close()
'change the text file . you can just repeat this for each text file. i would recommend using a sub or function to reduce redundant code
reader = New System.IO.StreamReader("C:\Test2.txt")
While Not reader.EndOfStream
Dim bufferLineParts() As String = reader.ReadLine.Split("/"c)
If bufferLineParts.Length = 2 Then
nameMessages.Add(bufferLineParts(0), bufferLineParts(1))
End If
End While
reader.Close()
If nameMessages.Count > 1 Then
cmbNames.DataSource = nameMessages.Keys.ToArray
End If
End Sub
Private Sub cmbNames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbNames.SelectedIndexChanged
Dim selectedKey As String = cmbNames.SelectedItem
If selectedKey <> "" Then
lstMessage.Items.Clear()
lstMessage.Items.Add(nameMessages.Item(selectedKey))
End If
End Sub
#Region "GENERATED"
Sub New()
InitializeComponent()
End Sub
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Private components As System.ComponentModel.IContainer
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.cmbNames = New System.Windows.Forms.ComboBox()
Me.lstMessage = New System.Windows.Forms.ListBox()
Me.SuspendLayout()
'
'cmbNames
'
Me.cmbNames.FormattingEnabled = True
Me.cmbNames.Location = New System.Drawing.Point(12, 12)
Me.cmbNames.Name = "cmbNames"
Me.cmbNames.Size = New System.Drawing.Size(260, 21)
Me.cmbNames.TabIndex = 0
'
'lstMessage
'
Me.lstMessage.FormattingEnabled = True
Me.lstMessage.Location = New System.Drawing.Point(12, 39)
Me.lstMessage.Name = "lstMessage"
Me.lstMessage.Size = New System.Drawing.Size(260, 212)
Me.lstMessage.TabIndex = 1
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.lstMessage)
Me.Controls.Add(Me.cmbNames)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents cmbNames As System.Windows.Forms.ComboBox
Friend WithEvents lstMessage As System.Windows.Forms.ListBox
#End Region
End Class
just look for the selected index, i believe it is 0 for the first item and -1 if no item is selected?. SelectedIndex of the combobox is what you would need.
I did that in the example code i posted above, here maybe this will help.
Public Class Form1
'Create a variable that is global to this class
Dim nameMessages As New Dictionary(Of String, String)
'when the form fires the load event
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'create a new streamreader
'the scope of this is inside this method/subroutine only.
Dim reader As New System.IO.StreamReader("C:\Test.txt")
'while the reader has not finished reading
While Not reader.EndOfStream
'create a string array variable to store the line read in
'this variable is local to this while loop only
'the split is a function of the string and creates an array from one line of string by parsing it with the delimiter specified
Dim bufferLineParts() As String = reader.ReadLine.Split("/"c)
'if that array contains exactly two items
'0 the name (zero-based)
'1 the message (the delimiter is not in either of these, it is removed)
If bufferLineParts.Length = 2 Then
'get the specific elements from the array and add it to the dictionary that was declared local to this class
nameMessages.Add(bufferLineParts(0), bufferLineParts(1))
End If
End While
'close the file
reader.Close()
'change the text file . you can just repeat this for each text file. i would recommend using a sub or function to reduce redundant code
reader = New System.IO.StreamReader("C:\Test2.txt")
While Not reader.EndOfStream
Dim bufferLineParts() As String = reader.ReadLine.Split("/"c)
If bufferLineParts.Length = 2 Then
nameMessages.Add(bufferLineParts(0), bufferLineParts(1))
End If
End While
reader.Close()
'if the dictionary contains items
If nameMessages.Count > 1 Then
'set the combobox datasource to the key values stored in the dictionary
'the key values are all of the names
'u can set the datasource to any array, arraylist, list, works as well
cmbNames.DataSource = nameMessages.Keys.ToArray
End If
End Sub
'when the selected index event is fired, basically when the user clicks on a elemnt in the combobox
Private Sub cmbNames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbNames.SelectedIndexChanged
'create a string variable local to this method and store the selected item into it
'the selected item is what is shown, e.g. the name
Dim selectedKey As String = cmbNames.SelectedItem
'make sure it is not an empty string
If selectedKey <> "" Then
'clear every item that is current in the listbox
lstMessage.Items.Clear()
'add the item
'gets the item from the dictionary based on the key
'the key values should be unique values so it knows which message to get
lstMessage.Items.Add(nameMessages.Item(selectedKey))
End If
End Sub
there i commented my code quickly and briefly, hope that helps a bit.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.