如何检查标签位置是否与按钮位置重叠?

时间:2018-01-11 07:00:45

标签: c# winforms

我在Windows窗体中有一个标签和一个按钮,我根据标签文字大小将标签的位置移动到左侧,使用以下代码:

lblsize = lblVal.Text.Length;
if (lblsize > 5)
{
    lblIncrement = (lblsize - 5) * 7;
    lblVal.Location = new Point(lblVal.Location.X , lblVal.Location.Y);
    btn.Location = new Point(btn.Location.X, btn.Location.Y);
    if (lblVal.Bounds.IntersectsWith(btn.Bounds))
    {

    }
    else
    {
        lblVal.Location = new Point(lblVal.Location.X - lblIncrement, lblVal.Location.Y);
    }

}

但是当我检查标签边界是否与按钮边界相交时,它不起作用。

1 个答案:

答案 0 :(得分:0)

您可以将两个控件的位置打印到控制台,以检查交叉点是否存在。如果是,并且您仍然因交叉检查而出现错误,那么它们位于不同的面板

顺便说一句,这些行什么都不做:(除了生成新对象......)

blVal.Location = new Point(lblVal.Location.X , lblVal.Location.Y);
btn.Location = new Point(btn.Location.X, btn.Location.Y);