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

    Post How to create n no of buttons dynamically and handle their events?

    please send ur suggestions regarding this topic

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to create n no of buttons dynamically and handle their events?

    It's pretty basic...

    Code:
    for ( int i = 0; i < n; ++i)
    {
        Button button = new Button( );
        // set properties like "Location"...
        button.Click += button_Click;
        // assuming the "this" pointer is the parent of the controls.
        this.Controls.Add( button );
    }
    
    private void button_Click( object sender, EventArgs e )
    {  
        // handle the click event for each button here
    }
    Last edited by BigEd781; May 19th, 2009 at 04:05 PM.

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: How to create n no of buttons dynamically and handle their events?

    Quote Originally Posted by BigEd781 View Post
    Code:
    private void button_Click(EventArgs e)
    {  
        // handle the click event for each button here
    }
    Shouldn't it be
    Code:
    private void button_Click(object sender, EventArgs e)
    {  
        // handle the click event for each button here
    }

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to create n no of buttons dynamically and handle their events?

    Quote Originally Posted by dannystommen View Post
    Shouldn't it be
    Code:
    private void button_Click(object sender, EventArgs e)
    {  
        // handle the click event for each button here
    }
    Yes, it should

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