Why is Button.Hide( ) so slow ???

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hi,

When I call Hide( ) on five buttons, it takes 600 mSec to complete.

The screen updates are very, very slow on a 2Ghz machine.

Ouch! How can this be so slow?

Code looks like:

foreach (MyButton button in this.buttonList)
{
button.Hide( );
}

Note: MyButton is derived from Forms.Button

I have tried "for loop" instead of foreach....No help.
I have tried button.Visible = false instead of Hide( )....No help.

Any thoughts?

Thanks, Jeff
 
Are you using any databindings to define any attributes? I have found that
manipulating some controls (my experience was with an attempt to clear a
comboBox with an valid data source) takes a real long time. When it happened
to me, I eventually reset the Datasource property to nothing (or NULL) and
the comboBox.Items.Clear() ran normally. Regardless, yours is still going
much faster than mine did.
 
Hi Jeff,

Based on my understanding, you want to hide 5 buttons in your winform
application, but it takes you 600ms to hide it.

I think your MyButton class may do some special work which makes the code
execute slow, because I tried to hide some normal buttons. For 100 buttons,
it takes only 60ms on my computer, code like this:

ArrayList al=new ArrayList();
private void Form1_Load(object sender, System.EventArgs e)
{
for(int i=0;i<100;i++)
{
Button bt=new Button();
bt.Location = new System.Drawing.Point(336, 32+i*10);
bt.Size = new System.Drawing.Size(88, 56);
bt.Text = "button1";
this.Controls.Add(bt);
al.Add(bt);
}
}

private void button2_Click(object sender, System.EventArgs e)
{
DateTime dt_start=DateTime.Now;
for(int i=0;i<al.Count;i++)
{
Button bt=al as Button;
bt.Visible=false;
}
DateTime dt_end=DateTime.Now;
TimeSpan ts=dt_end-dt_start;
MessageBox.Show("Sencods: "+ts.Seconds.ToString()+", ms:
"+ts.Milliseconds.ToString());
}
You may try my code snippet on your side.

If the problem is MyButton class, and you failed to figure out the problem,
please provide the source code of MyButton class for me to reproduce the
problem out, thanks.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeffrey,

Your comments and test code helped find the problem...
I forgot to account for the Layout event that gets fired when Button.Visible
is set to "false".
My Layout handler is quite involved.

Lesson learned: Don't forget about asynchronous events when debugging.

Thanks for all of your help.

Regards, Jeff
 
Hi Jeff,

I am glad your problem resolved, if you need further help, please feel free
to post, I will work with you. Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top