C# 在已经绘制后移动按钮的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11214033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:59:56  来源:igfitidea点击:

Move a button's location after it is already drawn

c#winforms

提问by fraXis

On my C# Winform (VS 2010 / C# / .Net 3.5), I have created a button in the designer. I want to move and resize that button to a different location based on a user's setting upon start-up of that form.

在我的 C# Winform (VS 2010 / C# / .Net 3.5) 上,我在设计器中创建了一个按钮。我想根据用户在该表单启动时的设置将该按钮移动和调整大小到不同的位置。

In my form's 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 of the code to create the button is *.designer.cs file for this particular form.

创建按钮的所有代码都是用于此特定表单的 *.designer.cs 文件。

The problem is this: When the form loads, I can see the button in it's new location based on the 3 lines of code above. But then when the form is done loading and going through all it's events, the button goes back to it's original location which is in the *.designer.cs InitalizeComponent() method.

问题是这样的:当表单加载时,我可以根据上面的 3 行代码在它的新位置看到按钮。但是当表单完成加载并完成所有事件时,按钮会返回到它在 *.designer.cs InitalizeComponent() 方法中的原始位置。

I do not want to take the code out of the *.designer.cs file and put it only into the form's .cs file because I still want to be able to see the button in the designer when I work on the design of the form.

我不想将代码从 *.designer.cs 文件中取出并仅放入表单的 .cs 文件中,因为我仍然希望在设计表单时能够看到设计器中的按钮.

I just want to move and resize the button if the user has that option toggled upon loading of the form.

如果用户在加载表单时切换了该选项,我只想移动和调整按钮的大小。

How can I do this since .Net seems to draw the buttons on my form after the load event has processed thus moving the button back to it's original spot?

我该怎么做,因为 .Net 似乎在加载事件处理后在我的表单上绘制按钮,从而将按钮移回原来的位置?

采纳答案by Sam I am says Reinstate Monica

Here's some code I just spiked together. and It works for me.

这是我刚刚拼凑的一些代码。它对我有用。

This is a SSCCE, a Simple, Self-Contained Correct Example. Very useful when asking questions on SO.

这是一个 SSCCE,一个简单的、自包含的正确示例。在问关于 SO 的问题时非常有用。

*Since I don't know what your specific problem is, the Correct, part might not be accurate in this case

*由于我不知道您的具体问题是什么,在这种情况下,正确的部分可能不准确

Try and make a copy of this project, and see if you experience the same issue. If you don't than We really can't help you that much without getting more information about your project.

尝试复制此项目,看看您是否遇到相同的问题。如果您不这样做,我们真的无法在没有获得有关您的项目的更多信息的情况下为您提供太多帮助。

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);
        }
    }
}