HtmlElement.GotFocus 이벤트

정의

요소에서 사용자 입력 포커스를 받았을 때 발생합니다.

public:
 event System::Windows::Forms::HtmlElementEventHandler ^ GotFocus;
public event System.Windows.Forms.HtmlElementEventHandler GotFocus;
public event System.Windows.Forms.HtmlElementEventHandler? GotFocus;
member this.GotFocus : System.Windows.Forms.HtmlElementEventHandler 
Public Custom Event GotFocus As HtmlElementEventHandler 

이벤트 유형

예제

다음 HTML 코드를 파일에 저장하고 Windows Forms 프로젝트의 컨트롤에 WebBrowser 파일을 로드합니다.

<HTML>  
    <BODY>  
        <FORM name="form1">  
            <INPUT type="text" size=20 name="text1">  
            <INPUT type="text" size=20 name="text2">  
            <INPUT type="text" size=20 name="text3">  
        </FORM>  
    </BODY>  
</HTML>  

다음 코드 예제에서는 이전 요소에 5자 미만이 포함된 경우 탭 순서의 다음 INPUT 요소가 사용자 입력 포커스를 받지 못하도록 합니다. 이 예제에서는 앞에서 언급한 HTML 파일을 라는 WebBrowser1컨트롤의 WebBrowser instance 로드해야 합니다.

HtmlElement targetFormElement;

private void HandleFormFocus()
{
    if (webBrowser1.Document != null)
    {
        HtmlDocument doc = webBrowser1.Document;
        if (doc.Forms.Count > 0)
        {
            HtmlElement targetForm = doc.Forms[0];
            HtmlElementCollection searchCollection = targetForm.All.GetElementsByName("text1");
            if (searchCollection.Count == 1)
            {
                targetFormElement = searchCollection[0];
            }
        }
    }
}

private void TargetFormElement_Focus(Object sender, HtmlElementEventArgs e)
{
    HtmlElement textElement = e.FromElement;
    String elementText = textElement.GetAttribute("value");

    // Check that this value is at least five characters long.
    if (elementText.Length < 5)
    {
        e.ReturnValue = true;
        MessageBox.Show("The entry in the current field must be at least five characters long.");
    }
}
Dim WithEvents TargetFormElement As HtmlElement

Private Sub HandleFormFocus()
    If (WebBrowser1.Document IsNot Nothing) Then
        With WebBrowser1.Document
            If (.Forms.Count > 0) Then
                Dim TargetForm As HtmlElement = .Forms(0)
                Dim SearchCollection As HtmlElementCollection = TargetForm.All.GetElementsByName("text1")
                If (SearchCollection.Count = 1) Then
                    TargetFormElement = SearchCollection(0)
                End If
            End If
        End With
    End If
End Sub

Private Sub TargetFormElement_Focus(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
    Dim TextElement As HtmlElement = e.FromElement
    Dim ElementText As String = TextElement.GetAttribute("value")

    ' Check that this value is at least five characters long.
    If (ElementText.Length < 5) Then
        e.ReturnValue = True
        MessageBox.Show("The entry in the current field must be at least five characters long.")
    End If
End Sub

설명

이 이벤트의 기본 동작을 취소하거나 버블링을 방지할 수 없습니다. 요소에서 포커스를 제거하려면 이벤트 내에서 다른 요소를 호출 Focus 합니다 GotFocus .

적용 대상

추가 정보