Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

c# - 计时器在C#Visual Studio 2019中疯了吗?(Has timer gone beserk in C# Visual Studio 2019?)

To keep it simple. (为了简单起见。) In VS2019 I have created 1 form (frm1) with dimensions 500x500 and a button. (在VS2019中,我创建了1个尺寸为500x500的表单(frm1)和一个按钮。) I create a Windows.Forms.Timer timer1, set its interval to 10. Upon pushing the button I enable timer1 and tell frm1 to move to the Left for 500px (equal to its width) (我创建一个Windows.Forms.Timer timer1,将其间隔设置为10。按下按钮后,我启用timer1并告诉frm1向左移动500px(等于其宽度))

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

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 10;
            if (this.Left > 500) 
            {
                timer1.Enabled = false; 
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }


    }
}

However, when I run the code frm1 only moves to the left 10px as if the timer has only ticked once. (但是,当我运行代码frm1时,它只会向左移动10px,就好像计时器只被勾选了一次一样。) I have to add something like >= 1000 to get it to cover the whole length of frm1 (500px wide), which doesnt make sense. (我必须添加> = 1000之类的内容才能覆盖frm1(宽500px)的整个长度,这没有任何意义。) Any ideas?? (有任何想法吗??)

  ask by Dimitris Platis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Left is a property inherited from Control . (Left是从Control继承的属性 。) It deals with the position relative to the "containing" Control. (它处理相对于“包含”控件的位置。) Stuff like the position of a button on the Window. (类似于窗口上按钮的位置。) I am surprised it even has any effect on the Window, wich is the topmost container (so no apparent parent to move it relative too). (我很惊讶它甚至对Window都没有影响,因为它是最顶层的容器(因此也没有明显的父级来相对移动它)。)

As I understand it, you want to change the screen Location of the Form. (据我了解,您想更改表单的屏幕位置 。) That is a case for Location . (对于Location就是这种情况。)

However I feel the need to point out, that such things are not considered good user experience. (但是,我觉得有必要指出,这种事情不被认为是良好的用户体验。) Location should only be used to retrieve and store the location between runs of the Application. (位置仅应用于检索和存储应用程序两次运行之间的位置。) Not Animations of moving the Window. (不是移动窗口的动画。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...