Leer en inglés

Compartir a través de


Button.Top Property

Definition

Gets or sets the distance, in points, between the top edge of the Button and the top edge of the worksheet.

C#
public double Top { get; set; }

Property Value

The distance, in points, between the top edge of the Button and the top edge of the worksheet.

Examples

The following code example compares the resizing and relocating behavior of two Button controls to a worksheet. The Click event handler of the first button adjusts the Height and Width properties of the button so that the button changes size, but remains in the same location on the worksheet. The Click event handler of the second button adjusts the Top and Left properties of the button so that the button changes location on the worksheet, but stays the same size.

This example is for a document-level customization.

C#
private void ModifySizeAndLocation()
{
    Microsoft.Office.Tools.Excel.Controls.Button sizeButton =
        this.Controls.AddButton(25, 30, 100, 25,
        "sizeButton");
    sizeButton.Name = "sizeButton";
    sizeButton.Text = "Click to resize";
    sizeButton.Click += new EventHandler(pointButtons_Click);

    Microsoft.Office.Tools.Excel.Controls.Button locationButton =
        this.Controls.AddButton(25, 150, 100, 25,
        "locationButton");
    locationButton.Name = "locationButton";
    locationButton.Text = "Click to move";
    locationButton.Click += new EventHandler(pointButtons_Click);
}

// Represents the toggle states of the buttons.
bool toggleState1 = true;
bool toggleState2 = true;

void pointButtons_Click(object sender, EventArgs e)
{
    Microsoft.Office.Tools.Excel.Controls.Button clickedButton =
        (Microsoft.Office.Tools.Excel.Controls.Button)sender;

    // Toggle the Height and Width of sizeButton.
    if (clickedButton.Name == "sizeButton")
    {
        if (toggleState1)
        {
            clickedButton.Height = 40;
            clickedButton.Width = 80;
            toggleState1 = false;
        }
        else
        {
            clickedButton.Height = 25;
            clickedButton.Width = 100;
            toggleState1 = true;
        }
    }

    // Toggle the Left and Top of locationButton.
    else
    {
        if (toggleState2)
        {
            clickedButton.Left = 50;
            clickedButton.Top = 100;
            toggleState2 = false;
        }
        else
        {
            clickedButton.Left = 25;
            clickedButton.Top = 150;
            toggleState2 = true;
        }
    }
}

Remarks

The Microsoft.Office.Tools.Excel.Controls.Button.Top property uses points, whereas System.Windows.Forms.Control.Top property uses pixels.

Applies to

Producto Versiones
Visual Studio Tools for Office 2017, 2019, 2022