Resizing a label at runtime

Page 1 of 1

3 Replies - 3337 Views - Last Post: 06 August 2011 - 01:39 PM Rate Topic: -----

#1 Sir_Ross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-August 09

Resizing a label at runtime

Posted 23 September 2009 - 02:47 PM

Hi,

Just reading through "Sams teach yourself visual c# in 24 hours" and it has anc exercise in it which asks you to resize a label, please see the below text:

"Use two for loops nested within each other to size a label in two dimensions.
Have the outer loop change the Width property of the label from 1 to 100 and
have the inner loop change the Height property from 1 to 100. Donít be surprised
by the result; itís rather odd. Be sure to call the Resize() method of the
label after setting its height and width."

The exercise has confused me a little, as the labels individual height and width properties appear to be read-only, the control doesn't resize, and when checking their values in the debug window after changing them, they haven't changed.

The Resize() method mentioned in the exercise, appears to be an event and not a method, so I don't know how I can use that. Another property which does to be changeable is the 'Size' property, but I don't know what syntax I need to put in for this:


"label.Size = x, y;" ?

Anyway, here's what I have so far:


		private void btnResize_Click(object sender, EventArgs e)
		{
			for (int x = 1; x <= 100; x++)
			{
				lblResize.Width = x;
				lblResize.Resize;
				for (int y = 1; y <= 100; y++)
				{
					lblResize.Height = y;
					lblResize.Resize();
				}
			}
		}



The above code doesn't build, as it doesn't like the lblResize.Resize line. Can anyone point me in the right direction for this?
I don't really want to continue on with the book until I've completed the exercise...too damn stubborn!

Thanks for your time,

Ross

Is This A Good Question/Topic? 0
  • +

Replies To: Resizing a label at runtime

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Resizing a label at runtime

Posted 23 September 2009 - 04:28 PM

There are a number of things that you will want to do here. The first is that Label controls have an AutoSize property. You will need to set that to false. If it is set to true the label will not change size at all.

You need to create an event handler for the Resize event of the label. You can do this in design mode or in code. To do it at design mode click the Label. In the properties window for the label there is a little lightning bolt symbol. Click that. Scroll down until you see the Resize event. Double click that to create an event handler for the Resize event. To do it in code go to code view for the form and I would do this in the Load event for the form. Type the name of your Label with .Resize like this label1.Resize += after typing += intellisense will pop up and say press tab to create the event handler.

Setting the size of the label is not that hard. Something similar to this will do the job:

for (x = 1; x <= 100; x++)
	for (y = 1; y <= 100; y++)
		label1.Size = new Size(x, y);



Hopefully that will help you out.
Was This Post Helpful? 1
  • +
  • -

#3 Sir_Ross  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-August 09

Re: Resizing a label at runtime

Posted 24 September 2009 - 09:52 AM

That's perfect, seems to have worked the way the excercise wanted it to, thanks for you help!
Was This Post Helpful? 0
  • +
  • -

#4 FlatTimmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-August 11

Re: Resizing a label at runtime

Posted 06 August 2011 - 01:39 PM

It looks like there is a typo for this exercise in the book. The author's own downloadable solutions use the Refresh() method, not the Resize() method. The author's solution does not employ an event handler.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1