C#试图使标签(Object)从表单的一侧反弹到另一侧

时间:2016-12-12 13:15:20

标签: c# object parent-child

我目前正在阅读Head First C#第三版,我正在使用visual studio 2015作为IDE。本书中的一个程序称为弹跳标签。

所需结果:在程序中,3个标签对象的数组对应于表单上的按钮。当按下每个标签的相应按钮时,标签应该从表格的一端移动到另一端,就像“弹跳”一样。

问题:标签移动到表单的右端,然后停止通过表单的3/4。他们永远不会反弹。查看图片链接。 (代表太低而不能内联)

https://postimg.org/image/qn7s9pfdz/

一些技术细节:该表单有一个计时器,启用 true 并且 Interval 为1.计时器是假设循环保镖数组,如果它们不为空,则调用它们的移动方法。

我已经包含了我正在学习的书中页面的链接。

https://books.google.com/books?id=mcWDAAAAQBAJ&pg=PA181&lpg=PA181&dq=bouncing+labels+c%23&source=bl&ots=U219enVOgQ&sig=9joHw7tNrCcM8d6rLZDR1ydFsBA&hl=en&sa=X&ved=0ahUKEwiizKqZw-7QAhUFWSYKHQnXAmQQ6AEINDAE#v=onepage&q=bouncing%20labels%20c%23&f=false

我有一个Bouncer类的脚本和表单。

Bouncer Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bouncinglabels
{
    using System.Windows.Forms;
    class LabelBouncer
    {
        public Label MyLabel;
        public bool GoingForward = true;

        public void Move()
        {
            if (MyLabel != null) 
            {
                if (GoingForward == true)
                {
                    MyLabel.Left += 5;
                }
                if (MyLabel.Left >= MyLabel.Parent.Width - MyLabel.Width)
                {
                    GoingForward = false;
                }
            }
            else
            {
                MyLabel.Left -= 5;
                if (MyLabel.Left <= 0)
                {
                    GoingForward = true;
                }
            }
        }
    }
}

这是Form1.Cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bouncinglabels
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        LabelBouncer[] bouncers = new LabelBouncer[3];

        private void ToggleBouncing(int index, Label labelToBounce)
        {
            if (bouncers[index]==null)
            {
                bouncers[index] = new LabelBouncer();
                bouncers[index].MyLabel = labelToBounce;
            }
            else
            {
                bouncers[index] = null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ToggleBouncing(0, label1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ToggleBouncing(1, label2);
        }

        private void button3_Click(object sender, EventArgs e)
        {

            ToggleBouncing(2, label3);
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < 3; i++)
            {
                if (bouncers[i] != null)
                {
                    bouncers[i].Move();
                }
            }
        }


    }
}

1 个答案:

答案 0 :(得分:0)

这应该有效。刚刚移动了你的其他声明。如果使用MyLabel,你的其他人就低于你的了!= null。

public void InsertData()
{
    SqlConnection connection = null;

    try {
      connection = new SqlConnection(DBHelper.ConnectionString);
      connection.Open();

      SqlCommand command = null;

      try {
         command = new SqlCommand("Some Simple Insert Query", connection);
         command.ExecuteNonQuery(); 
      }
      finally { // rain or shine, dispose the resource (if it has been created)
        if (command != null)
          command.Dispose(); 
      }
    }
    finally { // rain or shine, dispose the resource (if it has been created)
      if (connection != null)
        connection.Dispose();
    }