[英]How do i add a checkBox near each line?
int counter; 整数计数器 CheckBox[] _cbs;
CheckBox [] _cbs;
ScrollLabel._lines contain 151l ines. ScrollLabel._lines包含151l ines。 counter is int
计数器是int
I want that on line 0 and then each 3 lines to add a checkbox in the beginning of the line with one space to the right so there will be one space place between the text in the line and the checkBox. 我希望在第0行,然后每3行在行的开头添加一个复选框,并在右边添加一个空格,以便在行中的文本和checkBox之间有一个空格。
Now im getting exception in the loop on the line: 现在我在循环中得到异常:
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 is null. _cbs为空。
This is the constructor: 这是构造函数:
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];
}
This is how the loop in the button click look like now: 这是按钮单击中的循环现在的样子:
for (int i = 0; i < ScrollLabel._lines.Length; i++)
{
counter += 3;
_cbs[i].Location = new Point(0, counter);
this.scrollLabel1.Controls.Add(_cbs[i]);
}
But all the _cbs are null inside. 但是所有的_cbs在里面都是空的。
EDIT** 编辑**
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]);
}
Not getting null but i dont see checkBox near/in the beginning of each 3 lines why ? 没有得到空值,但我在每3行的开头/附近看不到checkBox为什么?
_cbs
is an array which you have declared and you are reallocating in your constructor: _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!
}
You need to define the array only once : 您只需要定义一次数组:
CheckBox[] _cbs = new CheckBox[ScrollLabel._lines.Length];
Then you allocate individual checkboxes to the elements of the array: 然后, 将各个复选框分配给数组的元素:
for (int i = 0; i < ScrollLabel._lines.Length; i++) {
_cbs[i] = new CheckBox(); // This allocates a new checkbox for the i-th element
}
To place a checkbox on every third line, try something like this: 要在每三行上放置一个复选框,请尝试如下操作:
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]);
}
Note that you only need one-third as many checkboxes as the number of lines. 请注意,您仅需要复选框的数量是行数的三分之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.