CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Sep 2010
    Posts
    4

    How to handle dynamic buttons?

    I created buttons dynamically with a loop. Into a table and it was successfully added.
    Here's a part of my code.

    PHP Code:
    for (int i 06i++)
    {
                
    Button btn = new Button();
                
    btn.Text "Available";
                
    btn.ID "btn" i;
                
    btn.Click += new System.EventHandler(btn_Click);
                
    tc.Controls.Add(btn);
    }

    public 
    void btn_Click(object senderSystem.EventArgs e)
    {
            
    Button currentButton = (Button)sender;
            
    string ID currentButton.ID;
            switch(
    ID)
            {
                case 
    "btn1"subjectID subjectIDs[0]; break;
                case 
    "btn2"subjectID subjectIDs[1]; break;
                case 
    "btn3"subjectID subjectIDs[2]; break;
                case 
    "btn4"subjectID subjectIDs[3]; break;
                case 
    "btn5"subjectID subjectIDs[4]; break;
                case 
    "btn6"subjectID subjectIDs[5]; break;
            }

    But when I click the buttons that were created, the event isn't firing.
    Is my button handler correct?
    Last edited by Mercvapor; September 28th, 2010 at 08:07 AM.

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

Featured