HtmlElement.GotFocus HtmlElement.GotFocus HtmlElement.GotFocus HtmlElement.GotFocus Event

定義

要素がユーザーの入力フォーカスを得たときに発生します。Occurs when the element has received user input focus.

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 コードを保存し、ファイルをWebBrowserWindows フォーム プロジェクトで制御します。Save the following HTML code into a file, and load the file into a WebBrowser control in a Windows Forms project.

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

次のコード例は、[次へ] を防止INPUT直前の要素には、5 つ未満の文字が含まれている場合、ユーザーの入力フォーカスを受け取らないタブ オーダー内の要素。The following code example prevents the next INPUT element in the tab order from receiving user input focus if the previous element contains less than five characters. 例では、前に説明した HTML ファイルがのインスタンスに読み込まれている必要があります、WebBrowserという名前のコントロールWebBrowser1します。The example requires that the previously mentioned HTML file is loaded into an instance of the WebBrowser control named 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

注釈

このイベントの既定の動作をキャンセルも、バブルを防止します。You can neither cancel this event's default behavior, nor prevent it from bubbling. フォーカスを要素から削除するには、呼び出すFocus内から別の要素で、GotFocusイベント。To remove focus from an element, call Focus on a different element from within the GotFocus event.

適用対象

こちらもご覧ください