Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How can you resize a User Control at design time using code? [modified]

How can you resize a User Control at design time using code? [modified]

Scheduled Pinned Locked Moved Visual Basic
questiondesignhelpannouncement
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Hasler
    wrote on last edited by
    #1

    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 and SliderStripImage.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 of

    Me.Size.Width = SliderStripImage.Width
    Me.Size.Height = SliderStripImage.Height

    But 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

    P 1 Reply Last reply
    0
    • P Paul Hasler

      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 and SliderStripImage.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 of

      Me.Size.Width = SliderStripImage.Width
      Me.Size.Height = SliderStripImage.Height

      But 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

      P Offline
      P Offline
      Paul Hasler
      wrote on last edited by
      #2

      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 a New 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

      R 1 Reply Last reply
      0
      • P Paul Hasler

        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 a New 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

        R Offline
        R Offline
        riced
        wrote on last edited by
        #3

        1. In the MSDN example:

        Public Class MyCustomerInfoUserControl
        Inherits System.Windows.Forms.UserControl

        MyCustomerInfoUserControl 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

        P 1 Reply Last reply
        0
        • R riced

          1. In the MSDN example:

          Public Class MyCustomerInfoUserControl
          Inherits System.Windows.Forms.UserControl

          MyCustomerInfoUserControl 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

          P Offline
          P Offline
          Paul Hasler
          wrote on last edited by
          #4

          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 new System.Drawing.Size object with the dimensions we want, and then setting the Size property of our existing System.Windows.Forms.UserControl to have the same Width and Height values as this new object.

          R 1 Reply Last reply
          0
          • P Paul Hasler

            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 new System.Drawing.Size object with the dimensions we want, and then setting the Size property of our existing System.Windows.Forms.UserControl to have the same Width and Height values as this new object.

            R Offline
            R Offline
            riced
            wrote on last edited by
            #5

            As Julie Andrews sang "By George he's got it!" That's what I think is happening.

            Regards David R

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups