Results 1 to 3 of 3

Thread: overcomming Control Arrays

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Israel
    Posts
    152

    overcomming Control Arrays

    Hello everybody,

    I come from a pretty colorful VB6 background and am now trying to learn C#.

    all goes pretty well for me except for the fact that there are no control arrays(correct me if I'm wrong).

    I've created a grid of command buttons at runtime, linked their click event to the same delegate and all, but now I can'f figure out how to retrieve the index of a button, (3,4) for example....

    it would serve my purpose perfectly well if I could create an array and link each button to an entry, but I would like to hear other ideas on the subject(how to overcome the obstacles that resulted from dropping array controls)

    please don't tell me to put the index into the Tag property of each button...

    Thanks in advance

  2. #2
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    Re: overcomming Control Arrays

    Is this something you need?
    VB Code:
    1. private void Form1_Load(object sender, System.EventArgs e)
    2.         {
    3.             int y=10;
    4.             for(int i=0;i<10;i++)
    5.             {
    6.                 Button b=new Button();
    7.                 b.Text="Button"+i.ToString();
    8.                 b.Location=new Point(10,y);
    9.                 b.Click+=new EventHandler(b_Click);
    10.                 this.Controls.Add(b);
    11.                 y+=25;
    12.             }
    13.         }
    14.         void b_Click(object sender,EventArgs e)
    15.         {
    16.             Button b=(Button)sender;
    17.             MessageBox.Show(b.TabIndex.ToString());
    18.         }
    Or I am missing something. If so, I am sorry... but if you need something from the sender, you can actually do anything on the b variable under the b_Click function. Hopefully this helps.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Location
    Israel
    Posts
    152

    Re: overcomming Control Arrays

    Thank you Mokey,

    it's the "this.Controls.Add(b);" line that I've been missing... Thank you for helping me out on this one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width