Sign in to follow this  

[.net] Updating multiple Windows.Forms.Labels very slow

This topic is 3721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, As part of a game when I hover the mouse over a unit I show that units stats. To display the stats I alter the text of one label per attribute. The problem is this is really slowing my programme down when I am doing other graphical stuff at the same time as updating the stats. Below is an example.
unit card
In the code below all the updated items are labels apart from the last which is a textbox.
hoverCp.Text = hoveringUnit.Completions.ToString();
hoverTd.Text = hoveringUnit.Touchdowns.ToString();
hoverIt.Text = hoveringUnit.Interceptions.ToString();
hoverCs.Text = hoveringUnit.Casualties.ToString();
hoverVp.Text = hoveringUnit.MostValuablePlayer.ToString();
hoverSp.Text = hoveringUnit.Spp.ToString();
hoverName.Text = hoveringUnit.Name;

hoverMove.Text = hoveringUnit.Ma.ToString();
hoverStrength.Text = hoveringUnit.St.ToString();
hoverAgility.Text = hoveringUnit.Ag.ToString();
hoverArmour.Text = hoveringUnit.Av.ToString();

hoverSkills.Text = null;
foreach (string skill in hoveringUnit.SkillNames)
{
	hoverSkills.AppendText(string.Format("{1}{0}", Environment.NewLine, skill));
}
I'm not sure why simply updating text should take so, comparatively, long. If needed the full game can be found here including graphics, 3.9MB. Many thanks, John

Share this post


Link to post
Share on other sites
Updating a label isn't a slow operation, but updating many of them frequently can be, because there's more machinery involved than just setting some text (validation, events, book keeping, and so on). There may be other problems beyond that snippet of code as well.

The thing is, using labels for this purpose isn't ideal. They consume signifigantly more resources (being controls) than a simple text string would, and simply drawing the strings directly on to your render surface is probably more ideal.

Share this post


Link to post
Share on other sites
One problem may be the refresh events of the form and labels.

When you are updating a lot of controls (GDI), tell the form and controls to not redraw until you are done. I think (although I could be wrong) that you can do this with the Layout stuff...

ofrmMyForm.SuspendLayout()
lblValue.SuspendLayout()
lblValue.Text = "Foo..."
lblValue.ResumeLayout()
ofrmMyForm.ResumeLayout()

Try it and see. There may be another property similar to it.

Share this post


Link to post
Share on other sites

This topic is 3721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this