Visual Basic与其他表单问题进行通信

时间:2014-11-25 20:52:23

标签: vb.net forms

所以我想在visual basic中的两个表单之间进行通信。 Form2有一个您选择的按钮,其目的是使形式3的图片框可见。我把'#34;继承Form3"作为表单2声明,但是picbox出现在From 2而不是Form 3上。单击按钮时的代码是

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

如何让它仅在form3上可见?

2 个答案:

答案 0 :(得分:1)

在VB.Net中,表单具有所谓的“默认实例”;这意味着你可以引用它们而不必将它们声明为对象(如果你不想这样做)。

所以,如果你想从另一种形式改变Form3上的一个对象(正如RBarryYoung在上面指出的那样),你写道:

Form3.picSupra.Visible = True

继承是一个完全独立的概念,与您所要求的内容无关。

答案 1 :(得分:0)

另一种方法可能是使用一个可观察的对象,通知它何时被更改,以及您可以用来绑定,或者只是注册/取消注册更新

对于共享类,例如:

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

这是一个单例实现,提供了一个PropertyChangedEvent(这样任何对变更感兴趣的人都可以在需要时通知(通过AddHandler / RemoveHandler))

Form1的一个例子是(它包含1个CheckBox和1个按钮)

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

然后Form2看起来像这样,听同一个对象,并在更改时更新生动。然后,当您选中Form1中的复选框时,无论目前有多少Form2可见,都会自动进行更改

重要的是要注意,您不应该忘记调用RemoveHandler,或者一旦您打开form2一次或多次,内存使用量可能会突然增加:)

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