Export (0) Print
Expand All
Expand Minimize

Control.Handle Property

Gets the window handle that the control is bound to.

[Visual Basic]
Public Overridable ReadOnly Property Handle As IntPtr  Implements _
   IWin32Window.Handle
[C#]
public virtual IntPtr Handle {get;}
[C++]
public: __property virtual IntPtr get_Handle();
[JScript]
public function get Handle() : IntPtr;

Property Value

An IntPtr that contains the window handle (HWND) of the control.

Implements

IWin32Window.Handle

Remarks

The value of the Handle property is a Windows HWND. If the handle has not yet been created, referencing this property will force the handle to be created.

Example

[Visual Basic, C#] The following code example demonstrates using the DrawFocusRectangle method and the Handle property. To run the example paste the following code in a form. Add two buttons named Button1 and Button2 to the form and ensure all events are connected to their event-handling methods.

[Visual Basic] 
' This method draws a focus rectangle on Button2 using the 
' handle and client rectangle of Button2.
Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    ControlPaint.DrawFocusRectangle(Graphics.FromHwnd(Button2.Handle), _
    Button2.ClientRectangle)
End Sub

[C#] 
// This method draws a focus rectangle on Button2 using the 
// handle and client rectangle of Button2.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
    ControlPaint.DrawFocusRectangle(Graphics.FromHwnd(Button2.Handle), 
        Button2.ClientRectangle);
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Control Class | Control Members | System.Windows.Forms Namespace | CreateHandle | RecreateHandle | IsHandleCreated

Show:
© 2014 Microsoft