c#在运行时更改标签(控件)位置
默认的工具提示对我来说不太合适,所以我使用Label控件及其“Visible”属性制作了一个自定义工具提示,当按下一个键时,它会作为一个错误弹出窗口。所以现在我尝试动态设置标签的位置,在我的例子中是文本框的位置,但它总是显示在表单的左上角。 方法如下: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
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
属性总是相对于它的容器,而不是表单。哦,它存在于拆分的容器中。那么,我该如何设置标签的位置呢?可能会重复