Access a form's controls from another class?

aspninja

New member
Joined
Aug 14, 2006
Messages
3
Programming Experience
1-3
I have a simple form containing just 1 textbox and calling a separate class. I am having some difficulty getting the class to be able to access the form's controls. I can get 'step 1' to work but not 'step 2'.

VB.NET:
Public Class Form1
    Public watchfolder As System.IO.FileSystemWatcher
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        watchfolder = New System.IO.FileSystemWatcher()
        watchfolder.Path = "C:\Upload"
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
        AddHandler watchfolder.Created, AddressOf logchange
        watchfolder.EnableRaisingEvents = True
        TextBox1.Text &= "Waiting for upload..." & vbCrLf
    End Sub
 
    Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Created Then
            TextBox1.Text &= "File " & e.FullPath & " has been created" & vbCrLf
            DoSomeStuff()
            Dim c1 As New Class1
            c1.DoSomeMoreStuff()
            c1 = Nothing
        End If
    End Sub
 
    Private Sub DoSomeStuff()
        ' ### Step 1 ###
        TextBox1.Text &= "Step 1 is done." & vbCrLf
    End Sub
End Class
 
Public Class Class1
    Public Sub DoSomeMoreStuff()
        ' ### Step 2 ###
        Form1.TextBox1.Text &= "Step 2 is done." & vbCrLf
    End Sub
End Class

Any ideas?

- ASPninja
 
Your instance of Class1 doesn't know that your instance of Form1 exists. As a real life analogy, I am an instance of the Person class. If I make a robot, can that robot just do Person.PutOnRedShirt and expect me to be magically wearing a red shirt? No. It has to first of all know which specific person to put the red shirt on. It can't know that unless I tell it that I exist. The same is true on programming objects. Your Class1 insiance has to refer to a specific instance of the Form1 class, not just the class itself, and the Form1 instance that created the Class1 instance about itself. I suggest that you read the Multiple Forms tutorial in my signature. It deals with exactly what the name suggests, but there is nothing special about forms in this regard. An object is an object and you must have a reference to an object and call a method of that specific object to make things happen.
 
Thanks for the tip. I got it to work by passing class1 a referance to the textbox:

VB.NET:
Public Class Form1
    Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        ...
        Dim c1 As New Class1()
        c1.DoSomeMoreStuff(Me.TextBox1)
        c1 = Nothing
        ...
    End Sub
End Class
Public Class Class1
    Public Sub DoSomeMoreStuff(ByRef txtBox As TextBox)
        txtBox.Text &= "Step 2 is done." & vbCrLf
    End Sub
End Class

I couldn't get the stuff in that article(making class1 inherit form1, giving form1 a public property that get/sets the textbox) to work for me... is there a better way of doing this?
 
The better way is how the tutorial explained it. Passing a reference to the TextBox is not the best idea because you can then change any of its properties instead of just the Text. Also, you absolutely NOT be passing it ByRef. That means that you could assign a completely different TextBox, which is certainly not what you want.
 
so you're saying i should roll with a module (part 4 of that article)?

VB.NET:
Friend Module Module1
    Friend f1 As Form1
End Module
 
Public Class Class1
    Public Sub DoSomeMoreStuff()
        f1.TextBox1.Text &= "Step 2 is done." & vbCrLf
    End Sub
End Class

Are these used a lot in the world?
 
Back
Top