2

My app needs to be able to dynamically create new form elements and work with them. Right now I have a panel with buttons and labels in it. I need to be able to make a duplicate of this and show it in my app and then work with it.

For example, I have panel1. Inside are label1, button1, and button2. Label 1 just counts up by seconds. When you click button1, label1 starts counting up. When you click button2, the timer stops. My problem is that I need to be able to duplicate panel1 many times and still have the new buttons correspond to the correct labels.

On button_click

private void button1_Click(object sender, EventArgs e)
{
   Button theSender = (Button)sender;
   Panel parentPanel = (Panel)theSender.Parent;
}

From here, I can't target any of the child control . I'm used to targeting and handles in jQuery, so I don't even know the correct C# terminology for how to explain myself.

leppie
  • 115,091
  • 17
  • 196
  • 297
Chris
  • 2,619
  • 6
  • 27
  • 34

5 Answers5

3

If understand your problem correctly, I recommend you to make a Usercontrol with a Panel and fill it with your Label, Button and whatever. Write the events for your buttons in the usercontrol. Then introduce this usercontrol in your form and it should work. You can introduce any number of usercontrols in your form and each button will behave/work for the label in that usercontrol only.

As you mentioned you are new in winforms and you are not sure what I am saying, let me know and I will help if I get enough time.

Hope it helps.

Sandy
  • 11,332
  • 27
  • 76
  • 122
  • I like this idea. I Need to do some more research and trying to implement it. I'll let you know how it goes. For now, I used the Button btn = this.Controls["button1"]; method to target my children controls. I may go take that out later. I hope UserControls are easy enough to understand. – Chris Jan 22 '13 at 08:00
  • Usercontrol are easy to learn....I suggest you to go with usercontrol instead of your approach. But if you can....wait for an hour and I will post another answer that you can understand easily and will also serve your purpose without introducing any new concepts. I am going for an break now. Need to have Chicken Mac Nuggets :P – Sandy Jan 22 '13 at 08:03
  • That works for me. I just started C# yesterday, so I know I'm tackling a beast of a first project. I'm just trying to build my resume and add a new language and skill set. I'll be reading between now and then anyway. – Chris Jan 22 '13 at 08:09
  • Dude, that was amazing. I made so much progress because of this! Thanks. – Chris Jan 22 '13 at 14:05
1

Children of a control can be accessed using Control.Contrtols collection, e.g. to access button on a form:

Button btn = this.Controls["button1"]; 

But that is only true if button1 is placed directly on your form and button1.Name property is set to "button1" (designer does that automattically, if you are creating your controls dynamically, you have to take care of naming your controls yourself.)

You can also enumerate child controls of any control, e.g. child controls of panel1:

foreach(Control c in panel1.Controls)
{
// do something, e.g.
   if(c is Label){//do sth...}
   if(c.Name.Equals("label1") && c is Label)
   {
      Label l = c as Label;
   }
}

and as @rapsalands said, UserControl may be an answer for you.

Arie
  • 5,251
  • 2
  • 33
  • 54
  • too much hassle, UserControls are the best way to incapsulate the functionality and be able to duplicate it over and over – Adi Jan 22 '13 at 07:24
  • I agree that usually UserControls are better – Arie Jan 22 '13 at 13:15
  • I ended up using UserControls, but I am using your first line of code to target controls. That was a headache for hours yesterday. Thanks for that line. – Chris Jan 22 '13 at 14:06
0

I would create a user control (UserControl) for this. Check this article for more explanation about the difference between Control and UserControl.

Controls and UserControls are easy to duplicate and the full functionality is there.

Community
  • 1
  • 1
Adi
  • 5,113
  • 6
  • 46
  • 59
0

You can create new UI Controls in code as you would any other object: Button b = new Button();

Then you can add them to the form using form.Controls.Add(b). You'll need to position and size the controls as well (there are properties available for doing this) and hook up your event handlers using b.Clicked += form.button_click;.

To see an example of this, you can try having a look at the designer.cs file that is generated in Visual Studio (don't make changes to it, just have a look). It will look quite complex at first but might go some way to helping demystify Windows Forms, and you will be able to find all of the properties you need to set in there.

Whenever you update something in the designer, Visual Studio generates new code and puts it in the designer.cs file. The entire form is set up in the InitializeComponent() method, which is called from the constructor of your form. You should be able to copy some of that code and with a couple of modifications use it for creating your own dynamic UI elements.

As rapsalands says, it sounds like a user control would be useful in this situation, as it will help encapsulate the functionality you're after. However that may take a bit of time to get your head round and you may find it simpler for now to do everything in your form without creating a new control.

rankAmateur
  • 2,067
  • 2
  • 18
  • 22
  • I am not clear with your answer.....I need to decide number of panels on runtime on some calculation basis. Then how can you write something in `.designer`. More over if I want 100 panels, then should I make 100 panels in designer manually?? And if then I decide to go with `flowlayoutpanel` instead of `Panel`, then should I edit the same amount of code again?? – Sandy Jan 22 '13 at 07:59
  • I'm not suggesting making changes to the .designer code, just to have a look at it to see how to instantiate controls in code. I'll edit my answer to make that clearer. – rankAmateur Jan 22 '13 at 08:11
0

So you are a beginner and need some time to understand Usercontrol as I mentioned in my previous answer. Use a for loop in the Constructor or Load event of your form to dynamically generate controls.

Panel panel;
Label label;
Button button1;
Button button2;

for(int i = 0; i > count; i++)
{
    panel = new Panel();
    button1 = new Button();
    button2 = new Button();
    label = new Label();

    panel.Controls.Add(button1);
    panel.Controls.Add(button2);
    panel.Controls.Add(label);
    Controls.Add(panel);

    button1.Click += Event1;
    button2.Click += Event2;
}

private void Event1()
{
    label.Text = "Button 1 Clicked."
}
private void Event2()
{
    label.Text = "Button 2 Clicked."
}

This way certainly you can create as many controls you want and will also serve your purpose. Use some variables to locate the panel controls appropriately. Set any properties you wish to add in the for loop for the controls.

This is just an alternative for my previous answer. I still recommend the previous answer given by me. This code is dummy and not tested.

Hope it helps.

Sandy
  • 11,332
  • 27
  • 76
  • 122