CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2004
    Posts
    14

    Exclamation Reading A textFile to a LAbel

    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..

  2. #2
    Join Date
    Nov 2004
    Posts
    35

    Talking Re: Reading A textFile to a LAbel

    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

  3. #3
    Join Date
    Nov 2004
    Posts
    35

    Exclamation Re: Reading A textFile to a LAbel

    ListBox1 text in that post = *YOURLISTBOX*

  4. #4
    Join Date
    Nov 2004
    Posts
    35

    Red face Re: Reading A textFile to a LAbel

    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??

  5. #5
    Join Date
    Nov 2004
    Posts
    14

    Re: Reading A textFile to a LAbel

    hey it says type streamreader is not defined!! Do u noe wats the problem?? sorry i m bothering u.. tks..
    Last edited by themaverick; November 20th, 2004 at 09:23 PM.

  6. #6
    Join Date
    Nov 2004
    Posts
    35

    Post Re: Reading A textFile to a LAbel

    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

  7. #7
    Join Date
    Oct 2004
    Posts
    17

    Re: Reading A textFile to a LAbel

    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
    Last edited by [email protected]; November 21st, 2004 at 01:56 PM.

  8. #8
    Join Date
    Nov 2004
    Posts
    14

    Re: Reading A textFile to a LAbel

    tks mate.. will try it out and give u a messsage

  9. #9
    Join Date
    Nov 2004
    Posts
    14

    Re: Reading A textFile to a LAbel

    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....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured