代码: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 WindowsFormsApplication19
{
    public partial class Form1 : Form
    {
      
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Top > Form1.ActiveForm.Size.Height || button1.Bottom < 0)
                label1.Top =label1.Top-50;
            else { label1.Top += 50; }
        }        private void button2_Click(object sender, EventArgs e)
        {
            if (button1.Left > Form1.ActiveForm.Size.Width || button1.Right < 0)
                label1.Left -=50;
            else { label1.Left += 50; }
        }

解决方案 »

  1.   

    label.Left = label.Left + 10;
    if (label.Left > this.Width) label.Left = 0;
      

  2.   


     bool moveBottom = true;
            private void button1_Click(object sender, EventArgs e)
            {
                if (moveBottom)
                {
                    label1.Top += 50;
                    if (label1.Top > this.Height)
                        moveBottom = false;
                }
                else
                {
                    label1.Top -= 50;
                    if (label1.Top < 0)
                        moveBottom = true;
                }
            }左右移动的仿着写写
      

  3.   

    你这个移动要和你窗体的大小对的上号,好像你点一下就加10那么你的窗体的大小必须是10的倍数,判断是否等于窗体大小,否则是不会返回的,获取的lable的X,Y的值好像是左上角的,判断完就把lable移回你想要开始的地方
      

  4.   

    为何不用while去判断  坐标大于或等于了就返回到初始位置!
      

  5.   


    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;
    using System.Collections;namespace cc
    {
        public partial class Form1 : Form
        {
            static bool bFlag; //false是向下移,true是向上移        public Form1()
            {
                InitializeComponent();
                bFlag = false;  //初始时向下移
            }        private void btnStart_Click(object sender, EventArgs e)
            {
                int nTop;   //label的x位置
                int nHeith; //lagel的高度
                            nTop = lab1.Top;
                nHeith = lab1.Height;            if ( !bFlag)
                {
                    //当前标识是false,向下移
                    if (nTop + nHeith + 20 > this.Height)
                    {
                        //如果这次移动后将超出窗体的下部,则改为向上移
                        nTop -= 20;
                        lab1.Top = nTop;
                        bFlag = true;
                    }
                    else
                    {
                        nTop += 20;
                        lab1.Top = nTop;
                    }
                }
                else
                {
                    //当前标识是true,向上移
                    if (nTop - 20 < 0)
                    {
                        //本次向上移,将会超出窗体的上部,则改为向下移
                        nTop += 20;
                        lab1.Top = nTop;
                        bFlag = false;
                    }
                    else
                    {
                        nTop -= 20;
                        lab1.Top = nTop;
                    }            }            this.Text = "Form1" + this.Height.ToString() + " ; lab:" + nTop.ToString() + " ; ";        }        
           
        }
    }
      

  6.   

        public partial class Form1 : Form
        {
            Button btnStar;
            Button btnStop;
            Label lbl;        int MoveSpeed = 12;
            bool Direction = true;//true is right,false is left
            bool IsRun = true;        public Form1()
            {
                InitializeComponent();
                this.Controls.Add(btnStar = new Button { Text = "Star" });
                this.Controls.Add(btnStop = new Button { Text = "Stop", Top = 25 });
                this.Controls.Add(lbl = new Label
                {
                    Width = 20,
                    Height = 20,
                    Top = this.Height / 2,
                    Left = this.Width / 2,
                    BackColor = Color.Red
                });                        btnStop.Click += (s, e) => IsRun = false;            btnStar.Click += (s, e) =>
                    {
                        IsRun = true;
                        new Thread(() =>
                            {
                                while (IsRun)
                                {
                                    Thread.Sleep(60);
                                    this.Invoke(new MethodInvoker(() => lbl.Left = Direction ? lbl.Left + MoveSpeed : lbl.Left - MoveSpeed));                                if (lbl.Left < 0)
                                        Direction = true;
                                    if (lbl.Left + lbl.Width > this.Width)
                                        Direction = false;
                                }
                            }).Start();
                    };
            }
        }
      

  7.   

    private void button1_Click(object sender, EventArgs e)
            {
                if (button1.Top > Form1.ActiveForm.Size.Height || button1.Bottom < 0)
                    label1.Top =label1.Top-50;
                else { label1.Top += 50; }
            }至少标红的地方 应该为label1移动的方向,只根据Label的当前位置是判断不出来的。如果位于窗体中间,可能是向上移动、也可能是向下移动。这个时候只能先约定一个方向。
    每次只是在应该转向的时候 调转方向即可。