繁体   English   中英

c#在运行时更改标签(控件)的位置

[英]c# change Label (Control) location at runtime

默认工具提示不适用于我,因此我使用标签控件及其“可见”属性作为自定义工具提示,并在按下键时将其作为错误弹出窗口。 因此,现在我尝试动态设置标签的位置(在我的情况下为textBox的位置),但它始终显示在表单的左上角。

方法如下:

    void ShowCustomToolTip(string text, Control targetControl, int duration = 1000, int x = 0, int y = 0)
    {
        customToolTip.Text = text;
        customToolTip.Visible = true;

        // the crucial line that needs to be changed, I guess
        customToolTip.Location = new Point(targetControl.Location.X + x, targetControl.Location.Y + y);

        Set.Timer(duration);
        customToolTip.Hide();
    }

我该怎么做? 谢谢!

问题是Control.Location给出了您在当前容器中的位置。 您只需要相对于如下形式获得控件的绝对位置:

void ShowCustomToolTip(string text, Control targetControl, int duration = 1000, int x = 0, int y = 0)
{
    customToolTip.Text = text;
    customToolTip.Visible = true;

    Point absoluteLocation = targetControl.FindForm().PointToClient(
        targetcontrol.Parent.PointToScreen(control.Location));

    // the crucial line that needs to be changed, I guess
    customToolTip.Location = new Point(absoluteLocation.X + x, absoluteLocation.Y + y);

    Set.Timer(duration);
    customToolTip.Hide();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM