문제

WinForm에서 텍스트 상자에 대한 유효성 검사 규칙을 구현했으며 잘 작동합니다. 그러나 필드에서 탭 할 때만 유효성 검사를 확인합니다. 상자에 무엇이든 입력 되 자마자 콘텐츠가 변경 될 때마다 확인하고 싶습니다. 또한 WinForm이 열리는 즉시 검증을 확인하고 싶습니다.

나는 몇 가지 이벤트와 무엇을 설정함으로써 최근에 이것을하는 것을 기억하지만, 어떻게 기억할 수없는 것 같습니다.

도움이 되었습니까?

해결책

TextChanged 이벤트

앞으로 MSDN 라이브러리에서 모든 이벤트를 찾을 수 있습니다. 텍스트 상자 클래스 참조:

http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox(vs.80).aspx

다른 팁

Databinding을 사용하는 경우 TextBox의 속성으로 이동하십시오. 맨 위에서 열린 (Databindings), (고급) 속성을 클릭하면 3 개의 점이 나타납니다 (...) 클릭하십시오. 고급 데이터 바인딩 화면이 나타납니다. 바인딩 된 텍스트 상자의 각 속성에 대해 귀하의 경우 Text, Databinding 및 검증이 Combobox를 사용하여 "Kick In"을 설정할 때 설정할 수 있습니다. Data Source Update mode. 당신이 그것을 설정했다면 OnPropertyChanged, 입력 할 때 다시 평가됩니다 (기본값은 OnValidation 탭으로 만 업데이트).

완료되지 않은 경우 데이터가 어떻게 유효합니까? 즉, 사용자가 숫자를 입력하고 날짜로 검증하고 검증하려고합니까?

텍스트 상자를 BindingSource에 바인딩 할 때 고급으로 이동하여 유효성 검사 유형을 선택합니다.
"재산이 변경되었습니다". 이렇게하면 각 키 프레스에서 데이터를 엔티티로 전파합니다.다음은 스크린 샷입니다

TextChanged 이벤트뿐만 아니라 Keypress 또는 Keydown 이벤트를 확인해야합니다.

다음은 C# 예제입니다 MSDN 문서:

// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
    //If shift key was pressed, it's not a number.
    if (Control.ModifierKeys == Keys.Shift) {
        nonNumberEntered = true;
    }
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top