如何延长DataGridView列标题工具提示的显示时长?
The built-in tooltip for DataGridView column headers uses a system default duration (around 5 seconds) and doesn’t let you adjust it directly. Since you’re already setting unique tooltips for each of your 20 columns, here’s a straightforward way to override the duration using a custom ToolTip control while keeping your existing setup intact:
Step-by-Step Implementation
1. Add a ToolTip Control to Your Form
You can do this either via the Visual Studio Designer (drag a ToolTip from the Toolbox onto your form) or programmatically in your form’s constructor/load event.
2. Configure the ToolTip’s Duration
Set the AutoPopDelay property to 10000 milliseconds (10 seconds) to control how long the tooltip stays visible when the mouse is stationary. You can optionally tweak InitialDelay (time before the tooltip first appears) and ReshowDelay (delay when switching between tooltips) to match your preferences.
3. Handle DataGridView Header Mouse Events
We’ll use the ColumnHeaderMouseEnter event to show the custom tooltip (reusing your existing ToolTipText values) and ColumnHeaderMouseLeave to hide it when the mouse moves away.
Code Examples
C#
private ToolTip _columnHeaderToolTip; public YourForm() { InitializeComponent(); // Initialize and configure the custom tooltip _columnHeaderToolTip = new ToolTip(); _columnHeaderToolTip.AutoPopDelay = 10000; // 10 seconds _columnHeaderToolTip.InitialDelay = 500; // Optional: 0.5s delay before tooltip appears _columnHeaderToolTip.ReshowDelay = 100; // Optional: 0.1s delay when switching tooltips // Hook up DataGridView events dgv.ColumnHeaderMouseEnter += Dgv_ColumnHeaderMouseEnter; dgv.ColumnHeaderMouseLeave += Dgv_ColumnHeaderMouseLeave; } private void Dgv_ColumnHeaderMouseEnter(object sender, DataGridViewCellEventArgs e) { var dgv = sender as DataGridView; if (dgv == null || e.ColumnIndex < 0) return; var column = dgv.Columns[e.ColumnIndex]; if (string.IsNullOrEmpty(column.ToolTipText)) return; // Position tooltip near the column header var headerRect = dgv.GetColumnDisplayRectangle(e.ColumnIndex, true); _columnHeaderToolTip.Show(column.ToolTipText, dgv, headerRect.X + headerRect.Width / 2, headerRect.Y + headerRect.Height); } private void Dgv_ColumnHeaderMouseLeave(object sender, DataGridViewCellEventArgs e) { _columnHeaderToolTip.Hide(dgv); }
VB.NET
Private _columnHeaderToolTip As ToolTip Public Sub New() InitializeComponent() ' Initialize and configure the custom tooltip _columnHeaderToolTip = New ToolTip() _columnHeaderToolTip.AutoPopDelay = 10000 ' 10 seconds _columnHeaderToolTip.InitialDelay = 500 ' Optional: 0.5s delay before tooltip appears _columnHeaderToolTip.ReshowDelay = 100 ' Optional: 0.1s delay when switching tooltips ' Hook up DataGridView events AddHandler dgv.ColumnHeaderMouseEnter, AddressOf Dgv_ColumnHeaderMouseEnter AddHandler dgv.ColumnHeaderMouseLeave, AddressOf Dgv_ColumnHeaderMouseLeave End Sub Private Sub Dgv_ColumnHeaderMouseEnter(sender As Object, e As DataGridViewCellEventArgs) Dim dgv = TryCast(sender, DataGridView) If dgv Is Nothing OrElse e.ColumnIndex < 0 Then Return Dim column = dgv.Columns(e.ColumnIndex) If String.IsNullOrEmpty(column.ToolTipText) Then Return ' Position tooltip near the column header Dim headerRect = dgv.GetColumnDisplayRectangle(e.ColumnIndex, True) _columnHeaderToolTip.Show(column.ToolTipText, dgv, headerRect.Location.X + headerRect.Width / 2, headerRect.Location.Y + headerRect.Height) End Sub Private Sub Dgv_ColumnHeaderMouseLeave(sender As Object, e As DataGridViewCellEventArgs) _columnHeaderToolTip.Hide(dgv) End Sub
Key Notes
- This approach keeps your existing code intact—you don’t need to change how you set
dgv.Columns(1).ToolTipText = "Some Text"for each column. - The tooltip automatically uses the unique text you’ve defined for each header.
GetColumnDisplayRectangleensures the tooltip stays positioned correctly even if columns are resized or reordered.
内容的提问来源于stack exchange,提问作者theMoreYouNgo

