繁体   English   中英

VB.Net - Combobox 的 Textchanged 事件上的按键事件

[英]VB.Net - Keypress event on Combobox's Textchanged event

我有这个 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 事件以及“0-0-50”验证。 谢谢

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM