为什么我的基本默认.acceptbutton不起作用?

时间:2014-10-20 16:37:42

标签: vb.net visual-studio

我拥有的:

我有两个组合框,每组都有一个文本框。第三个文本框放在两个组框之外。

enter image description here

按钮1是表单加载时的默认接受按钮。

我需要什么:

单击按钮1(或按下输入键)时,我需要按钮2成为默认接受按钮。

我的问题:

按钮3成为默认接受按钮,而不是按钮2,尽管我的代码。

我的代码:

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 个答案:

答案 0 :(得分:2)

问题是按下按钮后1按钮3获得焦点。
您可以通过添加代码来修复它,以便在按钮1单击事件中聚焦到您需要的按钮。 “Button2.Focus()”等。

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