Windows Develop Bookmark and Share   
 index > Windows Forms General > Simple forms question...
 

Simple forms question...

I have my primary form (Form1) and a label control on there (label1).  In my project I've created many custom classes for grabbing data of which I would like to display to the user via the primary form's label1.Text property.

How do I go about giving a class used by Form1 direct access to a control's property on Form1...and not via returning values from the functions called...but direct access?  Is this possible...I feel like I'm missing something very simple...

Many thanks for any help...


In a nutshell:

Form1 creates and runs a class (engine) to do a bunch of work...and I want engine to update directly Form1.label1.Text with it's status while it's working...
MigrationUser 1  Tuesday, April 05, 2005 9:44 PM
I think that the best practice, most flexible and scalable way to do that is to declare and fire an event in your class and capture that event with Form1.  Pass the data you want to display as an event parameter.

In Form1, you'd set it up as such:

+++++++++++++++++++++++++++
.
.
'In declarations section of your form...
Private WithEvents classInstanceVar1 As MyFirstClass
Private WithEvents classInstanceVar2 As MySecondClass

Public Sub SetLabel1Text(ByVal data As String) Handles classInstanceVar.InfoEvent  Label1.Text = data
End Sub
.
.
+++++++++++++++++++++++++++


In your MyFirstClass class here's code example of setting up and raising event to pass text for label1...
+++++++++++++++++++++++++++
Public Class MyFirstClass

Public Event InfoEvent(ByVal infoText As String)

Public Sub SomeSubroutineThatRaisesEvent()
'tell everybody who's listening to me, that I'm starting this job...
RaiseEvent InfoEvent("Hey Form1, I started doing some work...")
'do some work here...
'tell everyone I'm done doing work above...
RaiseEvent InfoEvent("Hey Form1, I'm done doing part X of job Y")
'do some more work here...
End Sub


End Class
+++++++++++++++++++++++++++
MigrationUser 1  Wednesday, April 06, 2005 3:48 AM
How can I do the same thing but have form1 create and raise the event and have form2 respond to it.
MigrationUser 1  Wednesday, April 06, 2005 11:13 AM
Well, a form is a class as well and can raise its own custom events...
You'd add the same event code to Form1 like I did in MyFirstClass in my first post.
Then Raise the event from Subs or Functions inside Form1 as Form1 does work or whatever.

What you need is a reference to form1 from form2 so that you can
handle form1's events...

one way is to open Form1 from inside Form2

inside Form2:

Public Class Form2
'
Private WithEvents frm As Form1
'
'
Public Sub ShowForm1()
'maybe call this Sub with a button click event or something
frm = New Form1
frm.Show()
End Sub
'
'
Private Sub form1info (ByVal data As String) Handles frm.InfoEvent
Label1.Text = data
End Sub
'
End Class


Another way maybe (but I haven't tested this) is to have a global variable for storing a reference to Form1 in a Module and then the global var is available to all forms.

e.g. instead of declaring

Private WithEvents frm As Form1

inside of Form2, you could add a module to your project
and declare the frm variable public as such:

Public WithEvents frm As Form1

and then set the frm variable to New Form1 and Show() it from anywhere.
MigrationUser 1  Wednesday, April 06, 2005 5:04 PM
You might just modify your class object to take a reference to the control you want to use as the target of your info.

You could do something simple like:

Public Class Engine
    Private myLabel As Label = Nothing

    'Add code for constructors, properties, and methods

    Public Property LabelControl() As Label
        Get
            Return myLabel
        End Get
        Set(ByVal Value As Label)
            myLabel = Value
        End Set
    End Property

    'A sample method that uses the label
    Public Sub SampleMethod()
        'do some work
        If Not IsNothing(myLabel) Then
            myLabel.Text = "SampleMethod did some work"
        End If
    End Sub
End Class

Now when you create the instance of the Engine object, you can set a reference to whatever label control you want, and change it whenever necessary.

        Dim myEngine As New Engine
        myEngine.LabelControl = Label1
        myEngine.SampleMethod()
MigrationUser 1  Wednesday, April 06, 2005 5:52 PM

You can use google to search for other answers

Custom Search

More Threads

• SqlClientPermission
• Bug in DateTimePicker control?
• Problem with using the CollectionEditor class for component class in C#.
• How to use SetLocaleInfo in C# for change regional settiings of a system using c# code
• Preventing a form from losing focus
• Excel in the WebBrowser control
• Setting custom control default settings from config
• detaching events
• Call A DTS Package
• BackgroundWorker loses / repeats progress events.