Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there.
I want to create controls through loop, but not to define it as new control on each step of loop, like: (because it consumes a lot of mem and cpu)
C#
for (int h = 0; h < Variables.test; h++)
{
  CheckBox chk = new System.Windows.Forms.CheckBox();
  chk.AutoSize = true;
  chk.Size = new System.Drawing.Size(67, 17);
  chk.UseVisualStyleBackColor = true;
  chk.Enabled = true;
  chk.Click += new EventHandler(Main_Click);
  chk.Location = new System.Drawing.Point(45, h * 20 + 200);
  chk.Name = Variables.tblNames[h];
  chk.Text = Variables.tblNames[h];
  this.Controls.Add(chk);
  a[h] = chk;
}

tblnames is a array with parameters as strings<br/>
a is a array with control parameters

But instead, like:
C#
CheckBox chk = new System.Windows.Forms.CheckBox();
chk.AutoSize = true;
chk.Size = new System.Drawing.Size(67, 17);
chk.UseVisualStyleBackColor = true;
chk.Enabled = true;
chk.Click += new EventHandler(Main_Click);
for (int h = 0; h < Variables.test; h++)
{
  chk.Location = new System.Drawing.Point(45, h * 20 + 200);
  chk.Name = Variables.tblNames[h];
  chk.Text = Variables.tblNames[h];
  this.Controls.Add(chk);
  a[h] = chk;
}

BUT, the problem is that in the second code, a values are all the same (a={three,three,three} instead of a={one,two,three})

Can someone help me to find where the problem lies on the code, or is there any better idea to create controls through loop.
Posted
Updated 23-Dec-10 16:50pm
v3
Comments
Dalek Dave 23-Dec-10 22:50pm    
Edited for Code Blocks.

This is because in second sample chk is outside the loop (look, it's where you call a constructor), so you're not working with 3 (or how many) different controls, but repeat all iterations with the same one. If you call a CheckBox constructor only once, there is only one CheckBox, right? so, no wonder...

For a record: your first sample make perfect sense. What would be a purpose of the second one?
You say "but not to define it as new control on each step of loop" (why not?!), but this is not to "define", this is to create. In a loop or not, you only have an instance when you create one...
 
Share this answer
 
Comments
Dalek Dave 23-Dec-10 22:49pm    
Good Answer.
Manfred Rudolf Bihy 24-Dec-10 17:59pm    
Definitely yes! 5+
Espen Harlinn 26-Feb-11 11:11am    
Good reply - a 5
Sergey Alexandrovich Kryukov 26-Feb-11 19:16pm    
Thank you.
--SA
SAKryukov, thank You for your answer. The deal with creating controls on each step of loop, is that it gets heavy and it hangs when i try to add 90 groupboxes with two checkboxes in each of it. So it's 270 controls in one loop, and it consumes 25% of my cpu (a quad core) for about a minute.

Do you have any better idea, to make it happen faster?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Dec-10 22:28pm    
We can discuss it. Your initial idea of dynamic creation of control is just fine, only... nobody is looking at all 270 control at once..? Let me think a bit, I'll try to think of couple of options...

Will you please accept this answer for now (also consider voting)?
makavellidadon 23-Dec-10 22:37pm    
I tried to use threads. I used BackGroundWorking, two of them. I created 600 controls for about 7.5sec . Not bad, but i know I can get better results. Looking forward at your ideas.
Sergey Alexandrovich Kryukov 23-Dec-10 22:43pm    
Thank you. I'll try to advice, but... see below.
Sorry, I'm not interested in improving performance of application which creates 600 controls. Why? Nobody will look at more than ten (to many two tens) active controls at a time. If you insist on so many controls, you'll have to explain the purpose, because this thing would be way too exotic.

So, if you're ready to re-design into more reasonable/practical approach, here is what you can do:

You need to structure design of you mass of controls into some "chapters", "sub-chapters"... into some hierarchy of reasonable depth. Each chapter goes in separate panel, but you should not ever create all 666 ;) controls. Only one chapter should be visible at a time, each containing limited observable an usable number of controls. You need only one panel, but controls are removed and re-populated each time you navigate (see below) to a chapter of interest.

How to navigate? On your main window left, imagine a control that behaves like Table of Contents (TOC). Most general view would provide a TreeView, a simple one (1-level hierarchy) -- a list box. On this control, you navigate from item to item each representing a chapter. Setup event handler OnSelectionChanged: it should clean the panel on your right and repopulate it with new set of controls.

What to put in tree view?

You need something that would be represented as a string, at the same time the item type should carry data on control set on your right. The way to resolve this is some structure with overridden object.ToString:

C#
struct ItemInfo {
    internal ItemInfo(string name /* something else ... */) { Name = name; }
    public override string ToString() {
        return Name.ToString();
    } //ToString
    string Name;
    //whatever else
    //...
} //struct ItemInfo


Now, you need to populate your TOC control with items of the type ItemInfo:

C#
//...
string name = //...
ItemInfo info = new ItemInfo(name);
box.Items.Add(info);
//...


When you handle OnSelectionChanged, you know that you can downcast you item's Content to ItemInfo.

Develop population procedure like CreatePageControls(ref ItemInfo info) and call it from your handler of OnSelectionChanged.

Now, as you show only few controls at a time, any performance will be sufficient (on UI event, one millisecond does not count).

To give you an idea of another look at this: are you familiar with virtual container controls and/or virtual mode of controls such as grid views or list views? You can think of the design I describe as of a virtual-mode container. Alternatively, you can make more general solution using virtual container controlled by, say, a scroll bar. A scroll bar can show 900 thousands of virtual items, with only a small part of them existing physically at the same time: only those in the visible part of scrollable area plus as many down and up of this place, for better cashing.

How about that?
 
Share this answer
 
Comments
makavellidadon 23-Dec-10 23:43pm    
Nice post, but it's another topic.
For this app I'm building, 600 controls are sometime nothing. You see I make an app that connects with DataBase, and depending on number of tables, it creates x checkboxes. with each ch.b. created, 3 other controls are created (groupbox and two radiobutons). Now for every column of a table, a new checkbox is created. Kinda exponential growth of controls.
Now how I explained, it may look nasty app but there's no other way I could do it.
Sergey Alexandrovich Kryukov 24-Dec-10 1:23am    
Yes, there must be another way. Whatever you say, the fact no one can deal with so many controls -- is a basic fact, and the rest is just an implementation detail. From the other hand, if you keep trying to ram the problem is your straightforward way, you may never get to acceptable solution. Think about it, this is the best I can advise.


You know, I love the wonderful advice (for software architects) I read: "Do not be a problem solver"; do you understand the meaning?

Anyway, sorry you cannot accept my advice, wish you good luck, best wishes in New Year!
Manfred Rudolf Bihy 24-Dec-10 18:03pm    
Too bad he didn't understand what you are trying to say. I'd have accepted this on as "the" answer. Anyway take another 5+
Sergey Alexandrovich Kryukov 25-Dec-10 0:04am    
Manfred, thank you very much for your support, all your votes and for your understanding.

Do you know better ways to explain such things which I might be missing? (Also, English is not my native.) If you could offer your criticism and give me any advices how to explain things better (I see you authored 183 answers by now) I would be enormously grateful.

I do have similar problems of misunderstanding with my articles and Tips. Did you see them? I think they are quite interesting, good and easy to use, but the motivation and ideology are kind of hard to explain. I would be very interested in your opinion.

Best wishes in New Year!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900