2

[Edit] - See edit below, I think I've figure it out!

I would like to do a similar thing to this => How to make a floating (tooltip) control in Windows.Forms? but was wondering if there was a better way now? I have tried this approach but my window seems to hang around and I'm having issues with it generally.

I know how to make tooltips for form controls (here) but the items in a listView don't seem obvious.

For my particular usage, I have a list of items in a ListView control. I would love if on ItemMouseHover I could get that little yellow tooltip box that is used to show some info or a bit of text related to the item in question.

I've created a tooltip (I think) and it just doesn't fire. I'm certainly getting the event but not the tool tip. Clearly I'm not doing this correctly. I tried variations on the below but all seem to fail miserably.

private void ShowLittleBox(object sender, ListViewItemMouseHoverEventArgs e)
{
    toolTip1 = new ToolTip
    {ToolTipTitle = "File Summary",AutomaticDelay = 10,BackColor = Color.Yellow};
    RichTextBox rtb = new RichTextBox //I needed some window - I used a lot of them
    {AutoSize = true,Visible = true,Text="Please note, only first 5 lines are shown"};

    string myTextDerivedFromObject = "Eventually this will be dynamic ... ";
    toolTip1.Show(myTextDerivedFromObject,rtb); //Also tried activewindow, my listview control etc.

}

Needless to say, I've been on this "swing-set" for about 4 hours give or take and need a little push ...

[EDIT]: I think I cracked it - I found that ListView a contains a separate property for allowing each item to have it's own tooltip. ListViewItem.ToolTipText Property So in the end, I just had to set that to true and add the items tooltip text when I build it.

Community
  • 1
  • 1
Sisyphus
  • 679
  • 6
  • 21

2 Answers2

1

I did the same thing like this, i put a panel with a label control on form and false the visible property of panel and then on mouseEnter event of any object i get the X,Y position and show the panel at that position.

Point locationOnForm = AnyObject.FindForm().PointToClient(AnyObject.Parent.PointToScreen(AnyObject.Location));
lblQue.Text = "Text to be shown";
panel8.Location = new Point(locationOnForm.X, locationOnForm.Y+27);
panel8.Size = new Size(470, 30);
panel8.BackColor = Color.SkyBlue;
this.Controls.Add(panel8);
panel8.BringToFront();
panel8.Show();

Hope it helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
Mogli
  • 1,972
  • 11
  • 34
  • 67
  • Thanks. Not sure that's precisely it - I liked the tool tip as it would hover over and disappear and could be outside the window. Do you know how I would detect which listViewItem I was on for this to fill with text from that listViewItem? – Sisyphus Apr 20 '13 at 06:41
1

Okay, so as mentioned, I had a list of files in a ListView controls. To get a sneak peak at what was in the file, I ended up using the ListViewItem.Tag Propterty once a kind SO user told me how to do it (from another of my questions) Once I'd attached an object (the loaded file in this case) and while loading I extracted the first couple of lines and set them to the ListViewItem.ToolTipText Property.

Here is a contrived example to enable the tooltips to be shown from the listViewItems: (For those who are very new to C#/VisualStudio: You'll need to make a form "form1", and add a ListView control "listView1" and a button "button1").

     namespace SneakPeak
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                listView1.ShowItemToolTips = true;

                ListViewItem ItemA = new ListViewItem("my item A");
                myObject ObjectA = new myObject("the description for A");
                ItemA.Tag = ObjectA;
                ItemA.ToolTipText = ((myObject) (ItemA.Tag)).ToString();

                ListViewItem ItemB = new ListViewItem("my item B"); 
                myObject ObjectB = new myObject("the description for B");
                ItemB.Tag = ObjectB;
                ItemB.ToolTipText = ((myObject)(ItemB.Tag)).ToString();

                ListViewItem ItemC = new ListViewItem("my item C"); 
                myObject ObjectC = new myObject("the description for C");
                ItemC.Tag = ObjectC;
                ItemC.ToolTipText = ((myObject)(ItemC.Tag)).ToString();

                ListViewItem ItemD = new ListViewItem("my item D");
                myObject ObjectD = new myObject("the last of the descriptions - description for D that goes off the side of the window");
                ItemD.Tag = ObjectD;
                ItemD.ToolTipText = ((myObject)(ItemD.Tag)).ToString();

                listView1.Items.Add(ItemA);
                listView1.Items.Add(ItemB);
                listView1.Items.Add(ItemC);
                listView1.Items.Add(ItemD);

            }

            public class myObject
            {
                public string filePreview = "";
                public override string ToString()
                {
                    return filePreview;
                }
                public myObject(string s)
                {
                    filePreview = s;
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }

Ideally, I'd like to be able to control the fonts etc through a "rich text box" that behaved like a tool tip but haven't found the ideal way of doing that yet.

Let me know if there is an easier way!!!

[EDIT:] after doing all this, I now find a similar question on SO How to set tooltip for a ListviewItem

Community
  • 1
  • 1
Sisyphus
  • 679
  • 6
  • 21