Solved

How do I use an openfiledialog box in a class?

Posted on 2004-10-02
5
190 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

763 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