Strange of prefix decrement and conditions in the C# Winforms programme
-
When pressed on the button, a little bit lower, another button is added and it should only be possible eight times.
Button[] bt = new Button[8]; private int i { get; set; } = 1; private void home_Load(object sender, EventArgs e) { bt[0] = addButton1; } public void addButton1_Click(object sender, EventArgs e) { if (i <= 8) { Button b = (Button)sender; bt[i] = new Button();
bt[i].BackColor = Color.Transparent; bt[i].FlatAppearance.BorderSize = 0; bt[i].FlatAppearance.MouseDownBackColor = Color.Transparent; bt[i].FlatAppearance.MouseOverBackColor = Color.Transparent; bt[i].FlatStyle = FlatStyle.Flat; bt[i].ForeColor = Color.White; bt[i].Image = Properties.Resources.add; bt[i].Location = new Point(543, 132); bt[i].Name = "addButton" + i.ToString(); bt[i].Size = new Size(37, 28); bt[i].UseVisualStyleBackColor = false; bt[i].Location = new Point(b.Location.X, b.Location.Y + 32); Controls.Add(bt[i]); bt[i].Click += new EventHandler(addButton1_Click); bt[i - 1].Image = Properties.Resources.remove; i++; } }
First,I don't quite understand why if there's a condition if (iRO=8), I can still press the last (eighth) button and even get a mistake.
SecondI had a problem, and I didn't actually add buttons. I couldn't figure out what the problem was, and then I fixed it by pumping,:bt[--i].Image = Properties.Resources.remove;
On:
bt[i - 1].Image = Properties.Resources.remove;
And it all started working.
-
I don't know how c#, but in java, the mass index starts with
0
♪
size[8]
has indices from0
before7
(length - 1)if (i <= 8)
and here, in checking i for equality, we shall be able to get out of the terrain, the last element we have under index 7.
Try test condition i to 8
if (i < 8)