slow button display

M

Mark Davis

Hey there,

I have a windows form with lots of buttons (36) that I create at runtime
using a simple loop:

for (int row = 0; row < (NoRows); row++)
{
for (int column = 0; column < (NoColumns); column++)
{
this.buttonArray[row][column] = new System.Windows.Forms.Button();
this.buttonArray[row][column].Size = new
System.Drawing.Size(ButtonWidth, ButtonHeight);
this.buttonArray[row][column].Font = Global.Const.BoldFont;
this.buttonArray[row][column].Visible = false;
this.buttonArray[row][column].TabIndex = 0;
this.buttonArray[row][column].BorderStyle =
System.Windows.Forms.BorderStyle.None;
this.buttonArray[row][column].BackColor =
Global.Const.BackgroundColour;
this.buttonArray[row][column].Visible = true;
this.buttonArray[row][column].Text =
this.buttonLabelsAlpha[row][column];
this.buttonArray[row][column].TabStop = false;
int top = 55 + ((ButtonHeight * row) + (VerticalSpacing * (row -
1)));
int left = 10 + ((ButtonWidth * column) + (HorizontalSpacing *
(column - 1)));
this.buttonArray[row][column].Location = new
System.Drawing.Point(left, top);
this.buttonArray[row][column].Click += new EventHandler(KeyClick);
this.Controls.Add(this.buttonArray[row][column]);
}
}

but this takes about a second to be rendered to the screen, even if i simply
hide and show the form by setting its visible property, and hence does not
look very good. Is there anyway I can speed up the display?

Thanks for any advise

Mark
 
G

Guest

Are you calling SuspendLayout on the form before adding them and then
ResumeLayout afterward?
 
M

Mark Davis

Hi Chris,

Yes I am. Doesn't seem to make any difference though.


Mark


Are you calling SuspendLayout on the form before adding them and then
ResumeLayout afterward?


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


Mark Davis said:
Hey there,

I have a windows form with lots of buttons (36) that I create at runtime
using a simple loop:

for (int row = 0; row < (NoRows); row++)
{
for (int column = 0; column < (NoColumns); column++)
{
this.buttonArray[row][column] = new System.Windows.Forms.Button();
this.buttonArray[row][column].Size = new
System.Drawing.Size(ButtonWidth, ButtonHeight);
this.buttonArray[row][column].Font = Global.Const.BoldFont;
this.buttonArray[row][column].Visible = false;
this.buttonArray[row][column].TabIndex = 0;
this.buttonArray[row][column].BorderStyle =
System.Windows.Forms.BorderStyle.None;
this.buttonArray[row][column].BackColor =
Global.Const.BackgroundColour;
this.buttonArray[row][column].Visible = true;
this.buttonArray[row][column].Text =
this.buttonLabelsAlpha[row][column];
this.buttonArray[row][column].TabStop = false;
int top = 55 + ((ButtonHeight * row) + (VerticalSpacing * (row -
1)));
int left = 10 + ((ButtonWidth * column) + (HorizontalSpacing *
(column - 1)));
this.buttonArray[row][column].Location = new
System.Drawing.Point(left, top);
this.buttonArray[row][column].Click += new EventHandler(KeyClick);
this.Controls.Add(this.buttonArray[row][column]);
}
}

but this takes about a second to be rendered to the screen, even if i
simply hide and show the form by setting its visible property, and hence
does not look very good. Is there anyway I can speed up the display?

Thanks for any advise

Mark
 
H

Hilton

Mark,

There is a lot you can do to improve this code:
1. Combine .Size and .Location into .Bounds
2. Replace this.Controls.Add (x) with "x.Parent = this"
3. Replace " = new EventHandler(KeyClick);" with "= ehKeyClick" and have
"ehKeyClick = new EventHandler(KeyClick);" outside the loop
4. Remove "this.buttonArray[row][column].Visible = false;"
5. Remove "this.buttonArray[row][column].Visible = true;"
6. Move "Global.Const.BoldFont" outside the loop
7. Move Global.Const.BackgroundColour outside the loop

Let me know how much these changes help.

Hilton
 
M

Mark Davis

Hi Hilton,

Thanks for the tips. They improved things by about 20%. I think its
acceptable performance now.

Regards

Mark

Hilton said:
Mark,

There is a lot you can do to improve this code:
1. Combine .Size and .Location into .Bounds
2. Replace this.Controls.Add (x) with "x.Parent = this"
3. Replace " = new EventHandler(KeyClick);" with "= ehKeyClick" and have
"ehKeyClick = new EventHandler(KeyClick);" outside the loop
4. Remove "this.buttonArray[row][column].Visible = false;"
5. Remove "this.buttonArray[row][column].Visible = true;"
6. Move "Global.Const.BoldFont" outside the loop
7. Move Global.Const.BackgroundColour outside the loop

Let me know how much these changes help.

Hilton



Mark Davis said:
Hey there,

I have a windows form with lots of buttons (36) that I create at runtime
using a simple loop:

for (int row = 0; row < (NoRows); row++)
{
for (int column = 0; column < (NoColumns); column++)
{
this.buttonArray[row][column] = new System.Windows.Forms.Button();
this.buttonArray[row][column].Size = new
System.Drawing.Size(ButtonWidth, ButtonHeight);
this.buttonArray[row][column].Font = Global.Const.BoldFont;
this.buttonArray[row][column].Visible = false;
this.buttonArray[row][column].TabIndex = 0;
this.buttonArray[row][column].BorderStyle =
System.Windows.Forms.BorderStyle.None;
this.buttonArray[row][column].BackColor =
Global.Const.BackgroundColour;
this.buttonArray[row][column].Visible = true;
this.buttonArray[row][column].Text =
this.buttonLabelsAlpha[row][column];
this.buttonArray[row][column].TabStop = false;
int top = 55 + ((ButtonHeight * row) + (VerticalSpacing * (row -
1)));
int left = 10 + ((ButtonWidth * column) + (HorizontalSpacing *
(column - 1)));
this.buttonArray[row][column].Location = new
System.Drawing.Point(left, top);
this.buttonArray[row][column].Click += new EventHandler(KeyClick);
this.Controls.Add(this.buttonArray[row][column]);
}
}

but this takes about a second to be rendered to the screen, even if i
simply hide and show the form by setting its visible property, and hence
does not look very good. Is there anyway I can speed up the display?

Thanks for any advise

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top