Update label location in C#?

前端 未结 6 401
春和景丽
春和景丽 2020-12-15 23:24

I have a method that returns a value, and I want this value to be the new location of a label in a windows form application. but I\'m being told that a label\'s location is

相关标签:
6条回答
  • 2020-12-15 23:30

    Use the Left property to change X coordinate of a Label

    objectA.Left = 100;
    
    0 讨论(0)
  • 2020-12-15 23:30

    You can only set properties of structs if you have a direct reference to that struct:

    Point loc = objectA.Location;
    loc.X = (int)A.position;
    objectA.Location = loc;
    
    0 讨论(0)
  • 2020-12-15 23:33

    the Location property is of type Point, which is a value type. Therefore, the property returns a copy of the location value, so setting X on this copy would have no effect on the label. The compiler sees that and generates an error so that you can fix it. You can do that instead :

    objectA.Location = new Point((int)A.position, objectA.Location.Y);
    

    (the call to Refresh is useless)

    0 讨论(0)
  • 2020-12-15 23:35

    This works to me

    this.label1.Location = new Point(10, 10);
    

    You even do not need to call Refresh or SuspendLayout etc.

    so this should help you

    this.label1.Location = new Point((int)A.position, (int)A.otherpos);
    
    0 讨论(0)
  • 2020-12-15 23:44
    objectA.Location = new Point((int)A.position, objectA.Location.Y);
    objectA.Refresh();
    

    Location is no a variable, just a public Property. Changing variables through properties is a bady idea unless you have events that update the parent.

    0 讨论(0)
  • 2020-12-15 23:54

    objectname.Location = System.Drawing.Point(100,100);

    0 讨论(0)
提交回复
热议问题