簡體   English   中英

將鼠標懸停在TableLayoutPanel中的單元格上時顯示文本-C#

[英]Show text when hovering over cell in TableLayoutPanel - C#

我有一個以編程方式創建的TableLayoutPanel,其每個單元格都包含一個Panel。 每個面板都有一個自定義標簽。 (Labels的Enabled屬性設置為false;不確定是否有區別。)每當用戶將鼠標懸停在Label上時,我都希望顯示Label的文本。

根據我的閱讀,工具提示是實現此目的的一種好方法,但我一直無法使其工作。

TableLayoutPanel的簡稱為“ tlp”,並且是表單的成員,以便於訪問(同樣使用ToolTip,即名稱為“ toolTip”)。

現在,我只是想獲取任何文本。 一旦可以使用,我將在這里用Label的文本替換字符串。

private void hoverOverSpace(object sender, EventArgs e)
{
    int row = tlp.GetRow((Panel)sender);
    int col = tlp.GetColumn((Panel)sender);

    toolTip.Show("Does this work?", tlp.GetControlFromPosition(col, row).Controls[0]);
    //toolTip.Show("Does this work?", tlp.GetControlFromPosition(col, row));
}

我顯示工具提示的任何嘗試都沒有成功。 我做錯了什么/是否有更好的方法來完成我要完成的工作?

編輯:我試圖將toolTip添加到每個面板,但仍然沒有任何反應

// Add Panels to TableLayoutPanel
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        // Create new Panel
        Panel space = new Panel()
       {
            Size = new Size(45, 45),
            Dock = DockStyle.Fill,
            Margin = new Padding(0)
        };

        space.MouseClick += new MouseEventHandler(clickOnSpace);

        CustomLabel info = new CustomLabel(false, 0, Color.White);      // Create new CustomLabel
        space.Controls.Add(info);   // Add CustomLabel to Panel
        tlp.Controls.Add(space, j, i);      // Add Panel to TableLayoutPanel

        toolTip = new ToolTip();
        toolTip.SetToolTip(space, info.Text);
    }
}

該答案基於以下答案中呈現的代碼: tablelayoutPanel從鼠標懸停獲取單元格位置,方法是:Aland Li Microsoft CSS

    #region GetPosition
    // Modified from answer to: tablelayoutPanel get cell location from mouse over
    // By:  Aland Li Microsoft CSS
    // https://social.msdn.microsoft.com/Forums/windows/en-US/9bb6f42e-046d-42a0-8c83-febb1dcf98a7/tablelayoutpanel-get-cell-location-from-mouse-over?forum=winforms

//The method to get the position of the cell under the mouse.
private TableLayoutPanelCellPosition GetCellPosition(TableLayoutPanel panel, Point p)
{

    //Cell position
    TableLayoutPanelCellPosition pos = new TableLayoutPanelCellPosition(0, 0);
    //Panel size.
    Size size = panel.Size;
    //average cell size.
    SizeF cellAutoSize = new SizeF(size.Width / panel.ColumnCount, size.Height / panel.RowCount);

    //Get the cell row.
    //y coordinate
    float y = 0;
    for (int i = 0; i < panel.RowCount; i++)
    {
        //Calculate the summary of the row heights.
        SizeType type = panel.RowStyles[i].SizeType;
        float height = panel.RowStyles[i].Height;
        switch (type)
        {
            case SizeType.Absolute:
                y += height;
                break;
            case SizeType.Percent:
                y += height / 100 * size.Height;
                break;
            case SizeType.AutoSize:
                y += cellAutoSize.Height;
                break;
        }
        //Check the mouse position to decide if the cell is in current row.
        if ((int)y > p.Y)
        {
            pos.Row = i;
            break;
        }
    }

    //Get the cell column.
    //x coordinate
    float x = 0;
    for (int i = 0; i < panel.ColumnCount; i++)
    {
        //Calculate the summary of the row widths.
        SizeType type = panel.ColumnStyles[i].SizeType;
        float width = panel.ColumnStyles[i].Width;
        switch (type)
        {
            case SizeType.Absolute:
                x += width;
                break;
            case SizeType.Percent:
                x += width / 100 * size.Width;
                break;
            case SizeType.AutoSize:
                x += cellAutoSize.Width;
                break;
        }
        //Check the mouse position to decide if the cell is in current column.
        if ((int)x > p.X)
        {
            pos.Column = i;
            break;
        }
    }

    //return the mouse position.
    return pos;
}
#endregion

它使用由參考代碼計算的TableLayoutPanelCellPosition來獲取該位置的Control (如果有),並在TableLayoutPanel.MouseHover事件上將其Text屬性顯示為ToolTip

private void tableLayoutPanel1_MouseHover(object sender, EventArgs e)
{
    Point pt = tableLayoutPanel1.PointToClient(Control.MousePosition);
    TableLayoutPanelCellPosition pos = GetCellPosition(tableLayoutPanel1, pt);
    Control c = tableLayoutPanel1.GetControlFromPosition(pos.Column, pos.Row);
    if (c != null)
    {
        toolTip1.Show(c.Text, tableLayoutPanel1, pt, 500);
    }
}

編輯:

我錯過了在TLP中填充控件的Dock屬性設置為DockStyle.Fill`的信息。 放置在TLP中的此類控件將接收鼠標事件而不是TLP。 因此,請修復此方法。

private void showtip(object sender, EventArgs e)
{
    Point pt = tableLayoutPanel1.PointToClient(Control.MousePosition);
    TableLayoutPanelCellPosition pos = GetCellPosition(tableLayoutPanel1, pt);
    Control c = tableLayoutPanel1.GetControlFromPosition(pos.Column, pos.Row);
    if (c != null && c.Controls.Count > 0)
    {
        toolTip1.Show(c.Controls[0].Text, tableLayoutPanel1, pt, 500);
    }
}

然后像這樣連接每個PanelLabel分組:

this.panel4.MouseHover += new System.EventHandler(this.showtip);
this.label4.MouseHover += new System.EventHandler(this.showtip);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM