다음을 통해 공유


ContentControlBase.Validating 이벤트

콘텐츠 컨트롤의 내용에 대해 유효성을 검사할 때 발생합니다.

네임스페이스:  Microsoft.Office.Tools.Word
어셈블리:  Microsoft.Office.Tools.Word(Microsoft.Office.Tools.Word.dll)

구문

‘선언
Event Validating As CancelEventHandler
event CancelEventHandler Validating

설명

Validating 이벤트는 컨트롤이 포커스를 잃을 때 발생합니다. Validating 이벤트를 처리하여 선택한 기준에 따라 콘텐츠 컨트롤의 텍스트가 유효한지 확인할 수 있습니다. 예를 들어, 전화 번호가 들어 있는 콘텐츠 컨트롤의 경우 이 컨트롤에 숫자, 괄호, 하이픈 등 적절한 문자만 있는지 확인할 수 있습니다. 내용이 유효하지 않으면 이벤트 처리기의 CancelEventArgs 매개 변수에서 Cancel 속성을 true로 설정하여 이벤트를 취소하고 포커스를 컨트롤로 되돌릴 수 있습니다. 이 속성을 설정하여 얻는 실제 효과는 올바른 텍스트를 입력할 때까지는 사용자가 컨트롤에서 이동할 수 없도록 하는 데 있습니다.

콘텐츠 컨트롤의 유효성 검사가 성공적으로 완료된 후 코드를 실행하려면 Validated 이벤트를 처리합니다.

이벤트 처리에 대한 자세한 내용은 이벤트 사용을 참조하십시오.

예제

다음 코드 예제에서는 Validating 및 Validated 이벤트의 이벤트 처리기를 보여 줍니다. 최종 사용자가 콘텐츠 컨트롤의 텍스트를 변경한 후에는 Validating 이벤트에 대한 이벤트 처리기가 정규식을 사용하여 해당 텍스트에 정수가 없는지 확인합니다.

이 예제에서는 문서에 plainTextContentControl1이라는 PlainTextContentControl이 포함되어 있다고 가정합니다. 이 코드를 사용하려면 프로젝트의 ThisDocument 클래스에 해당 코드를 붙여넣습니다. 또한 C#의 경우 plainTextContentControl1의 Validated 및 Validating 이벤트에 이벤트 처리기를 연결해야 합니다.

이 예제는 문서 수준 사용자 지정을 위한 것입니다.

Private Sub plainTextContentControl1_Validating(ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs) _
    Handles PlainTextContentControl1.Validating

    Dim control As Microsoft.Office.Tools.Word.PlainTextContentControl = _
        TryCast(sender, Microsoft.Office.Tools.Word.PlainTextContentControl)

    If control IsNot Nothing Then
        Dim regex As New System.Text.RegularExpressions.Regex("\d")
        If regex.IsMatch(control.Text) Then
            MessageBox.Show("Invalid name. Names cannot contain integers.")
            e.Cancel = True
        End If
    End If
End Sub

Private Sub plainTextContentControl1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles PlainTextContentControl1.Validated

    MessageBox.Show("The name is valid.")
End Sub
void plainTextContentControl1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
    Microsoft.Office.Tools.Word.PlainTextContentControl control =
        sender as Microsoft.Office.Tools.Word.PlainTextContentControl;

    if (control != null)
    {
        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\d");
        if (regex.IsMatch(control.Text))
        {
            MessageBox.Show("Invalid name. Names cannot contain integers.");
            e.Cancel = true;
        }
    }
}

void plainTextContentControl1_Validated(object sender, EventArgs e)
{
    MessageBox.Show("The name is valid.");
}

.NET Framework 보안

  • 직접 실행 호출자의 경우 완전히 신뢰합니다. 이 멤버는 부분적으로 신뢰할 수 있는 코드에서 사용할 수 없습니다. 자세한 내용은 부분 신뢰 코드에서 라이브러리 사용을 참조하십시오.

참고 항목

참조

ContentControlBase 인터페이스

Microsoft.Office.Tools.Word 네임스페이스