我做了一個ListBox,裏面放了幾個string,可是這些string很長,以至於ListBox無法完全顯示出來,雖然有水平scrollbar,但滾動畢竟太煩了。 我想讓鼠標移動到string上時,能夠顯示一個tip來顯示完全的string,令人失望的是,ListBox沒有這個功能,那如何才能用程序做到呢? --------------------------------------------------------------- 感謝您使用微軟產品。 你可以通過結合使用ListBox控件和Tooltip控件來實現需要的效果。如下提供一段示例代碼,供你參考:
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;
}