Working with SfToolTip in Windows Forms Tooltip (SfToolTip)

29 Apr 20214 minutes to read

Getting the ToolTip Text of a Control

The tooltip text of a control can be retrieved by using the GetToolTip method.

string toolTipText = this.sfToolTip1.GetToolTip(this.button1);

Setting the ToolTip Text of a Control

The tooltip text for a control can be initialized by using the SetToolTip method.

this.sfToolTip1.SetToolTip(this.button1, "Button1 ToolTip Text");

Getting the ToolTipInfo of a Control

The ToolTipInfo of a control can be retrieved by using the GetToolTipInfo method.

ToolTipInfo toolTipInfo = this.sfToolTip1.GetToolTipInfo(this.button1);

Setting the ToolTipInfo of a Control

The ToolTipInfo for a control can be initialized by using the SetToolTipInfo method.

this.sfToolTip1.SetToolTipInfo(this.button1, toolTipInfo);

Disabling Tooltip from Showing

The ToolTipShowing event can be used to disable the tooltip window by setting the e.Cancel to true.

this.sfToolTip1.ToolTipShowing += SfToolTip1_ToolTipShowing;
private void SfToolTip1_ToolTipShowing(object sender, ToolTipShowingEventArgs e)
{
    if (e.Control.Name == "cancelButton")
    e.Cancel = true;
}

Changing Location of the ToolTip

The location of the tooltip to be shown can be customized in the ToolTipShowing event using the e.Location property.

this.sfToolTip1.ToolTipShowing += SfToolTip1_ToolTipShowing;
private void SfToolTip1_ToolTipShowing(object sender, ToolTipShowingEventsArgs e)
{
    e.Location = new Point(e.Location.X + 20, e.Location.Y - 25);
}

Location of the tooltip to be shown can be customized in winforms tooltip

Setting Minimum and Maximum Widths

The minimum width and the maximum width of the tooltip can be changed using the MinWidth and MaxWidth properties.

ToolTipInfo toolTipInfo1 = new ToolTipInfo();
toolTipInfo1.MinWidth = 100;
toolTipInfo1.MaxWidth = 500;

NOTE

The width of the tooltip will be initialized to MinWidth, if the width is lesser than MinWidth. The width of the tooltip will be initialized to MaxWidth, if the width exceeds the MaxWidth.

Custom Drawing of ToolTip

The DrawToolTipItem event can be used to draw the ToolTipItem appearance. To cancel the default drawing of the ToolTipItem, e.Cancel property can be used.

this.sfToolTip1.DrawToolTipItem += SfToolTip1_DrawToolTipItem;
private void SfToolTip1_DrawToolTipItem(object sender, DrawToolTipItemEventArgs e)
{

// To cancel the drawing of ToolTipItem.
    e.Cancel = true;
    LinearGradientBrush gradientBrush = new LinearGradientBrush(e.ToolTipItemRectangle, Color.LightSkyBlue, Color.LightGreen, LinearGradientMode.Horizontal);
    e.Graphics.FillRectangle(gradientBrush, e.ToolTipItemRectangle);
    Pen pen = new Pen(Color.Black);
    Rectangle borderRectangle = e.ToolTipItemRectangle;
    borderRectangle.Width -= 1;
    borderRectangle.Height -= 1;
    e.Graphics.DrawRectangle(pen, borderRectangle);
    SolidBrush solidBrush = new SolidBrush(Color.Black);
    StringFormat stringFormat = new StringFormat();
    stringFormat.Alignment = StringAlignment.Center;
    stringFormat.LineAlignment = StringAlignment.Center;
    e.Graphics.DrawString(e.ToolTipItem.Text, e.ToolTipItem.Style.Font, solidBrush, e.ToolTipItemRectangle, stringFormat);
}

Custom drawing tooltip shown in winforms tooltip