Moving Controls (x,y)

  • 14 years ago

    Let's say I have a Label control (Label1) at 10,10 on my form, and upon clicking a command button I want that label to no longer be at 10,10 but go to 100,100 or any other spot I choose.

    How would I go about accomplishing this?

     

    Thanks

  • 14 years ago

    you can change the Location property of the control.

  • 14 years ago

    I did some reading on that and it doesn't work right

     

    I have tried label1.Location = 100,100

    label1.Location ="100,100"

    label1.Location(100,100)

    I dont know how to properly write the statement to get it to work ... I read the MSDN but all the stuff it talks about relating to the term Location doesn't show me what I want to do.

  • 14 years ago
    The Location property is type Point, which means to set it you have to create a Point object and assign that to the property.  The MSDN help topic for the Control.Location property has a code example so you mustn't have read it very carefully or not at all.  I suggest that you read it the help topic for the property and follow the code provided.

  • 14 years ago
    Hi,
    First, you have to Add System.Drawing to your Project Reference,
    Then to change your Control Location you have to assign it to new System.Drawing.Point:
    Ex:    Label1.Location = New System.Drawing.Point(100,100)






  • 14 years ago

    jmcilhinney,

    when I searched the MSDN I typed in "location" and it brought up a whole pile of stuff everything but Control.Location. I did not know that was the term I needed to find exactly what I wanted. I now have a better understanding and realize I gotta be more secific when searching MSDN :)

  • 14 years ago

    If you want information on the Location property of the Label class then you should just go straight to the Label member listing topic and click on the link to the Location property.  It's inherited from the Control class so it's not specific to the Label class, but the Label member listing includes all inherited members.

  • 14 years ago

    It sounds stupid I know, but i dont think the location property allows you to actually change the location. Instead, I would use the top, and left properties to change it.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Label1.Top = 100

    Label1.Left = 100

    End Sub

    That is the only way i can do that and it's always worked for me.

    Hope this helps Big Smile [:D]

    VB Spanner

  • 14 years ago

    Of course the Location property allows you to change the location.  It is type Point, which is a value type, so you cannot set the X or Y property of the Location directly.  If you want to set ONLY the X or ONLY the Y then it IS appropriate to use the Left or Top property of the control.  If you want to set BOTH then you can still use the Left and Top properties if you want, but if you do you ABSOLUTELY should call SuspendLayout beforehand and ResumeLayout afterwards.  Otherwise the control will be refreshed on screen twice, which is nunecessary and wasteful and may even be visible to the user.  The more appropriate way is to create a new Point object and assign it to the Location property.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook