简体   繁体   中英

Tool tip on cell in TableLayoutPanel

I'm building a simple app to get some practise with the .net library. Basically, I want to populate a form with cells of different colours. This works great (I have a list ColourSwatches with hex values in it):

private void AddListToTable()
{
    TableLayoutPanel tlp = new TableLayoutPanel();
    tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;
    tlp.RowCount = 5;
    tlp.GrowStyle = TableLayoutPanelGrowStyle.AddColumns;
    tlp.Padding = new Padding(1, 1, 4, 5);
    tlp.AutoSize = true;

    foreach (String colour in ColourSwatches)
    {
        Panel panel = new Panel();
        panel.BackColor = System.Drawing.ColorTranslator.FromHtml("#" + colour);
        panel.Size = new Size(100, 100);            
        tlp.Controls.Add(panel);
    }

    Form.Controls.Add(tlp);
}

What I'd like to add is the ability to hover over the panel and get the hex value to display in a tool tip window.

What's the simplest way to achieve this?

If I wasn't using the same name for all of my panels I guess I could use that, but I can't think of an alternative approach.

Any ideas?

Thanks!

As far as I know The Panel object does not have a tool tip property out of the box. There are many plugins you could use to help out such as JQueryUI, Ajax tool kit.

Alternatively, the TableRow and the TableCell classes have tool tip properties, so you could create your colour swatch using a table.

foreach (String colour in ColourSwatches)
{
    TableRow Row = new TableRow();
    Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#" + colour);
    Row.ToolTip = "mycolor";
    table.rows.Add(row);
}

create a table in your aspx page:

<asp:Table runat="server" id="colortable"></asp:table>

then in code behind add the row to the tables row collection like:

TableRow newrow = new TableRow();
colortable.Rows.Add(newrow);

Cells can be added to the rows cell collection like:

TableCell newcell = new TableCell();
newrow.Cells.Add(newcell);

The error suggests that you are tying to add the TableCell directly the pages control collection. A TableCell needs to be added to a TableRow. And A TableRow added to a Table. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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