Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
HtmlElement Class
HtmlElement Events
 GotFocus Event
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
HtmlElement..::.GotFocus Event

Occurs when the element has received user input focus.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic
Public Event GotFocus As HtmlElementEventHandler
C#
public event HtmlElementEventHandler GotFocus
Visual C++
public:
 event HtmlElementEventHandler^ GotFocus {
    void add (HtmlElementEventHandler^ value);
    void remove (HtmlElementEventHandler^ value);
}
F#
member GotFocus : IEvent<HtmlElementEventHandler,
    HtmlElementEventArgs>

You can neither cancel this event's default behavior, nor prevent it from bubbling. To remove focus from an element, call Focus on a different element from within the GotFocus event.

For more information about the difference between canceling event bubbling and canceling the default action on an event, see About the DHTML Object Model.

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>

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. The example requires that the previously mentioned HTML file is loaded into an instance of the WebBrowser control named WebBrowser1.

Visual Basic
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
C#
        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.");
            }
        }

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker