Results 1 to 9 of 9

Thread: Create controls during runtime

  1. #1
    hellswraith
    Guest

    Create controls during runtime

    Hey,
    I need to create and destroy controls (picturebox's) on a form inside a panel object during runtime. For the life of me, I can't seem to figure out how to do that. I also need to treat it as a control array (old VB6 comming out here, don't know if this is the way you would do it with C# though). How would I do this?

  2. #2
    BG
    Guest
    This will add a radio button to a panel at runtime. Change the type to picture box.
    Code:
    RadioButton oRadioButton = new RadioButton();
    oRadioButton.Location = new System.Drawing.Point(20, 30); 
    oRadioButton.Size = new System.Drawing.Size(10, 15); 
    oRadioButton.ForeColor = System.Drawing.SystemColors.ControlText;
    oRadioButton.Text = xmlRadioNodes[z].FirstChild.InnerText.ToString();
    oRadioButton.Name = "RadioButton" + y.ToString();
    oRadioButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.panel1.Controls.Add(oRadioButton);
    To remove the radio button at runtime use this.panelname.controls.clear()

  3. #3
    BG
    Guest
    Oh yea,
    About the array thing. If all you are trying to do is tie them to the same event you can manually assign events to the control.
    Code:
    oRadioButton.Click += Clicked;
    "Clicked "is the custom created event

  4. #4
    hellswraith
    Guest
    The reason for the array thing, I want to be able to refer to the picture boxes with the same name, and loop through them. I will be creating an unknown amount of pictureboxs during runtime, and I need to be able to access them dynamically instead of using different names for each one. Is this possible?

  5. #5
    BG
    Guest
    You could use the Controls collection of the Pane object to loop through the picture boxes.

  6. #6
    hellswraith
    Guest
    I am thinking about doing that, but I still don't know how to create from as low as 1 to as many as 100 picture boxs during runtime. I can't name them the same name can I?

    I was looking at your code and you have this:
    oRadioButton.Name = "RadioButton" + y.ToString();

    I have been thinking of that, but what is your 'y' object? Is it just a integer that you increment when you create a new radio button?

  7. #7
    BG
    Guest
    Yes That code is in a loop in one of my apps that creates a variable ammount of radiobuttons based on a database. y is an integer that increments itself on every pass through the loop.

  8. #8
    hellswraith
    Guest
    Thanks a bunch. You straightened me out....lol.

    I got it all working now....I really appreciate the help you gave me.

  9. #9
    BG
    Guest
    No Problem man. Have a good 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