C# 如何在每行附近添加复选框?

C# 如何在每行附近添加复选框?,c#,winforms,C#,Winforms,整数计数器; 复选框[]\u cbs 滚动标签。\ u行包含151l行。 计数器为int 我希望在第0行,然后每3行在行的开头添加一个复选框,右边有一个空格,以便在行中的文本和复选框之间有一个空格 现在,我在线路上的循环中遇到异常: private void button1_Click(object sender, EventArgs e) { if (this.button1.Text == "stop") {

整数计数器; 复选框[]\u cbs

滚动标签。\ u行包含151l行。 计数器为int

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

现在,我在线路上的循环中遇到异常:

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++)
        {
            _cbs = new CheckBox[i]; // This line reallocates the array as a larger array each time through the loop!
        }
这是按钮单击中的循环现在的样子:

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在里面都是空的

编辑**

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行的开头/附近我看不到复选框为什么?

\u 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++)
        {
            _cbs = new CheckBox[i]; // This line reallocates the array as a larger array each time through the loop!
        }
然后将各个复选框分配给数组的元素:

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]);
}

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

在哪里初始化复选框[]\u cbs?Tim我现在在我的问题中添加了构造函数的代码,最后我添加了一个初始化复选框的循环。但我只看到150个复选框,其中有151行。在初始化构造函数中的复选框后,我再次更新了我的问题。我在按钮单击循环代码时在_cbs[i]上变为null。位置=新点0,计数器;我看到里面有150个索引,它们都是空的。但是为什么我在开始的时候没有看到右边每3行旁边的复选框呢?如果我有151行,我希望每3行在行首右侧添加一个复选框控件。所以在开始的第3行有一个复选框第6行第9行第12行等等…但它不起作用,我只看到一个复选框,而且很凌乱。上次再次更新了我的问题。Sorry.Checkbox.Location指定复选框相对于其容器的位置,请参见。您想要的是在每三行上放置一个复选框,因此您需要确定行大小并将其乘以每三行的三。