我有這樣的代碼,調整組合框的下拉的寬度:在C#中調整組合框下拉寬
private void comboBox_DropDown(object sender, EventArgs e)
{
ComboBox senderComboBox = (ComboBox)sender;
int width = senderComboBox.DropDownWidth;
Graphics g = senderComboBox.CreateGraphics();
Font font = senderComboBox.Font;
int vertScrollBarWidth =
(senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth;
foreach (string s in ((ComboBox)sender).Items)
{
newWidth = (int)g.MeasureString(s, font).Width
+ vertScrollBarWidth;
if (width < newWidth)
{
width = newWidth;
}
}
senderComboBox.DropDownWidth = width;
}
它的偉大工程,除了它擴展了下拉到右側的寬度,而我寧願它擴展到左邊,因爲組合框位於我的表單的右側。任何想法或建議,你可能會感激。謝謝。
「.anchor」屬性沒有處理這個問題嗎? – Pretzel 2010-05-20 20:51:50
@Pretzel:據我所知,當表單被調整大小時,Anchor隻影響控件的大小。 – 2010-05-20 20:55:56