重绘控件吧。 我有vb的代码如下#Region "重绘ListBox控件" Private Sub lstDockItem_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lstDockItem.DrawItem If e.Index = -1 Then Exit Sub Dim txtBrush As SolidBrush = New SolidBrush(Color.Blue) Dim bgBrush As SolidBrush = New SolidBrush(Color.LightCyan) Dim txtFnt As Font = New Font(Me.Font.Name, Me.Font.Size) Try If e.Index Mod 2 = 0 Then txtBrush = New SolidBrush(Color.Red) If e.State And System.Windows.Forms.DrawItemState.Selected Then txtFnt = New Font(Me.Font.Name, Me.Font.Size, FontStyle.Italic Or FontStyle.Bold) bgBrush = New SolidBrush(Color.LightSteelBlue) End If e.Graphics.FillRectangle(bgBrush, e.Bounds) Dim R As New RectangleF(e.Bounds.X + e.Bounds.Height, e.Bounds.Y, e.Bounds.Width - e.Bounds.Height, e.Bounds.Height) Dim SF As New StringFormat : SF.LineAlignment = StringAlignment.Center e.Graphics.DrawImage(Me.DockManager.Item(e.Index).Icon, e.Bounds.Left + 2, e.Bounds.Top + 2, e.Bounds.Height - 4, e.Bounds.Height - 4) e.Graphics.DrawString(lstDockItem.Items(e.Index).ToString, txtFnt, txtBrush, R, SF) e.DrawFocusRectangle() Catch ex As Exception End Try txtBrush.Dispose() : bgBrush.Dispose() : txtFnt.Dispose() '释放资源 End Sub Private Sub lstDockItem_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles lstDockItem.MeasureItem Try Dim S As New Size(Me.Width, 400) Dim itmSize As SizeF = e.Graphics.MeasureString(lstDockItem.Items(e.Index).ToString, Me.Font, S) e.ItemHeight = itmSize.Height * 2 : e.ItemWidth = itmSize.Width Catch ex As Exception End Try End Sub #End Region
string strTip = ""; //Get the item
int nIdx = listBox1.IndexFromPoint(e.Location);
if ((nIdx >= 0) && (nIdx < listBox1.Items.Count))
strTip = listBox1.Items[nIdx].ToString(); toolTip1.SetToolTip(listBox1, strTip);这里有实例
http://download.csdn.net/source/1614
private void Form1_Load(object sender, System.EventArgs e)
{
string connString = " server=SHA-RICKIE-01;database=pubs;uid=user;pwd=user";
string sqlString = " Select title from titles";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter myDataAdapter = new SqlDataAdapter(sqlString,conn);
myDataAdapter.Fill(ds,"titles");
listBox1.DataSource = ds.Tables["titles"].DefaultView ;
// Set Field Name you want to get data from
listBox1.DisplayMember = "title";
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
toolTip1.SetToolTip(listBox1,listBox1.Text);
toolTip1.Active = true;
}
我有vb的代码如下#Region "重绘ListBox控件"
Private Sub lstDockItem_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lstDockItem.DrawItem
If e.Index = -1 Then Exit Sub Dim txtBrush As SolidBrush = New SolidBrush(Color.Blue)
Dim bgBrush As SolidBrush = New SolidBrush(Color.LightCyan)
Dim txtFnt As Font = New Font(Me.Font.Name, Me.Font.Size) Try
If e.Index Mod 2 = 0 Then txtBrush = New SolidBrush(Color.Red)
If e.State And System.Windows.Forms.DrawItemState.Selected Then
txtFnt = New Font(Me.Font.Name, Me.Font.Size, FontStyle.Italic Or FontStyle.Bold)
bgBrush = New SolidBrush(Color.LightSteelBlue)
End If e.Graphics.FillRectangle(bgBrush, e.Bounds) Dim R As New RectangleF(e.Bounds.X + e.Bounds.Height, e.Bounds.Y, e.Bounds.Width - e.Bounds.Height, e.Bounds.Height)
Dim SF As New StringFormat : SF.LineAlignment = StringAlignment.Center
e.Graphics.DrawImage(Me.DockManager.Item(e.Index).Icon, e.Bounds.Left + 2, e.Bounds.Top + 2, e.Bounds.Height - 4, e.Bounds.Height - 4)
e.Graphics.DrawString(lstDockItem.Items(e.Index).ToString, txtFnt, txtBrush, R, SF)
e.DrawFocusRectangle()
Catch ex As Exception
End Try
txtBrush.Dispose() : bgBrush.Dispose() : txtFnt.Dispose() '释放资源
End Sub Private Sub lstDockItem_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles lstDockItem.MeasureItem
Try
Dim S As New Size(Me.Width, 400)
Dim itmSize As SizeF = e.Graphics.MeasureString(lstDockItem.Items(e.Index).ToString, Me.Font, S)
e.ItemHeight = itmSize.Height * 2 : e.ItemWidth = itmSize.Width
Catch ex As Exception
End Try
End Sub
#End Region
它存在的格式只能是一行一行显示,可以设置下拉和右拉
但不能设置自动换行,因为没有这个属性如果想换行,可以判断字符串长度后重新add一下