Solved

How do I use an openfiledialog box in a class?

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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