我应该每次按下按钮时随机更改三个标签的位置(例如:label1
将在label2
的位置)。我如何获得标签的位置?
因此,我决定采取标签的位置并将它们存储在一个数组中。但是,我不知道如何获得标签的位置。我试过说double position = label1.location.X;
但它没有奏效。
我应该每次按下按钮时随机更改三个标签的位置(例如:label1
将在label2
的位置)。我如何获得标签的位置?
因此,我决定采取标签的位置并将它们存储在一个数组中。但是,我不知道如何获得标签的位置。我试过说double position = label1.location.X;
但它没有奏效。
获得使用值
label1.Left, label1.Top
设置使用值
label1.Location = new Point(x, y);
不要忘了包括
using System.Windows.Forms;
using System.Drawing; // to use System.Drawing.Point(Label.Left, Label.Top)
我已经写了,我希望可以帮助你的代码得到这个想法。 单击标签以获取其坐标。
using System;
using System.Windows.Forms;
using System.Drawing;
class LabelForm : Form
{
Label label1;
//
public LabelForm()
{
label1 = new Label();
label1.Text = "ClickMe";
label1.Location = new Point(10, 10); // This is the place where you set the location of your label. Currently, it is set to 10, 10.
label1.Click += new EventHandler(labelClick);
Controls.Add(label1);
}
//
static void Main(string[] args)
{
LabelForm lf = new LabelForm();
Application.Run(lf);
}
//
protected void labelClick(object o, EventArgs e)
{
// This is how you can get label's positions
int left = label1.Left;
int top = label1.Top;
MessageBox.Show("Left position of the label: " + left
+ "\nTop position of the label: " + top,
"", MessageBoxButtons.OK);
}
}
然后只需使用randomizer将值设置为Point(x,y)。请注意,您还应该检查窗口的宽度和高度,并减去标签的宽度和高度,以避免超出窗口边界。
您可以访问Bounds
属性,它是Rectangle
类型的对象,它具有组件的宽度,高度和x,y坐标。
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bounds(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.drawing.rectangle(v=vs.110).aspx
这是假设你有在WinForm类工作Label
编辑:使用一种更简单的属性是位置
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.location(v=vs.110).aspx
如果您只需看看的msdn文档即可你会发现有很多方法来获取这些数据。
界限给出的位置,但你不能实际移动与那些控制。您必须将位置设置为新点或更改Label.Left和Label.Top。 – TyCobb
Label.Left, Label.Top
位置只是一个无法修改的Point结构。
这是winforms吗? asp.net? wpf? –
@MauricioGracia winforms – Sarah
标签是否位于表单上的固定位置,您只需交换订单? – Brian