B
Brad Huff
A couple of days ago, I had posted an issue about a
program crashing when too many Controls.Add(childcontrol)
was performed. The code snippet below illustrates this
problem with a TextBox component:
using System;
using System.Windows.Forms;
using System.Drawing;
public class Test{
public class MyForm:Form{
Button button = new Button();
int count = 0;
public MyForm(): base() {
InitializeComponent();
}
private void InitializeComponent(){
//this.Width = 1000;
//this.Height = 900;
button.Click += new EventHandler(click);
button.Width = 300;
Controls.Add(button);
}
private void click(object sender,EventArgs e){
TextBox[,] controlSet = new TextBox
[1,5000];
Label label = new Label();
for (int i = 0;i<5000;i++){controlSet
[0,i] = new TextBox();}
count++;
button.Text = count.ToString();
Controls.Clear();
Controls.Add(button);
label.Location = new Point(0,50);
Controls.Add(label);
for (int i=0;i<5000;i++){
try {
controlSet[0,i].Location =
new Point(0,100); Controls.Add(controlSet[0,i]);
} catch (Exception ex)
{label.Text = ex.Message;}
}
}
public static void Main(string[] args)
{
MyForm form = new MyForm();
Application.Run(form);
}
}
}
When the button is pressed the 2nd time, after about 2
minutes, an error message states that a window handle
could not be found. I know that the situation above
maybe hypothetical but but it would be useful to explore
the limitation and or possibly a bug with Control class.
How would you change the snippet so that the button
clicks good. The example is for 5000 TextBox
components. If I use only 1000, the button could be
pressed many times without a problem, also goes much
quicker.
Thanks,
Brad
program crashing when too many Controls.Add(childcontrol)
was performed. The code snippet below illustrates this
problem with a TextBox component:
using System;
using System.Windows.Forms;
using System.Drawing;
public class Test{
public class MyForm:Form{
Button button = new Button();
int count = 0;
public MyForm(): base() {
InitializeComponent();
}
private void InitializeComponent(){
//this.Width = 1000;
//this.Height = 900;
button.Click += new EventHandler(click);
button.Width = 300;
Controls.Add(button);
}
private void click(object sender,EventArgs e){
TextBox[,] controlSet = new TextBox
[1,5000];
Label label = new Label();
for (int i = 0;i<5000;i++){controlSet
[0,i] = new TextBox();}
count++;
button.Text = count.ToString();
Controls.Clear();
Controls.Add(button);
label.Location = new Point(0,50);
Controls.Add(label);
for (int i=0;i<5000;i++){
try {
controlSet[0,i].Location =
new Point(0,100); Controls.Add(controlSet[0,i]);
} catch (Exception ex)
{label.Text = ex.Message;}
}
}
public static void Main(string[] args)
{
MyForm form = new MyForm();
Application.Run(form);
}
}
}
When the button is pressed the 2nd time, after about 2
minutes, an error message states that a window handle
could not be found. I know that the situation above
maybe hypothetical but but it would be useful to explore
the limitation and or possibly a bug with Control class.
How would you change the snippet so that the button
clicks good. The example is for 5000 TextBox
components. If I use only 1000, the button could be
pressed many times without a problem, also goes much
quicker.
Thanks,
Brad