工具提示从控件获取属性

时间:2019-05-15 08:14:31

标签: c# tooltip

显示工具提示,并从投射控件中获取属性值。

我有一个简单的ToolTip控件,但是我需要在ToolTip中显示控件Properties中的值。

例如: TextBox显示工具提示,其中的文本包含在另一个控件中。

就我而言,我有一个具有以下属性的自定义控件:

MyCustomPanel

(double)                 WinRate  = 53,4
(dobule)                 LoseRate = 13,2
(CustomToolTip Text)     "Team A"

现在,如何恢复工具提示覆盖图上的属性?

// Panel

public class PanelContainer : Control
    {
        public enum type
        {
            Top,
            Middle,
            Out,
            All
        }

        type tp = type.All;
        bool min = false;
        private double wrate = 0, brate = 0;

  [Category("Option")]
        public double WinRate
        {
            get { return wrate; }
            set { wrate = value; }
        }


        [Category("Option")]
        public double BanRate
        {
            get { return brate; }
            set { brate = value; }
        }


        [Category("Option")]
        public type LandType
        {
            get { return tp; }
            set { tp = value; Invalidate(false); }
        }

//more...
}


//Simple ToolTip

   public class MyToolTip: ToolTip
    {
        Color border = Color.FromArgb(123, 93, 41);
        Color symb = Color.FromArgb(200, 170, 109);

             public MyToolTip()
        {
            this.OwnerDraw = true;
            this.Popup += new PopupEventHandler(this.OnPopup);
            this.Draw += new DrawToolTipEventHandler(this.OnDraw);
            this.UseFading = true;
        }

        private void OnPopup(object sender, PopupEventArgs e) // use this event to set the size of the tool tip
        {
            e.ToolTipSize = new Size(200, 100);
        }

        private void OnDraw(object sender, DrawToolTipEventArgs e) // use this event to customise the tool tip
        {

            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;    
            g.FillRectangle(new SolidBrush(Color.FromArgb(2,12,21)), e.Bounds);
            g.DrawRectangle(new Pen(new SolidBrush(symb)), 0, 0, e.Bounds.Width - 1, e.Bounds.Height - 1);
            SizeF sz = g.MeasureString(e.ToolTipText, e.Font);
            g.DrawString(e.ToolTipText, e.Font, new SolidBrush(Color.White), new PointF((e.Bounds.Width - sz.Width) / 2, 10));

//My Try whit Error
            g.DrawString(((Control)sender).Parent.GetType().ToString(), e.Font, new SolidBrush(Color.White), 50, 50);

        }

    }

需要一个绘制示例的好方法:

g.DrawString(((MyPanel)sender).wrate.ToString(),e.Font,new SolidBrush(Color.White),50,50);

无法将类型为'PLib.MyToolTip'的对象强制转换为类型为'System.Windows.Forms.Control'。

0 个答案:

没有答案