Public Shared Form Control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RSH

    #1

    Public Shared Form Control

    Hi,

    I have a situation where I have a Windows Form and I have 2 classes defined
    in the code, the primary form and a class that I got from the internet. I
    have a label in the form that I would like the 2nd class to write to...so I
    declared the label as "Public Shared". Which worked, I can write to it.
    But I get an error when closing down visual studio, it says the label was
    never declared and when i reopen Visual the label has disappeared off of the
    form. When I go to put it back I get an error saying it already exists.

    What is going on here? I simply need to write text to a label in the form
    from a class.

    Structure:

    Public Class DTSConvertor725

    Inherits System.Windows. Forms.Form

    'Required by the Windows Form Designer

    Private components As System.Componen tModel.IContain er

    'NOTE: The following procedure is required by the Windows Form Designer

    'It can be modified using the Windows Form Designer.

    'Do not modify it using the code editor.

    Friend WithEvents OpenFileDialog1 As System.Windows. Forms.OpenFileD ialog

    Friend WithEvents TestSQLConnecti on As System.Windows. Forms.Button

    Public Shared label1 As System.Windows. Forms.Label

    <code goes here>

    End Class




    Public Class PackageEventsSi nk

    Inherits System.Windows. Forms.Form

    Implements DTS.PackageEven ts

    Public fParentForm As DTSConvertor725

    Overridable Overloads Sub OnStart(ByVal EventSource As String) _

    Implements DTS.PackageEven ts.OnStart

    Console.WriteLi ne("START in {0}", EventSource)

    fParentForm.Ste pCount = fParentForm.Ste pCount + 1

    fParentForm.lab el1.Text = "Step " & fParentForm.Ste pCount & " " &
    EventSource

    System.Windows. Forms.Applicati on.DoEvents()

    End Sub

    End Class


  • Armin Zingler

    #2
    Re: Public Shared Form Control

    "RSH" <way_beyond_oop s@yahoo.com> schrieb[color=blue]
    > Hi,
    >
    > I have a situation where I have a Windows Form and I have 2 classes
    > defined in the code, the primary form and a class that I got from
    > the internet. I have a label in the form that I would like the 2nd
    > class to write to...so I declared the label as "Public Shared". Which
    > worked, I can write to it. But I get an error when closing
    > down visual studio, it says the label was never declared and when i
    > reopen Visual the label has disappeared off of the form. When I go
    > to put it back I get an error saying it already exists.
    >
    > What is going on here? I simply need to write text to a label in
    > the form from a class.[/color]


    A label can not be shared - at least not if you use the designer. It doesn't
    make sense. Without creating a Form, it wouldn't be visible anyway.

    If you want to access an object, you need a reference. If you don't have
    one, make it available. It depends on the circumstances how to do this.
    Usual approach is pass the reference to the class that needs the reference.


    Armin

    Comment

    Working...