|
-
May 12th, 2002, 09:05 PM
#1
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?
-
May 13th, 2002, 11:22 AM
#2
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()
-
May 13th, 2002, 11:29 AM
#3
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
-
May 13th, 2002, 01:19 PM
#4
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?
-
May 13th, 2002, 02:37 PM
#5
You could use the Controls collection of the Pane object to loop through the picture boxes.
-
May 13th, 2002, 03:12 PM
#6
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?
-
May 13th, 2002, 04:16 PM
#7
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.
-
May 13th, 2002, 05:55 PM
#8
Thanks a bunch. You straightened me out....lol.
I got it all working now....I really appreciate the help you gave me.
-
May 14th, 2002, 11:01 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|