如何在每行附近添加一个复选框?

时间:2014-02-11 07:00:52

标签: c# winforms

int counter; CheckBox [] _cbs;

ScrollLabel._lines包含151l ines。 counter是int

我希望在第0行然后每3行在行的开头添加一个复选框,右边有一个空格,这样行中的文本和checkBox之间会有一个空格。

现在我在循环中获得异常:

private void button1_Click(object sender, EventArgs e)
        {
            if (this.button1.Text == "stop")
            {
                this.button1.Text = "start";
                this.scrollLabel1.StopTimer();
                for (int i = 0; i < ScrollLabel._lines.Length; i++)
                {
                    counter += 3;
                    _cbs[i] = new CheckBox();
                    _cbs[i].Location = new Point(0, counter);
                    this.scrollLabel1.Controls.Add(_cbs[i]);

                }
            }
            else
            {
                this.button1.Text = "stop";
                this.scrollLabel1.MilliSecsSpeed = (int)this.numericUpDown1.Value;
                this.scrollLabel1.StartTimer();
            } 
        }

_cbs[i] = new CheckBox();

_cbs为空。

这是构造函数:

counter = 0;
            this.scrollLabel1.MouseWheel += new MouseEventHandler(ScrollLabel1_MouseWheel);
            this.scrollLabel1.MouseEnter += ScrollLabel1_MouseEnter;
            readableRss = RssReader.covertRss("http://rotter.net/rss/rotternews.xml");
            RssReader.CnnRss();
            this.DoubleBuffered = true;
            this.scrollLabel1.Text = readableRss;//File.ReadAllText(@"Demo.txt");
            this.scrollLabel1.MilliSecsSpeed = (int)this.numericUpDown1.Value;
            this.scrollLabel1.YStep = (float)this.numericUpDown2.Value;
            this.scrollLabel1.Words = new List<WordColor>();
            this.scrollLabel1.Words.Add(new WordColor() { WordOrText = "scrollLabel1", ForeColor = Color.Red, ColorOnlyThisWord = true, BackColor = Color.LightBlue });
            this.scrollLabel1.Words.Add(new WordColor() { WordOrText = "using", ForeColor = Color.Blue, DrawRect = true, RectColor = Color.Black, BackColor = Color.Wheat });


            this.scrollLabel1.PopLinesOnNonBmpMode = this.checkBox6.Checked;

            this.scrollLabel1.BitmapModus = this.checkBox4.Checked;
            this.scrollLabel1.TextLayoutCentered = this.checkBox5.Checked;
            this.scrollLabel1.AdditionalLinesAtEnd = (int)this.numericUpDown3.Value;

            for (int i = 0; i < ScrollLabel._lines.Length; i++)
            {
                _cbs = new CheckBox[i];
            }

这就是按钮点击中的循环现在的样子:

for (int i = 0; i < ScrollLabel._lines.Length; i++)
                {
                    counter += 3;
                    _cbs[i].Location = new Point(0, counter);
                    this.scrollLabel1.Controls.Add(_cbs[i]);

                }

但是所有的_cbs都是null。

编辑**

for (int i = 0; i < ScrollLabel._lines.Length; i++)
                {
                    _cbs[i] = new CheckBox();
                    counter += 3;
                    _cbs[i].Location = new Point(0, counter);
                    this.scrollLabel1.Controls.Add(_cbs[i]);

                }

没有得到null但我没有看到每个3行开头附近/的checkBox为什么?

1 个答案:

答案 0 :(得分:3)

_cbs是您已声明的数组,并且您在构造函数中重新分配

for (int i = 0; i < ScrollLabel._lines.Length; i++)
        {
            _cbs = new CheckBox[i]; // This line reallocates the array as a larger array each time through the loop!
        }

您只需要定义数组一次

CheckBox[] _cbs = new CheckBox[ScrollLabel._lines.Length];

然后你将单独的复选框分配给数组的元素:

for (int i = 0; i < ScrollLabel._lines.Length; i++) {
  _cbs[i] = new CheckBox(); // This allocates a new checkbox for the i-th element
}

要在每三行放置一个复选框,请尝试以下操作:

int lineHeight = 20;                   // pixels, set this to whatever your line height is
int checkboxInterval = lineHeight * 3; // every third line
int numCheckboxes = ScrollLabel._lines.Length / 3;
for (int i = 0; i < numCheckboxes; i++) {
  _cbs[i] = new CheckBox();
  _cbs[i].Location = new Point(0, i * checkboxInterval);
  this.scrollLabel1.Controls.Add(_cbs[i]);
}

请注意,您只需要三分之一的复选框作为行数。