HtmlElement.GotFocus Událost

Definice

Nastane, když element přijal fokus vstupu uživatele.

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 

Event Type

Příklady

Uložte následující kód HTML do souboru a načtěte soubor do WebBrowser ovládacího prvku v projektu model Windows Forms.

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

Následující příklad kódu zabraňuje dalšímu INPUT prvku v pořadí karet přijímat fokus vstupu uživatele, pokud předchozí prvek obsahuje méně než pět znaků. Příklad vyžaduje, aby byl dříve uvedený soubor HTML načten do instance WebBrowser ovládacího prvku s názvem WebBrowser1.

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

Poznámky

Výchozí chování této události nelze zrušit ani zabránit jejímu probublávání. Pokud chcete odebrat fokus z elementu, zavolejte Focus jiný prvek z GotFocus události.

Platí pro

Viz také