英文:
How do I load form data based on variable?
问题
Form1
    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
Form2
    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
I hope this helps!
英文:
I am trying to load a form when a button is clicked. The form should show a label based on which button is clicked. When I run the code below everything works correctly EXCEPT after I go thru the buttons the variable from the last button is the only one that displays no matter what I click.
Form1
    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
Form2
    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
I have tried changing the if statements to if/else if, and rearranging when the variables were called.
When I click reboot server on form1 I want form2 to load and display the label for rebooting server, same for start, stop and connection info.
答案1
得分: 1
创建一个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
现在,你的第二个表单简化为:
```vbnet
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
英文:
Make an Enum to hold the four different states, then declared a Shared variable of that type:
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
Now your second form simply becomes:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论