Move button location after it is already drawn

In my C # Winform (VS 2010 / C # /. Net 3.5) I created a button in the designer. I want to move and resize this button elsewhere based on user preferences when this form is run.

In my form load event, I have the following code to move and resize the button:

btnShare.Location = new System.Drawing.Point(16, 496);
btnShare.Margin = new System.Windows.Forms.Padding(4);
btnShare.Size = new System.Drawing.Size(408, 126);

All the code for creating the button is the * .designer.cs file for this particular form.

The problem is this: when the form loads, I see a button in it of a new location based on the three lines of code above. But then, when the form is loaded and goes through all the events, the button will return to its original location, which is located in the * .designer.cs InitalizeComponent () method.

I don’t want to output the code from the * .designer.cs file and put it only in the .cs file, because I still want to see the button in the designer when I work on the form design.

I just want to move and resize the button if the user has this option enabled when the form loads.

How can I do this, since .Net seems to draw buttons on my form after the load event has been processed, thus moving the button back to its original location?

+5
source share
1 answer

Here is the code I just flashed. and it works for me.

This is SSCCE, a simple, self-preserving correct example. Very helpful when asking questions on SO.

* Since I do not know what your specific problem is, in this case the wrong part may be inaccurate.

, . , , .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            button1.Location = new Point(40, 40);
        }
    }
}
+7

All Articles