winforms 如何基于变量加载表单数据?

pkwftd7m  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(90)

我试图加载一个表单时,一个按钮被单击。该表单应该显示一个标签的基础上,哪个按钮被单击。当我运行下面的代码一切正常工作,除了我通过按钮后,变量从最后一个按钮是唯一一个显示,无论我点击什么。
表格1

Public Class MainScreen
        Public RebootServer As Integer = 0
        Public StartServer As Integer = 0
        Public StopServer As Integer = 0
        Public CurrentServerInfo As Integer = 0

        Private Sub RebootServerMenuItem_Click(sender As Object, e As EventArgs) Handles RebootServerMenuItem.Click
            RebootServer = 1
            CurrentConnectionForm.Show()
        End Sub

        Private Sub StartServerMenuItem_Click(sender As Object, e As EventArgs) Handles StartServerMenuItem.Click
            StartServer = 1
            CurrentConnectionForm.Show()

        End Sub

        Private Sub StopServerMenuItem_Click(sender As Object, e As EventArgs) Handles StopServerMenuItem.Click
            StopServer = 1
            CurrentConnectionForm.Show()

        End Sub

        Private Sub ConnectionInfoMenuItem_Click(sender As Object, e As EventArgs) Handles ConnectionInfoMenuItem.Click
            CurrentServerInfo = 1
            CurrentConnectionForm.Show()

        End Sub
    End Class

表格2

Public Class CurrentConnectionForm

        Dim RebootServer As Integer = MainScreen.RebootServer
        Dim StartServer As Integer = MainScreen.StartServer
        Dim StopServer As Integer = MainScreen.StopServer
        Dim CurrentServerInfo As Integer = MainScreen.CurrentServerInfo

        Private Sub CurrentConnectionForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

            If RebootServer = 1 Then
                RebootServerLabel.Visible = True
                StartServerLabel.Visible = False
                StopServerLabel.Visible = False
                ConnectionInfoLabel.Visible = False
            End If
            If StartServer = 1 Then
                RebootServerLabel.Visible = False
                StartServerLabel.Visible = True
                StopServerLabel.Visible = False
                ConnectionInfoLabel.Visible = False
            End If
            If StopServer = 1 Then
                RebootServerLabel.Visible = False
                StartServerLabel.Visible = False
                StopServerLabel.Visible = True
                ConnectionInfoLabel.Visible = False
            End If
            If CurrentServerInfo = 1 Then
                RebootServerLabel.Visible = False
                StartServerLabel.Visible = False
                StopServerLabel.Visible = False
                ConnectionInfoLabel.Visible = True
            End If
        End Sub
    End Class

我尝试过将if语句更改为if/else if,并重新安排调用变量的时间。
当我在form 1上单击reboot server时,我希望form 2加载并显示reboot server的标签,start、stop和connection info也是如此。

xoefb8l8

xoefb8l81#

创建一个Enum来保存四种不同的状态,然后声明一个该类型的Shared变量:

Public Class MainScreen

    Public Enum ServerTypeEnum
        RebootServer
        StartServer
        StopServer
        CurrentServerInfo
    End Enum

    Public Shared ServerType As ServerTypeEnum

    Private Sub RebootServerMenuItem_Click(sender As Object, e As EventArgs) Handles RebootServerMenuItem.Click
        ServerType = ServerTypeEnum.RebootServer
        CurrentConnectionForm.Show()
    End Sub

    Private Sub StartServerMenuItem_Click(sender As Object, e As EventArgs) Handles StartServerMenuItem.Click
        ServerType = ServerTypeEnum.StartServer
        CurrentConnectionForm.Show()
    End Sub

    Private Sub StopServerMenuItem_Click(sender As Object, e As EventArgs) Handles StopServerMenuItem.Click
        ServerType = ServerTypeEnum.StopServer
        CurrentConnectionForm.Show()
    End Sub

    Private Sub ConnectionInfoMenuItem_Click(sender As Object, e As EventArgs) Handles ConnectionInfoMenuItem.Click
        ServerType = ServerTypeEnum.CurrentServerInfo
        CurrentConnectionForm.Show()
    End Sub

End Class

现在你的第二种形式变成了:

Public Class CurrentConnectionForm

    Private Sub CurrentConnectionForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        RebootServerLabel.Visible = (MainScreen.ServerType = MainScreen.ServerTypeEnum.RebootServer)
        StartServerLabel.Visible = (MainScreen.ServerType = MainScreen.ServerTypeEnum.StartServer)
        StopServerLabel.Visible = (MainScreen.ServerType = MainScreen.ServerTypeEnum.StopServer)
        ConnectionInfoLabel.Visible = (MainScreen.ServerType = MainScreen.ServerTypeEnum.CurrentServerInfo)
    End Sub

End Class

相关问题