Question Need Combobox help...

Archit

Member
Joined
Jan 12, 2010
Messages
10
Programming Experience
1-3
hi to all,
ok here is the problem.
i wanna create an app which can take information and make notepad file of that data. Users can make any number of notepad files they want.

The prbolem: i want to show all the users files in combo box whic i did it.

but i don't know how to make that file show when user clicks on that name .

here is the code of mine.

''form load code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cur As String
cur = System.Environment.CurrentDirectory.ToString ' gets current dir
Dim str As String
Dim files As String() = Directory.GetFiles(cur, "*.txt")

For Each str In files
CB1.Items.Add(Path.GetFileNameWithoutExtension(str ))
Next

End Sub

'The above code adds names of all the files who have .txt extension in combobox (CB1)

what i want to do is i wanna open the file when user clicks on the particular name in combobox (CB1)

for example : file is test.txt
combobox shows "test.txt"
and when user or i clicks on "test" ,
the test file will get open in notepad.

i know how to open file i.e using process.start.
but dont know how to make that file open..
 
Here is an example.

Note I have added no error trapping. I left that for you and it is something I would strongly recommend you do.
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strCurrFile As String = Dir(System.Environment.CurrentDirectory.ToString & "\*.txt")
        Do Until strCurrFile Is Nothing
            ComboBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(strCurrFile))
            strCurrFile = Dir()
        Loop
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        System.Diagnostics.Process.Start("notepad.exe", System.Environment.CurrentDirectory.ToString & "\" & ComboBox1.Text & ".txt")
End Sub
 
You are welcome and as I said, I would add the error trapping.

If one of the items in your combo does not have a .txt extension, the code will blow up unless it is error trapped.
 
Back
Top