Activates the form and gives it focus.
style=3D"DISPLAY: none">none">[Visual Basic]
Public Sub Activate()
style=3D"DISPLAY: none">[C#]
public href=3D"ms-help://MS.MSDNQTR.2004APR.1033/cpref/html/frlrfsystemvoidclass=
topic.htm">void Activate();
style=3D"DISPLAY: none">[C++]
public: href=3D"ms-help://MS.MSDNQTR.2004APR.1033/cpref/html/frlrfsystemvoidclass=
topic.htm">void Activate();
style=3D"DISPLAY: none">[JScript]
public function Activate();
Remarks
Activating a form brings it to the front if this is the active =
application,=20
or it flashes the window caption if this is not the active application. =
The form=20
must be visible for this method to have any effect. To determine the =
active form=20
in an application, use the href=3D"ms-help://MS.MSDNQTR.2004APR.1033/cpref/html/frlrfsystemwindowsfo=
rmsformclassactiveformtopic.htm">ActiveForm=20
property or the href=3D"ms-help://MS.MSDNQTR.2004APR.1033/cpref/html/frlrfsystemwindowsfo=
rmsformclassactivemdichildtopic.htm">ActiveMdiChild=20
property if your forms are in a Multiple Document Interface (MDI)=20
application.
Example
style=3D"DISPLAY: none">[Visual Basic, C#] The =
following code=20
example demonstrates how to use the href=3D"ms-help://MS.MSDNQTR.2004APR.1033/cpref/html/frlrfsystemwindowsfo=
rmsformclasssetdesktoplocationtopic.htm">SetDesktopLocation,=20
href=3D"ms-help://MS.MSDNQTR.2004APR.1033/cpref/html/frlrfsystemwindowsfo=
rmsformclassloadtopic.htm">Load=20
and Activate members. To run the example, paste the following =
code in a=20
form called Form1 containing a button called Button1 and two Label =
controls=20
called Label1 and Label2. Ensure all events are connected to their=20
event-handling methods.
none">none">[Visual Basic]=20
Shared x As Integer =3D 200
Shared y As Integer =3D 200
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Create a new Form1 and set its Visible property to true.
Dim form2 As New Form1
form2.Visible =3D True
' Set the new form's desktop location so it appears below and=20
' to the right of the current form.
form2.SetDesktopLocation(x, y)
x +=3D 30
y +=3D 30
' Keep the current form active by calling the Activate method.
Me.Activate()
Me.Button1.Enabled =3D False
End Sub
' Updates the label text to reflect the current values of x and y,=20
' which was were incremented in the Button1 control's click event.
Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
Label1.Text =3D "x: " & x & " y: " & y
Label2.Text =3D "Number of forms currently open: " & count
End Sub
Shared count As Integer =3D 0
Private Sub Form1_Closed(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Closed
count -=3D 1
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
count +=3D 1
End Sub
style=3D"DISPLAY: none">[C#]=20
static int x =3D 200;
static int y =3D 200;
private void Button1_Click(System.Object sender,=20
System.EventArgs e)
{
// Create a new Form1 and set its Visible property to true.
Form1 form2 =3D new Form1();
form2.Visible =3D true;
// Set the new form's desktop location so it =20
// appears below and to the right of the current form.
form2.SetDesktopLocation(x, y);
x +=3D 30;
y +=3D 30;
// Keep the current form active by calling the Activate
// method.
this.Activate();
this.Button1.Enabled =3D false;
}
// Updates the label text to reflect the current values of x=20
// and y, which was were incremented in the Button1 control's=20
// click event.
private void Form1_Activated(object sender, System.EventArgs e)
{
Label1.Text =3D "x: "+x+" y: "+y;
Label2.Text =3D "Number of forms currently open: "+count;
}
static int count =3D 0;
private void Form1_Closed(object sender, System.EventArgs e)
{
count -=3D 1;
}
private void Form1_Load(object sender, System.EventArgs e)
{
count +=3D 1;
}