簡體   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