0
我有這個功能,其與按鍵事件評估的文本框(或組合框或MaskedTextBox中)控制的內容,並結合地,將允許或禁止輸入端。對於只有數字輸入有效的日期或文本框非常有用。如果在函數調用中指定,它允許一位小數點後的一組數字。如果文本框已滿,它也允許退格字符。適應文本驗證功能考慮所選文本
我希望它允許我輸入什麼,否則將有效的文本時,文本框已滿,但一個或多個字符突出顯示(因此將被按鍵字符替換。任何人都可以告訴我如何做到這一點好嗎?
''' <summary>
''' Validates that input is numeric.
''' Allows one decimal place unless intDecimal is less than 1
''' Will allow a set number of numbers after the decimal place.
''' </summary>
''' <param name="strKeyPress">The key which has been pressed</param>
''' <param name="strText">Current text of the textbox</param>
''' <param name="intPosition">Current cursor position in textbox</param>
''' <param name="intDecimal">Number of decimal places desired.</param>
''' <returns>Boolean: true means that input is numeric, false means it is not.</returns>
''' <remarks>Used with a keypress event to validate input. Do not handle input if function returns false.</remarks>
Public Function InputIsNumeric(ByVal strKeyPress As String, ByVal strText As String, ByVal intPosition As Integer, ByVal intDecimal As Integer) As Boolean
Dim dot As Integer
Dim ch As String
Dim returnValue As Boolean
If Not Char.IsDigit(CType(strKeyPress, Char)) Then returnValue = True
If strKeyPress = "-" And intPosition = 0 Then returnValue = False 'allow negative number
If strKeyPress = "." And strText.IndexOf(".") = -1 And intDecimal > 0 Then returnValue = False 'allow single decimal point
dot = strText.IndexOf(".")
If dot > -1 Then 'limit to set decimal places
ch = strText.Substring(dot + 1)
If ch.Length > (intDecimal - 1) Then returnValue = True
End If
If strKeyPress = Chr(8) Then returnValue = False 'allow Backspace
Return returnValue
End Function
我喜歡只是在文本框中傳遞的想法。我可能只是將一個ComboBox或MTB作爲一個文本框,對吧? – 2010-06-04 19:38:55
我不知道鑄件,因爲組合框的集合。如果你要通過控制,使重載函數(一個過程/函數,支持不同類型的參數)。或者你可以有功能接受一般控制類型和使用反射來獲取文本屬性。 – N0Alias 2010-06-05 01:23:01