I have a simple winform which creates has in it a NumericUpDown, two group boxes and two buttons. The code then dynamically inserts a number of NumberBox objects into one group box and a pile of labels into the other.
(A numberbox is just an overloaded TextBox - details are at http://www.all-the-johnsons.co.uk/csharp/numberbox.html)
If I click the NumericUpDown, the code inserts new boxes and also reduces it as expected. However, if I click once to create 3 boxes and then go back down to 2, half of the third row still remains and I have no idea why!
NumericUpDown goes from 3 to 6, no decimals
Expand|Select|Wrap|Line Numbers
- private void numericUpDown1_ValueChanged(object sender, EventArgs e)
- {
- drawBoxes((int)numericUpDown1.Value);
- }
- private void drawBoxes(int number)
- {
- int ar = number;
- int b = ar;
- int p = 0; // position
- int r = 0; // current row
- ar = Convert.ToInt32(Math.Pow(ar, 2));
- NumberBox[] tmp = new NumberBox[ar]; // do it for the smallest size
- Label[] lbl = new Label[ar];
- int t = 0;
- for (int n = 0; n < ar; ++n)
- {
- tmp[n] = new NumberBox();
- tmp[n].Location = new System.Drawing.Point(7 + p, 20 + (26 * r));
- tmp[n].Name = "numberBox" + n.ToString();
- tmp[n].Size = new System.Drawing.Size(42, 20);
- tmp[n].TabIndex = n;
- groupBox1.Controls.Add(tmp[n]);
- lbl[n] = new Label();
- lbl[n].Location = new System.Drawing.Point(7 + p, 20 + (26 * r));
- lbl[n].Name = "label" + n.ToString();
- lbl[n].Text = "0";
- lbl[n].Size = new System.Drawing.Size(42, 20);
- lbl[n].TabIndex = n + ar;
- groupBox2.Controls.Add(lbl[n]);
- p += 48;
- if (t + 1 != b)
- t++;
- else
- {
- t = 0;
- r++;
- p = 0;
- }
- }
- int z = 8 + b * 48;
- int y = 8 + (b * 26) + 16;
- groupBox1.Size = new System.Drawing.Size(z, y);
- groupBox2.Location = new System.Drawing.Point(32 + b * 48, 43);
- groupBox2.Size = new System.Drawing.Size(z, y);
- this.clear.Location = new System.Drawing.Point(16, y + 48);
- this.calc.Location = new System.Drawing.Point(97, y + 48);
- ClientSize = new System.Drawing.Size(z * 2 + 32, y + 80);
- nbArray = tmp; // global
- rdArray = lbl; // global
- }
The attached screenshot illustrates the problem, look above the buttons. The numberbox gadgets aren't there (I can't tab onto them or click in them), but they remain on screen.
Any help here would be appreciated
Paul