通过另一个类修改主类的组件

时间:2014-10-06 12:58:49

标签: vb.net winforms class

我们有一个学校项目,我试图将我们的代码分成几组。我将FormBorderStyle设置为None,以便我可以修改它。但是我可以使用另一个类来修改我主要的组件吗? 一个例子是:

Public Class main
    Private Sub btn_title_bar_exit_Click(sender As Object, e As EventArgs) Handles btn_title_bar_exit.Click
        Me.Close()
    End Sub
    Private Sub btn_title_bar_minimize_Click(sender As Object, e As EventArgs) Handles btn_title_bar_minimize.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub

End Class

Public Class user_interface
    'Modify components and the form through here
    'example: btn_exit.ForeColor = Color.Black 
    'example: Me.Close()
End Class

还想知道,这是打破代码的好方法吗?

1 个答案:

答案 0 :(得分:1)

如果你只是想分开例程(函数/方法/变量声明......),因为你有很多代码填充你的类文件,只需使用:

1)Partial关键字

Public Partial Class MainClass
    ' MAIN CONTENT

    Public Sub New()
        Me.InitializeComponents()
        ' ...
    End Sub

    ' ...
End Class

并在另一个档案中:

Public Partial Class MainClass
    ' USER INTERFACE HANDLERS...

    'Modify components and the form through here
    'example: btn_exit.ForeColor = Color.Black 
    'example: Me.Close()

    ' ...
End Class

所以这是相同的类,其他类可能无法访问/修改其成员。

2)或使用#Region拆分代码并对其进行分组:

Public Class MainClass

    #Region "Instanciation..."
    Public Sub New()
        Me.InitializeComponents()
        ' ...
    End Sub
    #End Region

    #Region "User Interface..."
        'Modify components and the form through here
        'example: btn_exit.ForeColor = Color.Black 
        'example: Me.Close()
        Public Sub btn1_Click(...)
        Public Sub Picture1_MouseMove(...)
    #End Region

    #Region "Public Properties..."
    '...
    #End Region

End Class

然后使用加号/减号展开/折叠代码的那一部分。



如果确实想要从其他课程编辑MainClass的成员,比如RemoteClass,那么 TONs 就是这样做的它取决于您想要做的完全

您可以在MainClass中创建 Public

找到您的表单设计器文件(包含表单所有控件声明的文件)并将每个声明更改为Public。

Private pictureBox1 As Picturebox
' becomes
Public pictureBox1 As Picturebox

(或者只需单击IDE中的控件,并将其辅助功能级别更改为“公共”)

然后,如果您可以在MainClass的实例中传递指向RemoteClass实例的变量,则可以通过RemoteClass访问TheMainClassInstance.pictureBox1,然后更改它的大小,位置等一切。

那么如何在RemoteClass中创建MainClass实例?这取决于你的应用程序的结构...没有细节,猜猜是什么..我们必须猜测..!

Public Class RemoteClass
    Private _InstanceOfMainClass As MainClass = Nothing

    Public Sub New(ByRef NewInstanceOfMainClass As MainClass)
        _InstanceOfMainClass = NewInstanceOfMainClass
        ' ^^ this is one way doing it.
        ' ...
    End Sub
    ' ...

    Private Sub ChangeBackgroundColor()
        _InstanceOfMainClass.picturebox1.BackColor = Color.Black ' and voila !
    End Sub

End Class

然后你的RemoteClass中有一个MainClass。不要忘记处理_InstanceOfMainClass以避免内存泄漏(我假设你知道如何......)


你有多少个MainClass实例?
如果它只是一个,并且您有几个RemoteClass类,那么您可以考虑将要作为静态(共享)成员访问的MainClass成员。

Public Class MainClass
    Private Shared _MyInstance As MainClass = Nothing

    Private Shared Sub InitializeMyInstance()
        If _MyInstance Is Nothing Then
            _MyInstance = New MainClass(...)
        Else
            If _MyInstance.IsDisposed Then
                ' Requires an IDisposable interface 
                ' and handling of Me.Closed event elsewhere...
                _MyInstance = Nothing
                _MyInstance = New MainClass(...)
            End If
        End If
    End Sub

    Public Shared ReadOnly Property MyInstance() As MainClass
        Get
            InitializeMyInstance()
            Return _MyInstance
        End Get
    End Property

    Public Shared ReadOnly Property PictureBox1() As PictureBox
        Get
            Return MyInstance.pictureBox1
        End Get
    End Property

    ' Create as many Properties as required...

End Class

然后在任何RemoteClass实例中,您只需调用:

MainClass.PictureBox1.Width = 400
MainClass.Close()
MainClass.PictureBox1.Height = 200

但正如我所说,只有当你只有一个MainClass实例时,这才有效。


如果您有多个MainClass实例和多个RemoteClass实例,请考虑使用唯一ID和静态函数/属性来访问特定实例。

如何创建ID?

Public Class MainClass
    Private Shared _NextID As Integer = 0
    Private _ID As Integer

    Public ReadOnly Property ID() As Integer
        Get
            Return _ID
        End Get
    End Property

    Public Sub New(...)
        Me.InitializeComponents()
        ' ...
        _ID = _NextID
        _NextID = _NextID + 1
    End Sub

    ' ...
End Class

然后......创建一个包含所有ID的排序列表:

Public Class MainClass

    Private Shared _IDsList As New SortedList(Of Integer, MainClass)

    ' then edit your New() method :
    Public Sub New(...)
        Me.InitializeComponents()
        ' ...
        _ID = _NextID
        _NextID = _NextID + 1
        _IDList.Add(_ID, Me)
    End Sub

    ' ...
End Class

然后创建一个静态函数以通过其ID获取MainClass的特定实例。

Public Class MainClass

    ' ...

    Public Shared Function GetInstanceByID(ByVal iID As Integer) As MainClass
        If _IDList.ContainsKey(iID) Then
            Return _IDList.Item(iID)
        Else
            Return Nothing
        End If
    End Function

    ' ...

    ' And create the appropriate Dispose() method
    ' the appropriate Clear() method
    ' the appropriate FormClosing events handlers
    ' etc. etc. etc.

End Sub
事实是,我们不知道你的MainClass的目的是什么,以及为什么RemoteClass实例必须修改MainClass成员(哪些成员呢?控件,变量,添加/删除控件?)