VB.Net - Combobox的Textchanged事件上的Keypress事件

时间:2017-01-11 01:03:12

标签: vb.net winforms visual-studio-2013

我有这个VB.net程序,我需要更改文本框'基于组合框的textchanged事件的按键条件。

例如,如果组合框中的所选项目包含 " 0-0-50"那么文本框必须接受十进制数字,否则只接受整数。

这是我的代码而且它无法正常工作,我也不知道错误:

Private Sub cboFertilizer_TextChanged(sender As Object, e As EventArgs) Handles cboFertilizer.TextChanged

    Try
        g_SqlSTR = <string>
                       SELECT FAID, [FertilizerType], [Issued], [ReturnedTransfered], [Used], [UsedRemarks]
                        ,[ReturnTransfer], [ReturnTransferRemarks], [Sold], [SoldRemarks], [UsedInOtherCrops]
                        ,[UsedInOtherCropsRemarks], [Others], [OthersRemarks], [ChildLabor]
                    FROM Buying.tFertilizerAuditDetails
                    WHERE FertilizerType = '<%= cboFertilizer.Text %>' AND FAID = '<%= _FAID %>'
                   </string>
        ExecuteSQLQuery(g_SqlSTR)
        If g_SqlDT.Rows.Count <> 0 Then
            txtIssued.Text = g_SqlDT.Rows(0)("Issued")
            txtReturned.Text = g_SqlDT.Rows(0)("ReturnedTransfered")
            txtUsed.Text = g_SqlDT.Rows(0)("Used")
            txtRemarksUsed.Text = g_SqlDT.Rows(0)("UsedRemarks")
            txtReturn.Text = g_SqlDT.Rows(0)("ReturnTransfer")
            txtRemarksReturn.Text = g_SqlDT.Rows(0)("ReturnTransferRemarks")
            txtSold.Text = g_SqlDT.Rows(0)("Sold")
            txtRemarksSold.Text = g_SqlDT.Rows(0)("SoldRemarks")
            txtUsedInCrops.Text = g_SqlDT.Rows(0)("UsedInOtherCrops")
            txtRemarksUsedInCrops.Text = g_SqlDT.Rows(0)("UsedInOtherCropsRemarks")
            txtOthers.Text = g_SqlDT.Rows(0)("Others")
            txtRemarksOthers.Text = g_SqlDT.Rows(0)("OthersRemarks")

            If g_SqlDT.Rows(0)("ChildLabor").ToString = "True" Then
                rYes.Checked = True
            ElseIf g_SqlDT.Rows(0)("ChildLabor").ToString = "False" Then
                rNo.Checked = True
            End If
        Else
            ClearTextBoxes()
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    If cboFertilizer.Text.Contains("0-0-50") Then
        AddHandlerDecimal()
    Else
        AddHandlerWholeNo()
    End If

End Sub

这是处理程序:

Public Sub AddHandlerDecimal()
    AddHandler txtIssued.KeyPress, AddressOf WholenumbersAndDecimalonlywithoutNegative
    AddHandler txtReturned.KeyPress, AddressOf WholenumbersAndDecimalonlywithoutNegative
    AddHandler txtUsed.KeyPress, AddressOf WholenumbersAndDecimalonlywithoutNegative
    AddHandler txtReturn.KeyPress, AddressOf WholenumbersAndDecimalonlywithoutNegative
    AddHandler txtSold.KeyPress, AddressOf WholenumbersAndDecimalonlywithoutNegative
    AddHandler txtUsedInCrops.KeyPress, AddressOf WholenumbersAndDecimalonlywithoutNegative
    AddHandler txtOthers.KeyPress, AddressOf WholenumbersAndDecimalonlywithoutNegative

End Sub

Public Sub AddHandlerWholeNo()
    AddHandler txtIssued.KeyPress, AddressOf Wholenumbersonlywithoutnegative
    AddHandler txtReturned.KeyPress, AddressOf Wholenumbersonlywithoutnegative
    AddHandler txtUsed.KeyPress, AddressOf Wholenumbersonlywithoutnegative
    AddHandler txtReturn.KeyPress, AddressOf Wholenumbersonlywithoutnegative
    AddHandler txtSold.KeyPress, AddressOf Wholenumbersonlywithoutnegative
    AddHandler txtUsedInCrops.KeyPress, AddressOf Wholenumbersonlywithoutnegative
    AddHandler txtOthers.KeyPress, AddressOf Wholenumbersonlywithoutnegative

End Sub

如果我将处理程序放在表单载荷上,处理程序就可以工作,但是我需要将它插入到组合框的textchanged事件中以及&#34; 0-0-50&#34;验证。 感谢

1 个答案:

答案 0 :(得分:0)

我使用了每个文本框的按键事件。

Private Sub txtIssued_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtIssued.KeyPress, txtUsedInCrops.KeyPress, txtUsed.KeyPress, txtSold.KeyPress, txtReturned.KeyPress, txtReturn.KeyPress, txtOthers.KeyPress

If e.KeyChar <> ControlChars.Back Then

    If cboFertilizer.Text.Contains("0-0-50") Then
        WholenumbersAndDecimalonlywithoutNegative(sender, e)
    Else
        Wholenumbersonlywithoutnegative(sender, e)
    End If

End If
End Sub