Finch
Finch

Reputation: 73

Bug while inserting button form control onto Excel Worksheet

I’m trying to insert a simple Button form control onto worksheet within a Excel VSTO Addin. This code works, but the button it offset down and to the right (as you can see in image below). Any clue why this is happening and how I can fix it? Thanks!

Microsoft.Office.Tools.Excel.Worksheet vstoWorksheet = Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]);
System.Windows.Forms.Button button1 = vstoWorksheet.Controls.AddButton(ws.Range["$B$2:$D$6"], "button1");
button1.Left = 50;
button1.Top = 50;
button1.FlatAppearance.BorderSize = 0;
button1.MouseClick += Button1_MouseClick;
button1.Text = "OK";
button1.BackColor = System.Drawing.Color.Red;
button1.FlatStyle = FlatStyle.Flat;

enter image description here

Upvotes: 0

Views: 37

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

In the code you are setting the location by specifying the following properties:

button1.Left = 50;
button1.Top = 50;

Try to not set them to any value or just comment these two lines of code and the offset will be removed.

Upvotes: 1

Related Questions