Results 1 to 4 of 4

Thread: Problems referencing form object from module..

  1. #1

    Thread Starter
    Lively Member nemesys777's Avatar
    Join Date
    Nov 2001
    Posts
    103

    Problems referencing form object from module..

    Hi,
    In my project I have form1 and module1
    On form1 I have a textbox1

    In my module I am trying to open a text file and send it to the textbox on form1. This is my code..

    Code:
    Imports system.IO
    
    Dim serverconfig As TextReader 
    
    Public Sub openfile() 
            Dim frm1 As New Form1() 
            Dim a As String 
            a = "C:\a.txt" 
            serverconfig = New StreamReader(a) 
            frm1.TextBox1.Text = serverconfig.ReadToEnd 
    End Sub
    On form1 Load I simply put -- "openfile"

    When I do it this way, no text appears in my textbox. But If I put the code from my module directly into my form1_load (replacing frm1.Textbox1.Text with Textbox1.Text) It prints out the Text File fine. I seem to be having problems with the line "frm1.TextBox1.Text = serverconfig.ReadToEnd"...
    However in my module if I just type frm1...Then it comes up with all the objects on my Form1 (TextBox1) So I don't know what the problem is...
    Any help would be great. Thx

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    having troubles? Does that mean it does nothing? Gives an error? If error, what error?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Lively Member nemesys777's Avatar
    Join Date
    Nov 2001
    Posts
    103
    There is no error.. It simply just doesnt print the contents of the textfile (a.txt) into my TextBox1.Text on Form1............

    However If I use the move the code from my module to the Form1_Load It then works....(prints the textfile into the textbox)

  4. #4

    Thread Starter
    Lively Member nemesys777's Avatar
    Join Date
    Nov 2001
    Posts
    103
    I found out what I was doing wrong. Another guy helped me out...I should have had my code like this:

    Code:
    Public serverconfig As TextReader
    
    Public Sub Listen(ByVal frm1 As Form1)
            Dim a As String
            a = "C:\a.txt"
            serverconfig = New StreamReader(a)
            frm1.TextBox1.Text = serverconfig.ReadToEnd
    End Sub
    
    Form1_Load
    Listen(Me)
    End Sub
    Apparently..This is something VB6 programmers have been used to..Unfortunately I had made the transition from VB5 to .NET...So there's a few things I'm not used to yet..Anyways thanks for the help.

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