Why is my basic default .acceptbutton is not worki

2019-07-21 09:53发布

What I have:

I have two group boxes each with a text box inside. A third text box is placed outside both of the group boxes.

enter image description here

Button 1 is the default accept button on form load.

What I need:

When button 1 is clicked (or enter key is pressed), I need button 2 to become the default accept button.

My problem:

Button 3 becomes the default accept button rather than button 2 in spite of my code.

My code:

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    GroupBox1.Enabled = True
    GroupBox2.Enabled = False
    Me.AcceptButton = Button1
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MessageBox.Show("Button 1 pressed!")
    GroupBox1.Enabled = False
    GroupBox2.Enabled = True
    Me.AcceptButton = Button2
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    MessageBox.Show("Button 2 pressed!")
    GroupBox1.Enabled = True
    GroupBox2.Enabled = False
    Me.AcceptButton = Button1
End Sub

End Class

1条回答
祖国的老花朵
2楼-- · 2019-07-21 10:14

the problem is after you press button 1 button 3 gets focus.
you could fix it by adding code to focus to the button you need in the button 1 click event. "Button2.Focus()" etc..

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MessageBox.Show("Button 1 pressed!")
    GroupBox1.Enabled = False
    GroupBox2.Enabled = True
    Me.AcceptButton = Button2
    Button2.Focus()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    MessageBox.Show("Button 2 pressed!")
    GroupBox1.Enabled = True
    GroupBox2.Enabled = False
    Me.AcceptButton = Button1
    Button1.Focus()
End Sub
查看更多
登录 后发表回答