开发者

VB.net form reopens on its own

i have a login form, which se开发者_运维知识库nds u if login is correct to a main menu...the main menu has buttons leading to other forms. when clicking a button it hides the main menu and show the other form. but the problem is that when the main menu hides and other appears, the main menu reopens on its own and each time u close it, it opens again. its driving me crazy. here's the code :-

Public Class mainmenu

    Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        Maintenance.Show()
    End Sub

    Private Sub mainmenu_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
        If Login.c1 <> 0 Then
            Me.memberbtn.Visible = True
        Else
            Me.memberbtn.Visible = False
        End If
    End Sub

    Private Sub memberbtn_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles memberbtn.Click
        Me.Hide()
        Member.Show()
    End Sub
End Class

check the click events. the program doesnt give me any errors. plz help.


You posted the wrong code. You must have done something to ensure that the menu becomes visible again when the user closes the form. That's the code that is causing the problem.

Let's make another version of it that doesn't have this problem. You need to listen to the FormClosed event to know that the menu needs to become visible again. Write a little helper method that ensures this:

Private Sub DisplayForm(ByVal frm As Form)
    AddHandler frm.FormClosed, AddressOf DisplayMenu
    frm.Show()
    Me.Hide()
End Sub

Private Sub DisplayMenu(ByVal sender As Object, ByVal e As EventArgs)
    Me.Show()
End Sub

A button's click event handler is now simple:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    DisplayForm(New Maintenance())
End Sub

You can further improve the DisplayForm method. It is very likely you'll want to set the form's StartPosition property to manual and set its Location property so that the forms show up in a consistent location on the screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜