Solved

How do I use an openfiledialog box in a class?

Posted on 2004-10-02
5
167 Views
Last Modified: 2010-04-23
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
Comment
Question by:BrianLetterman
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 63 total points
ID: 12210439
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
 
LVL 7

Assisted Solution

by:J_Mak
J_Mak earned 62 total points
ID: 12210471
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now