Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

How do I use an openfiledialog box in a class?

I have a form to open a text file and count the number of characters, words, sentences and paragraphs.  I am trying to use a class to do all of the work but cannot figure out how to refence labels and buttons on the form in the class?  Any suggextions?
0
BrianLetterman
Asked:
BrianLetterman
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can pass in a reference to either the form containing your labels and buttons, or directly pass references to your labels and buttons into your class.  You can do this either is several ways:

1) Use the constructor of your class:

Public Class Foo

    Private f As Form

    Public Sub New(ByVal sourceForm As Form)
        f = sourceForm
    End Sub

End Class

From a form:
    Dim clsFoo As New Foo(Me)

2) Make your form reference variable public in your class:

Public Class Foo

    Public f As Form

End Class

From a form:
    Dim clsFoo As New Foo()
    clsFoo.f = Me

3) Pass in a reference directly to a sub/function in your class:

Public Class Foo

    Public Sub updateSomeValue(ByVal lbl As Label)
        lbl.Text = "3.14159"
    End Sub

End Class

From a form:
    Dim clsFoo As New Foo()
    clsFoo.updateSomeValue(Me.Label1)
0
 
J_MakCommented:
I don't know where openfiledialog box comes into the picture but it's very trivial to reference labels and buttons in a class. What you could do is in your class you could create a property like so:

Public Class YourClassName

    Private formReference As frmName

    Friend Property FormToAccess() As frmName
        Get
            FormToAccess = formReference
        End Get
        Set(ByVal Value As frmName)
            formReference = Value
        End Set
    End Property
    .......

End Class

And in your code behind the form, you could do something like this:

Public Class frmName
    Inherits System.Windows.Forms.Form

    Dim cls As YourClassName = New YourClassName
    .....
    cls.FormToAccess = Me 'This line assigns this form to the class's 'FormToAccess' property, hence you can access any of the forms labels or buttons using it.
    ....

End Class

I hope that is understandable. Cheers.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now