So I have a problem in C#. I am new to C# so you have to bear with me.
I have alot of buttons I created at startuptaht I want to change text with (button1,button 2, ...) and I want to automate the process, instead of just writing each line out.
The problem is that every time I run the program, I get a NullReferenceEx ception "Object reference not set to an instance of an object."
It is in the line "this.Contr ols[string.Format(" button{0}", n)].Text = "sometext"; "
I am out of ideas, any help would be great.
Here is part of my code
private void button1_Click(o bject sender, EventArgs e)
{
Button button2 = new Button();
Button button3 = new Button();
int n = 1;
while (n <= 2)
{
int n1 = (n * 27) + 27;
this.Controls[string.Format(" button{0}", n)].Text = "sometext";
this.Controls[string.Format(" button{0}", n)].Size = new Size(57, 21);
this.Controls[string.Format(" button{0}", n)].Location = new Point(11, n1);
this.Controls.A dd(this.Control s[string.Format(" button{0}", n)]);
n++;
}
I have alot of buttons I created at startuptaht I want to change text with (button1,button 2, ...) and I want to automate the process, instead of just writing each line out.
The problem is that every time I run the program, I get a NullReferenceEx ception "Object reference not set to an instance of an object."
It is in the line "this.Contr ols[string.Format(" button{0}", n)].Text = "sometext"; "
I am out of ideas, any help would be great.
Here is part of my code
private void button1_Click(o bject sender, EventArgs e)
{
Button button2 = new Button();
Button button3 = new Button();
int n = 1;
while (n <= 2)
{
int n1 = (n * 27) + 27;
this.Controls[string.Format(" button{0}", n)].Text = "sometext";
this.Controls[string.Format(" button{0}", n)].Size = new Size(57, 21);
this.Controls[string.Format(" button{0}", n)].Location = new Point(11, n1);
this.Controls.A dd(this.Control s[string.Format(" button{0}", n)]);
n++;
}
Comment