如何分配回车键?

时间:2018-12-13 00:41:34

标签: vb.net

我的代码:

Public Class Form1
    Dim attempt As Integer
    Private Sub BtnLogIn_Click(sender As Object, e As EventArgs) Handles BtnLogIn.Click
        If CBUser.Text = "Mineomyn" And TBPass.Text = "lemsilog" Then
            MsgBox("Log in successful!", MsgBoxStyle.Information)
            TBPass.Clear()
            GBAddons.Enabled = True
            GBPizza.Enabled = True
            RichTextBox1.Enabled = True
            BtnCalculate.Enabled = True
            BtnCompleteOrder.Enabled = True
            BtnClearOrder.Enabled = True
            TBAmount.Enabled = True
            TBChange.Enabled = True
            TBTotal.Enabled = True
            BtnChange.Enabled = True

        ElseIf CBUser.Text = "Lemsilog" And TBPass.Text = "lemsilog" Then
            MsgBox("Log in successful!", MsgBoxStyle.Information)
            TBPass.Clear()
            GBAddons.Enabled = True
            GBPizza.Enabled = True
            RichTextBox1.Enabled = True
            BtnCalculate.Enabled = True
            BtnCompleteOrder.Enabled = True
            BtnClearOrder.Enabled = True
            TBAmount.Enabled = True
            TBChange.Enabled = True
            TBTotal.Enabled = True
            BtnChange.Enabled = True

        ElseIf CBUser.Text = "Lemzipon" And TBPass.Text = "lemsilog" Then
            MsgBox("Log in successful!", MsgBoxStyle.Information)
            TBPass.Clear()
            GBAddons.Enabled = True
            GBPizza.Enabled = True
            RichTextBox1.Enabled = True
            BtnCalculate.Enabled = True
            BtnCompleteOrder.Enabled = True
            BtnClearOrder.Enabled = True
            TBAmount.Enabled = True
            TBChange.Enabled = True
            TBTotal.Enabled = True
            BtnChange.Enabled = True

        Else attempt = attempt + 1

        End If

        If attempt = 1 Then
            MsgBox("Incorrect user or pass. Attempts left: 2", MsgBoxStyle.Exclamation)
            TBPass.Clear()
            GBAddons.Enabled = False
            GBPizza.Enabled = False
            RichTextBox1.Enabled = False

        ElseIf attempt = 2 Then
            MsgBox("Incorrect user or pass. Attempts left: 1", MsgBoxStyle.Exclamation)
            TBPass.Clear()
            GBAddons.Enabled = False
            GBPizza.Enabled = False
            RichTextBox1.Enabled = False

        ElseIf attempt = 3 Then
            MsgBox("Incorrect user or pass. You have reached maximum attempts to log in. Try again later.", MsgBoxStyle.Critical)
            TBPass.Clear()
            GBAddons.Enabled = False
            GBPizza.Enabled = False
            RichTextBox1.Enabled = False
            BtnLogIn.Enabled = False
        End If
    End Sub

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

        GBPizza.Enabled = False
        GBAddons.Enabled = False
        RichTextBox1.Enabled = False
        BtnCalculate.Enabled = False
        BtnCompleteOrder.Enabled = False
        BtnClearOrder.Enabled = False
        TBAmount.Enabled = False
        TBChange.Enabled = False
        TBTotal.Enabled = False
        BtnChange.Enabled = False
    End Sub

    Private Sub calculate()
        Dim cost As Double
        Dim order As String

        If Radsmall.Checked Then
            cost = Radsmall.Tag
            order = "18 inch"

        ElseIf Radmedium.Checked Then
            cost = Radmedium.Tag
            order = "24 inch"

        Else
            cost = Radlarge.Tag
            order = "28 inch"
        End If

        If CB15Coke.Checked Then cost += CB15Coke.Tag
        If CB1Coke.Checked Then cost += CB1Coke.Tag
        If CBCrust.Checked Then cost += CBCrust.Tag
        If CBextracheese.Checked Then cost += CBextracheese.Tag
        If CBmushroom.Checked Then cost += CBmushroom.Tag
        If CBsingledrink.Checked Then cost += CBsingledrink.Tag

        TBTotal.Text = cost

        Dim display As String = "You order a " & order

        If ComboPizza.Text = "Pepperoni" Then display += " pepperoni pizza"
        If ComboPizza.Text = "4 Cheese" Then display += " 4 cheese pizza"
        If ComboPizza.Text = "All meat" Then display += " all meat pizza"
        If ComboPizza.Text = "Hawaiian" Then display += " hawaiian pizza"
        If ComboPizza.Text = "Classic Italian" Then display += " classic italian pizza"
        If ComboPizza.Text = "New York Style" Then display += " New York style pizza"
        If ComboPizza.Text = "Pure Beef" Then display += " pure beef pizza"

        If CB15Coke.Checked Then display += " with 1.5L Coke"
        If CB1Coke.Checked Then display += " with 1L Coke"
        If CBCrust.Checked Then display += " with tuff crust"
        If CBextracheese.Checked Then display += " with extra cheese"
        If CBmushroom.Checked Then display += " with mushroom"
        If CBsingledrink.Checked Then display += " with single drink"

        Me.RichTextBox1.Text = display

    End Sub

    Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
        calculate()
    End Sub

    Private Sub BtnCancelOrder_Click(sender As Object, e As EventArgs) Handles BtnClearOrder.Click
        TBAmount.Clear()
        TBChange.Clear()
        TBTotal.Clear()
        Radlarge.Checked = False
        Radmedium.Checked = False
        Radsmall.Checked = False
        CB15Coke.Checked = False
        CB1Coke.Checked = False
        CBCrust.Checked = False
        CBextracheese.Checked = False
        CBmushroom.Checked = False
        CBsingledrink.Checked = False
        ComboPizza.Text = ""
        Me.RichTextBox1.Clear()

    End Sub

    Private Sub BtnChange_Click(sender As Object, e As EventArgs) Handles BtnChange.Click

        TBChange.Text = Val(TBTotal.Text - TBAmount.Text)
    End Sub

    Private Sub BtnCompleteOrder_Click(sender As Object, e As EventArgs) Handles BtnCompleteOrder.Click
        MsgBox("Order is complete. Your order will be delivered to you within 45 minutes. Thank you!")

    End Sub
End Class

“计算订单”按钮将执行总订单。然后,我必须手动将金额输入“已付金额”文本框以进行更改。现在,我要做的就是分配回车键,这样我就可以在输入金额后按Enter键,而不必将鼠标悬停在“已付金额”按钮上。我不知道它的代码,应该在文本框或按钮上的哪个位置放置代码?我的“登录”按钮上已经使用了接受按钮属性。

1 个答案:

答案 0 :(得分:0)

您可以按以下方式覆盖表单的ProcessCmdKey函数:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
    If keyData = Keys.Enter AndAlso Me.TbAmount.Focused = True Then
        Me.BtnAmountPaid.PerformClick()
        Return True
    End If

    Return False
End Function