Vb.net Visual Basic与其他表单问题的通信
所以我想用VisualBasic在两个表单之间进行通信。Form2有一个按钮,您可以选择,其目的是使表格3中的图片框可见。我将“Inherits Form3”作为Form 2声明,但picbox显示在From 2上,而不是Form 3上。单击按钮时的代码是Vb.net Visual Basic与其他表单问题的通信,vb.net,forms,Vb.net,Forms,所以我想用VisualBasic在两个表单之间进行通信。Form2有一个按钮,您可以选择,其目的是使表格3中的图片框可见。我将“Inherits Form3”作为Form 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)
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上可见?在VB.Net中,窗体具有所谓的“默认实例”;这意味着您可以引用它们,而无需将它们声明为对象(如果您不想) 因此,如果您想将Form3上的对象从另一个表单更改为其他表单(正如RBarryYoung在上面指出的),您可以编写:
Form3.picSupra.Visible = True
继承是一个完全独立的概念,与您的要求无关。另一种方法可能是使用一个可观察的对象,该对象在更改时发出通知,您可以使用该对象绑定到更新,或者只是注册/注销更新 例如,对于共享类:
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个复选框和1个按钮)
Inherits
与类继承有关,它根本不是您想要的。只要说Form3.picSupra.Visible=True
。因为表单是类,所以您应该能够继承。这可能不是你真正需要/想要做的。
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
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