Results 1 to 2 of 2

Thread: Dynamic generation of Button

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2004
    Location
    India
    Posts
    53

    Dynamic generation of Button

    hi everyone

    i have tried generating Button control dynamically... but faced two problems:
    1) if i want to add this button to any container control...then its fine( i can user control.add(objButton)...but i couldnt work out how to add it to a form and place it at desired location;

    2)how do we add event handler for this dynamically generated button...i mean where do we write coe to handle Click event for this button.

    thnx in advance.
    Yash

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Dynamic generation of Button

    1) You can either use a placeholder control or add the button to your form's control collection. As every control in the form has a index number you can use the AddAt(Int32, Control) method rather than the Add(Control) to position it where you want.

    2) Try:
    Code:
    // Handle Click Event
    void btnName_Click(object sender, EventArgs e) {
    // Do whatever
    }
    
    // Create Button
    Button btnName = new Button();
    btnName.ID = "newButton";
    btnName.Text = "Click here";
    btnName.Click += new EventHandler(btnName_Click);
    
    PlaceHolderName.Controls.Add(btnName);
    Hope that makes sense.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

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