Results 1 to 4 of 4

Thread: make form as shared object!

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    make form as shared object!

    Is it possible to turn a form as a shared object.Since VB.NET is OO . I've tried but I can't call functions in form1.

  2. #2
    Member
    Join Date
    Dec 2002
    Location
    NY, USA
    Posts
    52
    YOu can do it like that


    Code:
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    #End Region
    
    #Region " Global Form "
        'Copy this Region on every Form you wish to be accessed globally
        'Just rename the Form1 to the name of your Form
        Private Shared m_GlobalForm As Form1
        Public Shared Property GlobalForm() As Form1
            Get
                If m_GlobalForm Is Nothing OrElse m_GlobalForm.IsDisposed Then
                    m_GlobalForm = New Form1()
                End If
                Return m_GlobalForm
            End Get
            Set(ByVal Value As Form1)
                m_GlobalForm = Value
            End Set
        End Property
    #End Region
    
        Public SampleVariable As String
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Initialized Itself so its property can be accessed to any form in you project
            'You only need to declare this if this form is your Starting Form
            Me.GlobalForm = Me
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Dim Form2 as new Form2  -> no need to declare this, the
            'new Public Shared Property of Form2 will create automaticaly the 
            'new instance of Form2 
            Form2.GlobalForm.ShowDialog()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.Close()
        End Sub
    End Class
     '===============Form2==================
    Public Class Form2
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    #End Region
    
    #Region " Global Form "
        Private Shared m_GlobalForm As Form2
        Public Shared Property GlobalForm() As Form2
            Get
                If m_GlobalForm Is Nothing OrElse m_GlobalForm.IsDisposed Then
                    m_GlobalForm = New Form2()
                End If
                Return m_GlobalForm
            End Get
            Set(ByVal Value As Form2)
                m_GlobalForm = Value
            End Set
        End Property
    #End Region
    
    'send some text to form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Copy Form2.Textbox1 to Form1.TextBox1 
            Form1.GlobalForm.TextBox1.Text = Me.TextBox1.Text
            'You can even access Public variables of Form1 
            Form1.GlobalForm.SampleVariable = Me.TextBox1.Text
        End Sub
    
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            'If you close this form without setting Form1.GlobalForm = Nothing, 
            'it will work fine but the Form2 will not initialized its property 
            'and controls to original state when you show again the Form2
            Me.GlobalForm = Nothing
            Me.Close()
        End Sub
    End Class
    Iouri Boutchkine

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    VB Code:
    1. Private Shared Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2. 'some code
    3.  
    4. End Sub
    I want to have all objects (methods , functions ,sub) as static .I want form1 for example to be one shared class or object !

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I got error in form2 says (C:\Documents and Settings\Pirate\My Documents\Visual Studio Projects\WindowsApplication1\Form2.vb(83): Handles clause requires a WithEvents variable. )

    and this won't let me call subs in form1 !!
    anything wrong ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width