在Windows窗體(C#)中是否有動態設置工具提示高度和寬度的方法(在代碼中指示)。我正在使用DataViewGrid控件,所以我不得不使用Show方法。不過,我已經注意到(左到它自己的設備時),該工具提示控件並不總是調整到所提供的內容....在Windows窗體(C#)中是否有動態設置工具提示高度和寬度的方法
一個例子:
有一個工具提示控制添加到窗體(稱爲ttText),然後有它首先顯示的文字:
ttText.Show("I'm hungry\nand waiting!");
將截斷下一個呼叫:
ttText.Show("Well, too bad -- so much for your stamina, you should not be here!\nSo the little bear responds!");
對這個有什麼想法?
請記住,一個DataGridView需要一個機制,因此顯示工具提示幫助使用的顯示方法和我在非DataViewGrids其他地方看到這種現象......
下面是代碼的例子: using System.Collections.Generic; using System.Drawing;使用System.Linq的 ;使用System.Windows.Forms的 ;
namespace TestForm
{
class Form1 : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.ttText = new System.Windows.Forms.ToolTip(this.components);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(13, 19);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(453, 321);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.CellMouseEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_MouseCellEnter);
//
// ttText
//
this.ttText.AutomaticDelay = 60;
this.ttText.AutoPopDelay = 600000;
this.ttText.InitialDelay = 60;
this.ttText.IsBalloon = true;
this.ttText.ReshowDelay = 60;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(478, 352);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.ToolTip ttText;
public Form1()
{
InitializeComponent();
var ds = Sayings().ToList();
dataGridView1.DataSource = ds;
}
public List<dynamic> Sayings()
{
return new List<dynamic>
{
new
{
Human = "I'm hungry\nand waiting!",
BabyBear = "Well, too bad -- so much for your stamina, you should not be here!\nSo the little bear responds!"
}
};
}
private void dataGridView1_MouseCellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != -1 && e.RowIndex != -1)
{
var rect = dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true);
var left = rect.Left + (int)(rect.Width * .5f);
var top = rect.Top;
Point displayPoint = new Point(left + this.ClientRectangle.Left, top + this.ClientRectangle.Top + 40);
ttText.Show(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(), this, displayPoint);
}
}
}
}
如果您將鼠標懸停在第一列上,則第二列中的工具提示文本將被截斷。
只有一個字符串參數的Show()方法在哪裏?你能鏈接到MSDN中的方法嗎? – 2012-02-23 03:51:46
http://msdn.microsoft.com/en-us/library/ms752368.aspx,可能會有幫助嗎? – 2012-02-23 04:08:16
而在任何人譴責我沒有添加代碼示例之前,在這裏你去: – Silverten 2012-02-23 04:13:14