[英]change height of combobox in winform application
我正在開發一種觸摸屏設備的應用程序。 為了方便用戶,我需要改變組合框的大小。
我已經檢查了很多東西,包括DrawItemEventHandler
和MeasureItemEventHandler
,但它沒有按我的意願工作。
基本上我想在不觸及字體大小的情況下改變組合框的高度。 當我更改組合框的字體大小時,它看起來像圖像的左側。 如何設置看起來像圖像右側的組合框?
順便說一句,不知道它是否有效解決方案,我不是使用數組字符串。 我綁定的數據就像。
combobox.DisplayMember = "Name";
combobox.ValueMember = "ID";
combobox.DataSource = new BindingSource { DataSource = datalist };
提前致謝。
有了TaW解決方案,我設法按照自己的意願設置項目。 當組合框項目沒有下降時,我唯一無法在中間設置文本。 如何將此文本位置設置為中心?
您可以設置ItemHeight
屬性,然后在DrawItem
事件中自己繪制項目。
不是很難,搜索'ownerdraw'和'combobox'。 代碼項目有一個例子
這是一個最小版本,從上面的鏈接中提取:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
Font f = comboBox1.Font;
int yOffset = 10;
if ((e.State & DrawItemState.Focus) == 0)
{
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), f, Brushes.Black,
new Point(e.Bounds.X, e.Bounds.Y + yOffset));
}
else
{
e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), f, Brushes.White,
new Point(e.Bounds.X, e.Bounds.Y + yOffset));
}
}
您還必須將DropDownStyle
設置為DropDownList
以使突出顯示起作用,並且您需要將DrawMode
設置為OwnerDrawFixed
。 (或者對於OwnerDrawVariable
,如果你想為某些itmes設置不同的高度..)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.