越过C#中的任何控件 [英] crossing any control in c#

查看:59
本文介绍了越过C#中的任何控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想找到如何随时间随机移动按钮的位置.

Hi all,

I would like to find how to randomly move the location of a button over time.

推荐答案

您的问题根本不清楚:表单中的任何控件都可以交叉我的表格"没有任何意义.请编辑您的问题,并尝试更详细地解释. Google翻译可能会在这里提供帮助.

随机时间位更容易应付:

在表单类中添加两个变量:
Your question is not at all clear: "any control in form can crossing in me form" does not make sense. Please edit you question and try to explain in more detail. Google Translate may be of assistance here.

The random time bit is easier to cope with:

In your form class add two variables:
private int tenthsOfSeconds = 0;
private Random randomGenerator = new Random();


在您的Load事件中(如果希望通过按钮启动,则单击按钮):


In your Load event (or button click if you want it started by a button):

Timer myTimer = new Timer();
myTimer.Interval = 100;       // 100 milliseconds == 1/10th of a second
tenthsOfSeconds = randomGenerator.Next(100);    // Random time, up to 10 seconds
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Start();

添加事件处理程序:

void myTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
    if (tenthsOfSeconds > 0)
        {
        tenthsOfSeconds--;
        if (tenthsOfSeconds == 0)
            {
            tenthsOfSeconds = randomGenerator.Next(100);
            // Do your control stuff
            }
        }
    }




我喜欢发现按钮位置随时间随机变化的形式."

使用上面的代码,在我的注释您做控制的事情"所在的位置,添加代码:




"i like to find how button location move in form random with time ."

Use the code above, and where my comment "Do your control stuff" is, add the code:

int xOffset = randomGenerator.Next(-20, 21);
int yOffset = randomGenerator.Next(-20, 21);
myControlIWantToMove.Location = new Point(myControlIWantToMove.Location.X + xOffset, myControlIWantToMove.Location.Y + yOffset);


它需要进行一些微调,否则会超出表单的边缘,但这还是个主意.


It will need some fine tuning or it will run off the edge of your form, but that''s the idea anyway.


这篇关于越过C#中的任何控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆