none
Using different color for different part of text in the tooltip RRS feed

  • Question

  • Hi,

     I have MS chart in windows form.

    In chart's tooltip I want to use different color for each line of text.

    I derived the tooltip class.

    It is always drawing last line.

    public class MyToolTip : ToolTip { public MyToolTip () { this.OwnerDraw = true; this.Draw += new DrawToolTipEventHandler(OnDraw); } public MyToolTip (System.ComponentModel.IContainer Cont) { this.OwnerDraw = true; this.Draw += new DrawToolTipEventHandler(OnDraw); } Color[] colors = { System.Drawing.Color.Red, System.Drawing.Color.Green, System.Drawing.Color.Black }; private void OnDraw(object sender, DrawToolTipEventArgs e) { string s = e.ToolTipText; string[] s1 = s.Split('\n'); int j=0; foreach (string i in s1) { DrawToolTipEventArgs newArgs = new DrawToolTipEventArgs(e.Graphics, e.AssociatedWindow, e.AssociatedControl, e.Bounds, i, this.BackColor, colors[j], e.Font);

    j++; newArgs.DrawBackground(); newArgs.DrawBorder(); newArgs.DrawText(TextFormatFlags.TextBoxControl); } } }


    Please help me to resolve my problem.



    • Edited by sgrm123 Friday, May 24, 2019 6:54 AM
    Thursday, May 23, 2019 1:37 PM

All replies

  • Hi sgrm123,

    Maybe you can use a usercontrol to achieve it, rather than tooltip. Here is the simple demo you can refer to.

    UserControl:

        public partial class UCtips : UserControl
        {
            public UCtips()
            {
                InitializeComponent();
            }
    
            public string XX;
            public double YY;
    
            private void UCtips_Load(object sender, EventArgs e)
            {
                label1.ForeColor = Color.Red;
                label1.Text = "X = " + XX;
                label2.ForeColor = Color.Blue;
                label2.Text = "Y = " + YY.ToString();
            }
        }

    Form1:

        List<string> Xname = new List<string> { "a", "b", "c", "d", "e", "f" };
        List<int> Y1 = new List<int> { 2, 8, 3, 4, 10, 6 };
        List<int> Y2 = new List<int> { 4, 2, 6, 15, 4, 9 };
    
        private void Form1_Load(object sender, EventArgs e)
        {
            chart1.Titles.Add("Line Chart");
            chart1.Series.Clear();
            chart1.Series.Add("Y1");
            chart1.Series.Add("Y2");
            chart1.Series["Y1"].Points.DataBindXY(Xname, Y1);
            chart1.Series["Y2"].Points.DataBindXY(Xname, Y2);
            chart1.Series[0].ChartType = SeriesChartType.Line;
            chart1.Series[1].ChartType = SeriesChartType.Line;
            chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
            chart1.Series[1].MarkerStyle = MarkerStyle.Circle;
        }
    
        int x;
    
        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {
            Point mousePoint = new Point(e.X, e.Y);
            chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(mousePoint, false);
            try
            {
                double xValue = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
                x = (int)Math.Round(xValue, 0);
            }
            catch { }
        }
    
        string xtip;
        double ytip;
    
        private void chart1_GetToolTipText(object sender, ToolTipEventArgs e)
        {
            try
            {
                switch (e.HitTestResult.ChartElementType)
                {
                    case ChartElementType.DataPoint:
                        var dataPoint = e.HitTestResult.Series.Points[e.HitTestResult.PointIndex];
                        xtip = Xname[x - 1];
                        ytip = dataPoint.YValues[0];
                        foreach (Control c in Controls)
                        {
                            if (c is UCtips)
                            {
                                this.Controls.Remove(c);
                            }
                        }
    
                        UCtips uCtips = new UCtips();
                        uCtips.XX = xtip;
                        uCtips.YY = ytip;
                        this.Controls.Add(uCtips);
                        uCtips.BringToFront();
                        uCtips.Location = this.PointToClient(MousePosition);
                        break;
                }
            }
            catch { }
        }

    Result:

    Regards

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 24, 2019 9:48 AM
    Moderator