2010-06-04 5 views
0

이 함수는 텍스트 상자 (또는 콤보 박스 또는 마스크 된 텍스트 상자) 컨트롤의 내용을 평가하고 KeyPress 이벤트와 함께 입력을 허용하거나 금지합니다. 숫자 입력 만 유효한 날짜 또는 텍스트 상자에 유용합니다. 함수 호출에서 지정된 경우 소수점 이하 자릿수를 설정할 수 있습니다. 또한 텍스트 상자가 꽉 찬 경우 백 스페이스 문자를 허용합니다.선택한 텍스트를 고려하기 위해 텍스트 유효성 검사 함수를 적용하십시오.

텍스트 상자가 가득 차 있지만 하나 이상의 문자가 강조 표시되어있을 때 입력 할 수 있도록하고 싶습니다. (따라서 키 누르기 문자로 바꿀 수 있습니다.) ?

''' <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 

답변

0

당신은 매개 변수 (intLengthOfHighlightedText 및 intLengthOfControl)의 몇 가지를 추가하고 Textbox.SelectedText 및 TextboxName.MaxLength는 (한 줄 텍스트 컨트롤을 가정) 전달할 수 있습니다하십시오. 그런 다음 당신은 그렇지 않으면. 함수에서 그 해결 전체 텍스트 상자 컨트롤의 값을 전달할 수 있습니다 (오버로드 또는 콤보 상자 작업 등을 수행 할 수 있습니다).

+0

난 그냥 텍스트 상자에 전달의 아이디어처럼. 아마도 ComboBox 또는 MTB를 텍스트 상자로 캐스팅 할 수 있습니까? –

+0

Comboboxes는 콜렉션이므로 캐스팅에 대해서는 알지 못합니다. 컨트롤을 전달하려면 함수가 오버로드되도록하십시오 (다른 유형의 매개 변수를 지원하는 프로 시저/함수). 또는이 함수가 일반 컨트롤 유형을 허용하고 리플렉션을 사용하여 텍스트 속성을 가져올 수 있습니다. – N0Alias

관련 문제