Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i type the code below, but i only see one button in form with 100 left.

C#
private void Form1_Load(object sender, EventArgs e)
        {            
            Button[] btnexit = new Button[5];
            for (int i = 1; i <= 5; i++)
            {
                btnexit[i] = new Button();
                this.Controls.Add(btnexit[i]);
                this.Text = this.Text + i.ToString();                
            }
            btnexit[1].Left = 100;
            btnexit[2].Left = 200;
            btnexit[3].Left = 300;
            btnexit[4].Left = 400;
        }


i couldn't solve it. what is, it's solution?
Posted

Both code in the question and the answer by Mantu are wrong. It should be something like that:
C#
int buttonCount = 5;
Button[] buttons = new Button[buttonCount];

//...

int gap = 10; //... some suitable gap between buttons
int currentX = //... some starting position
for (int index = 0; index < buttonCount; ++index) {
    Button button = new Button();
    button.Text = //... and not this.Text, why?
    button.Top = //...  
    button.Left = currentX + gap;
    buttons[index] = button;
    this.Controls.Add(button);
    currentX += this.Width;
}

This way, you can arrange those buttons by X taking into account their actual width when the text is already set up. Also, pay attention that I did not repeat your "5" anywhere, which would be a deadly sin of programming.

—SA
 
Share this answer
 
v3
Comments
Sandeep Mewara 18-Jun-12 14:48pm    
Correct. A 5!
Sergey Alexandrovich Kryukov 18-Jun-12 14:50pm    
Thank you, Sandeep.
--SA
mrmarzban 19-Jun-12 0:47am    
i used "5" just as a number, that wasn't a part of my program.
i used "this", just to see that my "for" is working.
how ever your solution helped me to solve my problem better.
thanks for that.
Sergey Alexandrovich Kryukov 29-Jun-12 22:24pm    
If so, please accept the answer formally (green button) -- thanks.
--SA
The above code gives index out of bound exception just alittle correction and it works as below:-


C#
Button[] btnexit = new Button[5];
           for (int i = 0; i < 5; i++)
           {
               btnexit[i] = new Button();
               this.Controls.Add(btnexit[i]);
               this.Text = this.Text + i.ToString();
           }
           btnexit[1].Left = 100;
           btnexit[2].Left = 200;
           btnexit[3].Left = 300;
           btnexit[4].Left = 400;



Best of luck!!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jun-12 14:07pm    
Very bad; you repeated two mistakes of OP, to say the least...
--SA
Mantu Singh 19-Jun-12 14:57pm    
thanks a lot sir for reminding the mistake but I only wanted to run the code and see if i would be able to help and i found index out of range exception on first run.........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900