Hey guys...
i am trying to view contents of a text file to a textbox via aid of a button (ViewBtn).
i have a treeview(TreeView1) which shows the root folder and displays content in the listview.

my query is how do i select a file from listview (ListView1) and display it in a textbox (TextBox2).

Treeview and List view worki fine.

any help please

Member Avatar for iamthwee

You need to create an event handler so that when you click on a node(either parent or child) it recognises which one as a string.

Then you can open the text file using that string(path)

It's probably something like treeview1.selecteditem() . Google it.

Ummmm. i have had a look at the net and thus far my code is like:


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each File As ListViewItem In ListView1.Items
Dim FilePath As String = File.SubItems(0).Text & "\" & File.Text
If File.Selected = True Then
MessageBox.Show("You Are About To Open " & FilePath & ". Proceed?", "Open File", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
If DialogResult.Yes Then
' TextBox2.Text = ListView1.SelectedItems(FilePath, FileMode.Open)
'Process.Start(FilePath, FileMode.Append)
'IO.File.Open(FilePath, FileMode.Open)

' Dim SR As StreamReader(ListView1.SelectedItems)

' Do Until SR.EndOfStream
' TextBox2.AppendText.(SR.ReadLine)
' Loop
' SR.Close()

'

ElseIf DialogResult.No Then
MessageBox.Show("Operation Cancelled")

End If


End If
Next
End Sub

I have an idea of how to achive it but still not sure on how to geth the answer.

Unsure where to get the path from.
Did check treeview but not very good at VB to accomplish.. any code snippet will be helpful.

The Textfile i am trying to open jus has TEESSSSSST written inside. trying to get that displayed in textBox2 in aid of a button.

See All this is good control in listview :)
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, OpenInEditorToolStripMenuItem.Click

If ListView1.SelectedItems.Count <= 0 Then
MessageBox.Show(Me, "Open file", "File", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
Dim lvi As ListViewItem = ListView1.SelectedItems(0)
If ((Path.GetExtension(lvi.Text)).ToLower() <> ".rgu") Then
MessageBox.Show(Me, "RGU file only", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return
End If

Dim FileForm As Form = New Form()
FileForm.Text = lvi.Text
Dim filename As String = TreeView1.SelectedNode.FullPath + "\" + ListView1.SelectedItems(0).Text
Dim sr As StreamReader = New StreamReader(filename)
Dim lines As ArrayList = New ArrayList()
Do While (sr.Peek() <> -1)
lines.Add(sr.ReadLine())
Loop
txtResults.Lines = CType(lines.ToArray(Type.GetType("System.String")), String())
sr.Close()
Panel1.Visible = True
txtResults.Select(0, 0)
End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.