C#面板发生碰撞而移动 [英] C# panel move with collision

查看:79
本文介绍了C#面板发生碰撞而移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#和Winforms的新手,并尝试制作一个移动面板.它应该向右移动直到窗口结束,然后向左移动.它应该从一侧反弹到另一侧.但是经过数小时的尝试,唯一发生的事情是它向左移动并停止.

I am new to C# and Winforms and try to make a moving panel. It should move right until the end of my window and then back left. It should bounce from side to side. But the only thing happened after hours of trying is that it moves left and stops.

使用此表单工具:

Timer = tmrMoveBox (interval: 50)
Panel = pnlBox
Label = lblXY (for showing the X and Y coordinates in the form)

这是我的最佳尝试:

private void tmrMoveBox(object sender, EventArgs e)
{
    if (pnlBox.Location.X <= 316)
    {
        for (int i = 0; i <= 316; i++)
        {
            pnlBox.Location = new Point(
                pnlBox.Location.X + 2, pnlBox.Location.Y);
            string BoxLocationToString = pnlBox.Location.ToString();
            lblXY.Text = BoxLocationToString;
        }
    }

    else if (pnlBox.Location.X >= 0)
    {
        for (int i = 0; i >= 316; i++)
        {
            pnlBox.Location = new Point(
                pnlBox.Location.X - 2, pnlBox.Location.Y);
            string BoxLocationToString = pnlBox.Location.ToString();
            lblXY.Text = BoxLocationToString;
        }
    }
}

第二好的尝试:

private void tmrMoveBox(object sender, EventArgs e)
{
    int runBox = 1;

    if(runBox == 1)
    {
        while (pnlBox.Location.X <= 316)
        {
            pnlBox.Location = new Point(
                pnlBox.Location.X + 2, pnlBox.Location.Y);
            string BoxLocationString = pnlBox.Location.ToString();
            lblXY.Text = BoxLocationString;
            runBox = 0;
        }
    }
    else
    {
        while(pnlBox.Location.X > 0)
        {
            pnlBox.Location = new Point(
            pnlBox.Location.X - 2, pnlBox.Location.Y);
            string BoxLocationString = pnlBox.Location.ToString();
            lblXY.Text = BoxLocationString;
            runBox = 1;
        }
    }
}

也尝试使用while循环,但是随后面板消失了. 我不是专家,只是将此移动面板设置为自己的目标.希望任何人都能给我小费.

Tried to use a while loop too but then the panel just disappears. I'm no expert and just set this moving panel as a goal for myself. Hope anyone can give me a tip.

Form1.Designer.cs

Form1.Designer.cs

 this.timer1.Interval = 50;
 this.timer1.Tick += new System.EventHandler(this.tmrMoveBox);
 this.timer1.Start();
 this.timer1.Step = 2;

推荐答案

取决于您使用的内容:

  1. Windows窗体
  2. WPF

创建一个Timer并订阅Tick事件.另外,您应该创建新的int属性Step.

Create a Timer and subscribe to the Tick event. Also, you should create new int property Step.

1. Windows表单:

System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
int Step;

Form1 () 
{
     InitializeComponent()
    ....
     t.Interval = 15000; // specify interval time as you want
     t.Tick += new EventHandler(timer_Tick); 
     t.Start();

     this.Step = 2; 
}

在滴答声中,事件处理程序将您的逻辑放到了空位

And in ticks event handler put your logic, without while

void timer_Tick(object sender, EventArgs e)
{
        if (pnlBox.Location.X >= 316)
        {
            Step = -2;
        }
        if (pnlBox.Location.X <= 0) 
        {
            Step = 2;
        }

        pnlBox.Location = new Point(
        pnlBox.Location.X + Step , pnlBox.Location.Y);
        string BoxLocationString = pnlBox.Location.ToString();
        lblXY.Text = BoxLocationString;
}

因此,您的框将在每个计时器刻度上移动一步.

So your box will move on one step per one timer tick.

1. WPF:

由于System.Windows.Forms.Timer不可用,您可以使用System.Windows.Threading.DispatcherTimer:

As System.Windows.Forms.Timer is not available, you may use System.Windows.Threading.DispatcherTimer:

using System.Windows.Threading;

DispatcherTimer t = new DispatcherTimer();
t.Interval = new TimeSpan(0, 0, 15); // hours, minutes, seconds (there are more constructors)
t.Tick += Timer_Tick;
t.Start();

这篇关于C#面板发生碰撞而移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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