简体   繁体   中英

VB.Net - Keypress event on Combobox's Textchanged event

i have this VB.net program where i need to change the textboxes' keypress conditions based on a combobox's textchanged event.

For example, if the selected item on the combobox contains "0-0-50" then the textboxes must accept decimal numbers, else, only whole numbers.

this is my code and it's not working and i don't know what's wrong:

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

And this is the handlers:

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

The handlers work if I place it on form load, but I need to insert it on the combobox's textchanged event as well for the "0-0-50" validation. Thanks

I used the keypress event of each textboxes instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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