Export (0) Print
Expand All

Control.SetBounds Method

Sets the bounds of the control.

Overload List

Sets the bounds of the control to the specified location and size.

[Visual Basic] Overloads Public Sub SetBounds(Integer, Integer, Integer, Integer)
[C#] public void SetBounds(int, int, int, int);
[C++] public: void SetBounds(int, int, int, int);
[JScript] public function SetBounds(int, int, int, int);

Sets the specified bounds of the control to the specified location and size.

[Visual Basic] Overloads Public Sub SetBounds(Integer, Integer, Integer, Integer, BoundsSpecified)
[C#] public void SetBounds(int, int, int, int, BoundsSpecified);
[C++] public: void SetBounds(int, int, int, int, BoundsSpecified);
[JScript] public function SetBounds(int, int, int, int, BoundsSpecified);

Example

[Visual Basic, C#, C++] The following example centers a Form on the screen in the Layout event. This will keep the form centered as the user resizes it. This example assumes you have created a Form control.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of SetBounds. For other examples that might be available, see the individual overload topics.
[Visual Basic] 

    Private Sub MyForm_Layout(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.LayoutEventArgs) Handles MyBase.Layout

        ' Center the Form on the user's screen everytime it requires a Layout.
        Me.SetBounds((System.Windows.Forms.Screen.GetBounds(Me).Width / 2) - (Me.Width / 2), _
            (System.Windows.Forms.Screen.GetBounds(Me).Height / 2) - (Me.Height / 2), _
            Me.Width, Me.Height, System.Windows.Forms.BoundsSpecified.Location)
    End Sub

[C#] 
private void MyForm_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
{
   // Center the Form on the user's screen everytime it requires a Layout.
   this.SetBounds((Screen.GetBounds(this).Width/2) - (this.Width/2),
       (Screen.GetBounds(this).Height/2) - (this.Height/2),
       this.Width, this.Height, BoundsSpecified.Location);    
}

[C++] 
private:
    void MyForm_Layout(Object* /*sender*/, System::Windows::Forms::LayoutEventArgs* /*e*/) {
        // Center the Form on the user's screen everytime it requires a Layout.
        this->SetBounds((Screen::GetBounds(this).Width/2) - (this->Width/2),
            (Screen::GetBounds(this).Height/2) - (this->Height/2),
            this->Width, this->Height, BoundsSpecified::Location);    
    }

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

See Also

Control Class | Control Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft