Solved

How do I use an openfiledialog box in a class?

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

20 Experts available now in Live!

Get 1:1 Help Now