How can you resize a User Control at design time using code? [modified]
-
The Situation I have created a user control which is similar to a standard slider control but which has quite at few different features such as multiple slider pointers. The control uses an image to visualise the slider strip background.
Private mySliderStripImage As Image = My.Resources.Horizontal\_Slider\_\_\_Basic\_V1\_\_\_Strip
To make the user control more flexible at design time, I've included a SliderStripImage property.
<Description("(As Image) The image to use for the Slider Strip.")> \_ Property SliderStripImage() As Image Get SliderStripImage = mySliderStripImage End Get Set(ByVal value As Image) mySliderStripImage = value End Set End Property
The control functions correctly, and when I'm working with the control at design time I can change the SliderStripImage property. The Problem If I change the SliderStripImage property to an image of a different size at design time, I have to manually resize the control to suit. That is, if I change to a larger image, I have to manually increase the size of the control in order to be able to see all of the new larger image. I can get the dimensions of the new image with
SliderStripImage.Width
andSliderStripImage.Height
, but how can I get the size of the control to update itself with these values at design time?:confused: I thought I'd be able to do it with something along the lines ofMe.Size.Width = SliderStripImage.Width
Me.Size.Height = SliderStripImage.HeightBut this doesn't work. I get the message "Expression is a value and therefore cannot be the target of an assignment." Any advice greatly appreciated. Paul
modified on Monday, February 16, 2009 4:38 AM
-
The Situation I have created a user control which is similar to a standard slider control but which has quite at few different features such as multiple slider pointers. The control uses an image to visualise the slider strip background.
Private mySliderStripImage As Image = My.Resources.Horizontal\_Slider\_\_\_Basic\_V1\_\_\_Strip
To make the user control more flexible at design time, I've included a SliderStripImage property.
<Description("(As Image) The image to use for the Slider Strip.")> \_ Property SliderStripImage() As Image Get SliderStripImage = mySliderStripImage End Get Set(ByVal value As Image) mySliderStripImage = value End Set End Property
The control functions correctly, and when I'm working with the control at design time I can change the SliderStripImage property. The Problem If I change the SliderStripImage property to an image of a different size at design time, I have to manually resize the control to suit. That is, if I change to a larger image, I have to manually increase the size of the control in order to be able to see all of the new larger image. I can get the dimensions of the new image with
SliderStripImage.Width
andSliderStripImage.Height
, but how can I get the size of the control to update itself with these values at design time?:confused: I thought I'd be able to do it with something along the lines ofMe.Size.Width = SliderStripImage.Width
Me.Size.Height = SliderStripImage.HeightBut this doesn't work. I get the message "Expression is a value and therefore cannot be the target of an assignment." Any advice greatly appreciated. Paul
modified on Monday, February 16, 2009 4:38 AM
Well after a few more hours of banging my head against the keyboard I've managed to get a solution that works. But to be honest I don't fully understand why it works. Perhaps someone with a bit more knowledge could explain it? While reading more about the System.Windows.Forms.UserControl class in the MSDN (http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.aspx[^]), I noticed the following lines in some example code:
' Size the user control. Size = New System.Drawing.Size(375, 150)
So I modified my SliderStripImage property to
<Description("(As Image) The image to use for the Slider Strip.")> \_ Property SliderStripImage() As Image Get SliderStripImage = mySliderStripImage End Get Set(ByVal value As Image) mySliderStripImage = value Size = New System.Drawing.Size(SliderStripImage.Width, SliderStripImage.Height) End Set End Property
This raises some questions: 1.Why haven't we had to specify what
Size
refers to with something like Me.Size? 2.Why are we creating aNew
object every time the property changes? Doesn't the control already exist? Of course, nobody likes putting lines of code in their work that they don't understand themselves. :sigh: Can anyone shed some light on this?:confused: Regards Paul -
Well after a few more hours of banging my head against the keyboard I've managed to get a solution that works. But to be honest I don't fully understand why it works. Perhaps someone with a bit more knowledge could explain it? While reading more about the System.Windows.Forms.UserControl class in the MSDN (http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.aspx[^]), I noticed the following lines in some example code:
' Size the user control. Size = New System.Drawing.Size(375, 150)
So I modified my SliderStripImage property to
<Description("(As Image) The image to use for the Slider Strip.")> \_ Property SliderStripImage() As Image Get SliderStripImage = mySliderStripImage End Get Set(ByVal value As Image) mySliderStripImage = value Size = New System.Drawing.Size(SliderStripImage.Width, SliderStripImage.Height) End Set End Property
This raises some questions: 1.Why haven't we had to specify what
Size
refers to with something like Me.Size? 2.Why are we creating aNew
object every time the property changes? Doesn't the control already exist? Of course, nobody likes putting lines of code in their work that they don't understand themselves. :sigh: Can anyone shed some light on this?:confused: Regards Paul1. In the MSDN example:
Public Class MyCustomerInfoUserControl
Inherits System.Windows.Forms.UserControlMyCustomerInfoUserControl has a property Size inherited from UserControl hence any reference to Size in a method of of the MyCustomerInfoUserControl class does not need to be qualified. It refers to the property of the current instance. 2. The Size property is of type System.Drawing.Size. The new is creating a new instance of a System.Drawing.Size object not a new MyCustomerInfoUserControl control.
Regards David R
-
1. In the MSDN example:
Public Class MyCustomerInfoUserControl
Inherits System.Windows.Forms.UserControlMyCustomerInfoUserControl has a property Size inherited from UserControl hence any reference to Size in a method of of the MyCustomerInfoUserControl class does not need to be qualified. It refers to the property of the current instance. 2. The Size property is of type System.Drawing.Size. The new is creating a new instance of a System.Drawing.Size object not a new MyCustomerInfoUserControl control.
Regards David R
Ah ha! Thanks for the lightbulb moment David! :-D 1. This works in my code because my slider user control would also inherit from
System.Windows.Forms.UserControl
right? I'm using Visual Studio 2008 Express, and when I added a User Control, the template would have set up the inheritance in the hidden code. I didn't see it in the visible code which helped my confusion. 2. I think I get it. We're creating a newSystem.Drawing.Size
object with the dimensions we want, and then setting the Size property of our existingSystem.Windows.Forms.UserControl
to have the same Width and Height values as this new object. -
Ah ha! Thanks for the lightbulb moment David! :-D 1. This works in my code because my slider user control would also inherit from
System.Windows.Forms.UserControl
right? I'm using Visual Studio 2008 Express, and when I added a User Control, the template would have set up the inheritance in the hidden code. I didn't see it in the visible code which helped my confusion. 2. I think I get it. We're creating a newSystem.Drawing.Size
object with the dimensions we want, and then setting the Size property of our existingSystem.Windows.Forms.UserControl
to have the same Width and Height values as this new object.