Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#在运行时更改标签(控件)位置_C#_Dynamic_Location_Label - Fatal编程技术网

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

c#在运行时更改标签(控件)位置,c#,dynamic,location,label,C#,Dynamic,Location,Label,默认的工具提示对我来说不太合适,所以我使用Label控件及其“Visible”属性制作了一个自定义工具提示,当按下一个键时,它会作为一个错误弹出窗口。所以现在我尝试动态设置标签的位置,在我的例子中是文本框的位置,但它总是显示在表单的左上角。 方法如下: void ShowCustomToolTip(string text, Control targetControl, int duration = 1000, int x = 0, int y = 0) { cus

默认的工具提示对我来说不太合适,所以我使用Label控件及其“Visible”属性制作了一个自定义工具提示,当按下一个键时,它会作为一个错误弹出窗口。所以现在我尝试动态设置标签的位置,在我的例子中是文本框的位置,但它总是显示在表单的左上角。

方法如下:

    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();
    }
我怎样才能做到这一点?谢谢

问题在于给出了您在当前容器中的位置。您只需要获得控件相对于窗体的绝对位置,如下所示:

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();
}

我猜
targetControl.Location
是控件在其容器中的位置(即不相对于窗口)?@DavidG:是的,它是主窗体中的一个控件的位置,我希望标签出现在该位置。我是说,
targetControl
是否存在于另一个控件中,如选项卡视图或面板?
Location
属性总是相对于它的容器,而不是表单。哦,它存在于拆分的容器中。那么,我该如何设置标签的位置呢?可能会重复