Amit Patel
Amit Patel

Reputation: 15

Visual Basic communicating with other form problems

So i want to communicate between 2 forms in visual basic. Form2 has a button that you select and the intention is that it will make the pic box in form 3 visible. I put "Inherits Form3" as a Form 2 Declaration, but the picbox appears on the From 2 instead of Form 3. the code when you click on the button is

Public Class Form2 Inherits Form3 Private Sub cmdSupra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSupra.Click

    picSupra.Visible = True
    Form3.Show()
End Sub
End Class

How can i make it only visible on form3 ?

Upvotes: 1

Views: 83

Answers (2)

Icepickle
Icepickle

Reputation: 12806

Another way might be to use an observable object, that notifies when it got changed, and which you could use to bind to, or simply to register/unregister on updates

An example would be, for the shared class:

Imports System.ComponentModel

Public Class SharedProperties
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Protected Overridable Sub RaisePropertyChanged(propertyName As String)
        ' raises an event to all who are listening'
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Private Shared _instance As SharedProperties
    Public Shared ReadOnly Property Instance As SharedProperties
        Get
            If _instance Is Nothing Then
                _instance = New SharedProperties()
            End If
            Return _instance
        End Get
    End Property

    Private _showPicture As Boolean = True
    Public Property ShowPicture As Boolean
        Get
            Return _showPicture
        End Get
        Set(value As Boolean)
            If _showPicture = value Then
                Return
            End If
            _showPicture = value
            RaisePropertyChanged("ShowPicture")
        End Set
    End Property

    Private Sub New()
        ' empty constructor'
    End Sub
End Class

which is a singleton implementation, offering a PropertyChangedEvent (so that anyone intrested in changes can be notified, when they want (via AddHandler / RemoveHandler))

An example of Form1 would be (which would contain 1 CheckBox, and 1 Button)

Imports System.ComponentModel

Public Class Form1

    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        RemoveHandler SharedProperties.Instance.PropertyChanged, AddressOf ShowPictureChanged
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler SharedProperties.Instance.PropertyChanged, AddressOf ShowPictureChanged
        chkShowPicture.Checked = SharedProperties.Instance.ShowPicture
    End Sub

    Private Sub ShowPictureChanged(sender As Object, e As PropertyChangedEventArgs)
        If String.Equals(e.PropertyName, "ShowPicture") Then
            If chkShowPicture.Checked <> SharedProperties.Instance.ShowPicture Then
                chkShowPicture.Checked = SharedProperties.Instance.ShowPicture
            End If
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim frm2 As New Form2

        frm2.Show()
    End Sub

    Private Sub chkShowPicture_CheckedChanged(sender As Object, e As EventArgs) Handles chkShowPicture.CheckedChanged
        SharedProperties.Instance.ShowPicture = DirectCast(sender, CheckBox).Checked
    End Sub
End Class

And Form2 could then look like this, listening to the same object, and updating quite lively on changes. When you would then Check the checkbox in Form1, the changes would be made automatically, no matter how many Form2's are visible at the moment

It's important to note that you shouldn't forget to call RemoveHandler, or your memory usage might suddenly increase once you opened form2 one or more times :)

Imports System.ComponentModel

Public Class Form2

    Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        RemoveHandler SharedProperties.Instance.PropertyChanged, AddressOf ShowPictureChanged
    End Sub

    Private Sub ShowPictureChanged(sender As Object, e As PropertyChangedEventArgs)
        If String.Equals(e.PropertyName, "ShowPicture") Then
            PictureBox1.Visible = SharedProperties.Instance.ShowPicture
        End If
    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AddHandler SharedProperties.Instance.PropertyChanged, AddressOf ShowPictureChanged
        PictureBox1.Visible = SharedProperties.Instance.ShowPicture
    End Sub
End Class

Upvotes: 0

DWRoelands
DWRoelands

Reputation: 4940

In VB.Net, forms have what is called a "default instance"; which means that you can reference them without having to declare them as objects (if you don't want to).

So, if you want to change an object on Form3 from another form (as RBarryYoung points out above), you write:

Form3.picSupra.Visible = True

Inheritance is a completely separate comcept and does not have anything to do with what you appear to be asking.

Upvotes: 1

Related Questions