passing font size to user control tool tip in Winform C# project

Question: I have a Winform C# project that changes the tool tip text of a user control tool tip when passed from a host project. I need to pass font size as a variable, but dont know where to make the changes. I have tried a plethora of solutions online and am stuck with this implementation. Any help in this direction will be really appreciated.

What I have so far:

I have a C# user Control project where i have set a property to set the tool tip text to change to user specified value in my UC_ToolTipButton project, where the contents of the project are as below:

UC_ToolTipButton.cs

using System.Windows.Forms;

namespace UC_ToolTipButton
{
    public partial class UC_ToolTipButton : UserControl
    {
        public string TT_Message
        {
            get{
                return ToolTip_Message.GetToolTip(btnTT);
            }
            set{
                ToolTip_Message.SetToolTip(btnTT, value);
            }
        }
        public UC_ToolTipButton()
        {
            InitializeComponent();
        }
    }
}

enter image description here

In my designer file, I have placed a button (btnTT) on which I have put a tool tip (ToolTip_Message).

When I compile this User Control Forms project, it works fine and creates a dll file.

Upon importing this file in a project TryButtonTooltip, where I have the file TryTooltipForm.cs with the following content

using System.Windows.Forms;

namespace TryButtonToolTip
{
    public partial class TryToolTipForm : Form
    {
        public TryToolTipForm()
        {
            InitializeComponent();
            uC_TTMessage.TT_Message = @"Hi";
        }
    }
}

enter image description here

Upvotes: 0

Views: 1454

Answers (1)

Arie
Arie

Reputation: 5373

Set OwnerDraw on ToolTip to true, in ToolTip's Draw event set the desired font, then in Popup event measure and set the size of your ToolTip, as is explained in the example here.

For example like this (untested):

public partial class UC_ToolTipButton : UserControl
{
    public string TT_FontFamily { get; set; }
    public float TT_FontSize { get; set; }

    public string TT_Message
    {
        get
        {
            return ToolTip_Message.GetToolTip(btnTT);
        }
        set
        {
            ToolTip_Message.SetToolTip(btnTT, value);
        }
    }
    public UC_ToolTipButton()
    {
        InitializeComponent();
        TT_FontFamily = "Tahoma";
        TT_FontSize = 10;
        ToolTip_Message.OwnerDraw = true;
        ToolTip_Message.Draw += new DrawToolTipEventHandler(TT_Draw);
        ToolTip_Message.Popup += new PopupEventHandler(TT_Popup);
    }

    private void TT_Popup(object sender, PopupEventArgs e)
    {
        using (Font f = new Font(TT_FontFamily, TT_FontSize))
        {
            e.ToolTipSize = TextRenderer.MeasureText(ToolTip_Message.GetToolTip(e.AssociatedControl), f);
        }
    }
    private void TT_Draw(System.Object sender,
        System.Windows.Forms.DrawToolTipEventArgs e)
    {
            e.DrawBackground();
            e.DrawBorder();
            using (StringFormat sf = new StringFormat())
            {
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;

                using (Font f = new Font(TT_FontFamily, TT_FontSize))
                {
                    e.Graphics.DrawString(e.ToolTipText, f, SystemBrushes.ActiveCaptionText, e.Bounds, sf);
                }
            }

    }
}

Upvotes: 3

Related Questions