Click to See Complete Forum and Search --> : Reading A textFile to a LAbel
themaverick
November 19th, 2004, 06:35 AM
i am doing a educational software.
I have a tree view on the left of the form with links to different chapter and topics.
On the other section of the form is the Text and graphics display.
How do i load a texfile into the label or a textbox. I am a newbie so if someone could explain it to me i will be grateful. So the thing is that when i click on the chapter index the specific texfile will be loaded into the label.
waiting for the reply.. tks..
BlkSmth
November 20th, 2004, 12:36 PM
I'm new also, but everyone seems to be helpful here so i will do my bets to help ya with that task m8....dont seem too hard
try this my friend:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
'Demonstrates how to read a file by using StreamReader
'Uses *YOUR_FILE* as an example
'try...catch is used to detect a 0 byte file.
Dim reader As StreamReader = _
New StreamReader(winDir & "\*YOUR_FILE*")
Try
Me.ListBox1.Items.Clear()
Do 'Until reader.Peek = -1
Me.ListBox1.Items.Add(reader.ReadLine)
Loop Until reader.Peek = -1
Catch
Me.ListBox1.Items.Add("File is empty")
Finally
reader.Close()
End Try
End Sub
BlkSmth
November 20th, 2004, 12:39 PM
ListBox1 text in that post = *YOURLISTBOX* ;)
BlkSmth
November 20th, 2004, 12:42 PM
as you can tell I posted rather quickly before reading that whole thing and considering the outcome...but it really dont matter I think whether or not you attach that to a LISTABOX or a LABEL??
themaverick
November 20th, 2004, 07:52 PM
hey it says type streamreader is not defined!! Do u noe wats the problem?? sorry i m bothering u.. tks..
BlkSmth
November 20th, 2004, 10:37 PM
here, paste this into the VERY top 2 lines of your code
Option Strict On
Imports System.IO
also I didnt get this working the way you'd like with a label1 m8
listbox1 works.....maybe this will do what you like
also by changing one line to this:
New StreamReader(Application.StartupPath & ".\Test.txt")
you can have it grab the txt file from the \bin directory or installed directory
hope that helps though its not quite what you expected :(
ikatiar01@yahoo.come
November 21st, 2004, 12:46 PM
I took this code from an application I have been working on, I believe its what your looking for.
Each chapter/topic will need its own txt file, for instance Computer.txt.
Create a new folder inside of the solution folder, titled Resoruces, then add all the .txt files there.
Run the solution, go to Project \ Add Existing Item, choose files, then in properties window add them as Embedded.
Tell me what you think.
Chris
=======================================================
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents treeInformation As System.Windows.Forms.TreeView
Friend WithEvents rtbInformation As System.Windows.Forms.RichTextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.treeInformation = New System.Windows.Forms.TreeView
Me.rtbInformation = New System.Windows.Forms.RichTextBox
Me.SuspendLayout()
'
'treeInformation
'
Me.treeInformation.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.treeInformation.ImageIndex = -1
Me.treeInformation.Location = New System.Drawing.Point(0, 0)
Me.treeInformation.Name = "treeInformation"
Me.treeInformation.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Information1", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Example1"), New System.Windows.Forms.TreeNode("Example2"), New System.Windows.Forms.TreeNode("Example3")})})
Me.treeInformation.SelectedImageIndex = -1
Me.treeInformation.Size = New System.Drawing.Size(176, 392)
Me.treeInformation.TabIndex = 0
'
'rtbInformation
'
Me.rtbInformation.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.rtbInformation.Location = New System.Drawing.Point(176, 0)
Me.rtbInformation.Name = "rtbInformation"
Me.rtbInformation.Size = New System.Drawing.Size(384, 392)
Me.rtbInformation.TabIndex = 1
Me.rtbInformation.Text = "rtbInformation"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(560, 389)
Me.Controls.Add(Me.rtbInformation)
Me.Controls.Add(Me.treeInformation)
Me.Name = "Form1"
Me.Text = "Information"
Me.ResumeLayout(False)
End Sub
#End Region
Function GetResourceContent(ByVal filename As String) As String
' get the current assembly
Dim asm As System.Reflection.[Assembly] = _
System.Reflection.[Assembly].GetExecutingAssembly()
' resources are named using a fully qualified name
Dim strm As System.IO.Stream = asm.GetManifestResourceStream(asm.GetName().Name + "." + filename)
' read the contents of the embedded file
Dim reader As New System.IO.StreamReader(strm)
GetResourceContent = reader.ReadToEnd()
reader.Close()
End Function
Private Sub treeInformation_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeInformation.AfterSelect
rtbInformation.Text = GetResourceContent(treeInformation.SelectedNode.Text & ".txt")
End Sub
End Class
themaverick
November 22nd, 2004, 10:33 PM
tks mate.. will try it out and give u a messsage
themaverick
November 27th, 2004, 11:42 PM
i used u r codes but it gives me an error mesaage..
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: Value cannot be null.
reader As New System.IO.StreamReader(strm) is highled in GREEN
So i checked my codes again .. n thought of using this..
Function GetFileContent(ByVal Filename As String)
Dim fsobject As FileStream
Dim sinput As StreamReader
Dim fname As String
Dim fcontent As String
fsobject = New FileStream(Filename, FileMode.Open)
sinput = New StreamReader(fsobject)
fcontent = sinput.ReadLine()
Label2.Text = fcontent
fsobject.Close()
sinput.Close()
End Function
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Splitter1_SplitterMoved(ByVal sender As System.Object, ByVal e As System.Windows.Forms.SplitterEventArgs) Handles Splitter1.SplitterMoved
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
GetFileContent(TreeView1.SelectedNode.Text & ".txt")
It worked too.. But i dun think it has exception handling.. or so.. I guess it not that stable.. What do u think??.. Gimme a response tks....
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.