如果能够让下拉列表的宽度随着内容的变化而变化,这个问题不就解决了。下面我们看看如何让ComboBox的下拉列表宽度自适应内容的宽度:
1private void AdjustComboBoxDropDownListWidth(object comboBox)
2{
3 Graphics g = null;
4 Font font = null;
5 try
6 {
7 ComboBox senderComboBox = null;
8 if (comboBox is ComboBox)
9 senderComboBox = (ComboBox)comboBox;
10 else if (comboBox is ToolStripComboBox)
11 senderComboBox = ((ToolStripComboBox)comboBox).ComboBox;
12 else
13 return;
14
15 int width = senderComboBox.Width;
16 g = senderComboBox.CreateGraphics();
17 font = senderComboBox.Font;
18
19 //checks if a scrollbar will be displayed.
20 //If yes, then get its width to adjust the size of the drop down list.
21 int vertScrollBarWidth =
22 (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
23 ? SystemInformation.VerticalScrollBarWidth : 0;
24
25 int newWidth;
26 foreach (object s in senderComboBox.Items) //Loop through list items and check size of each items.
27 {
28 if (s != null)
29 {
30 newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
31 + vertScrollBarWidth;
32 if (width < newWidth)
33 width = newWidth; //set the width of the drop down list to the width of the largest item.
34 }
35 }
36 senderComboBox.DropDownWidth = width;
37 }
38 catch
39 { }
40 finally
41 {
42 if (g != null)
43 g.Dispose();
44 }
45}
2{
3 Graphics g = null;
4 Font font = null;
5 try
6 {
7 ComboBox senderComboBox = null;
8 if (comboBox is ComboBox)
9 senderComboBox = (ComboBox)comboBox;
10 else if (comboBox is ToolStripComboBox)
11 senderComboBox = ((ToolStripComboBox)comboBox).ComboBox;
12 else
13 return;
14
15 int width = senderComboBox.Width;
16 g = senderComboBox.CreateGraphics();
17 font = senderComboBox.Font;
18
19 //checks if a scrollbar will be displayed.
20 //If yes, then get its width to adjust the size of the drop down list.
21 int vertScrollBarWidth =
22 (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
23 ? SystemInformation.VerticalScrollBarWidth : 0;
24
25 int newWidth;
26 foreach (object s in senderComboBox.Items) //Loop through list items and check size of each items.
27 {
28 if (s != null)
29 {
30 newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
31 + vertScrollBarWidth;
32 if (width < newWidth)
33 width = newWidth; //set the width of the drop down list to the width of the largest item.
34 }
35 }
36 senderComboBox.DropDownWidth = width;
37 }
38 catch
39 { }
40 finally
41 {
42 if (g != null)
43 g.Dispose();
44 }
45}
如果每次在我们向ComboBox中添加一项后,就要调用一下这个方法,那就太麻烦了。能不能把这种自适应宽度的功能集成到 ComboBox中呢?这里我们继承ComboBox,实现一个自定义的控件,在用户每次打开下拉列表的时候,让控件自动调整下拉列表的宽度。
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8
9namespace WindowsApplication2
10{
11 class MyComboBox : ComboBox
12 {
13 protected override void OnDropDown(EventArgs e)
14 {
15 base.OnDropDown(e);
16 AdjustComboBoxDropDownListWidth(); //调整comboBox的下拉列表的大小
17 }
18
19 private void AdjustComboBoxDropDownListWidth()
20 {
21 Graphics g = null;
22 Font font = null;
23 try
24 {
25 int width = this.Width;
26 g = this.CreateGraphics();
27 font = this.Font;
28
29 //checks if a scrollbar will be displayed.
30 //If yes, then get its width to adjust the size of the drop down list.
31 int vertScrollBarWidth =
32 (this.Items.Count > this.MaxDropDownItems)
33 ? SystemInformation.VerticalScrollBarWidth : 0;
34
35 int newWidth;
36 foreach (object s in this.Items) //Loop through list items and check size of each items.
37 {
38 if (s != null)
39 {
40 newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
41 + vertScrollBarWidth;
42 if (width < newWidth)
43 width = newWidth; //set the width of the drop down list to the width of the largest item.
44 }
45 }
46 this.DropDownWidth = width;
47 }
48 catch
49 { }
50 finally
51 {
52 if (g != null)
53 g.Dispose();
54 }
55 }
56 }
57}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8
9namespace WindowsApplication2
10{
11 class MyComboBox : ComboBox
12 {
13 protected override void OnDropDown(EventArgs e)
14 {
15 base.OnDropDown(e);
16 AdjustComboBoxDropDownListWidth(); //调整comboBox的下拉列表的大小
17 }
18
19 private void AdjustComboBoxDropDownListWidth()
20 {
21 Graphics g = null;
22 Font font = null;
23 try
24 {
25 int width = this.Width;
26 g = this.CreateGraphics();
27 font = this.Font;
28
29 //checks if a scrollbar will be displayed.
30 //If yes, then get its width to adjust the size of the drop down list.
31 int vertScrollBarWidth =
32 (this.Items.Count > this.MaxDropDownItems)
33 ? SystemInformation.VerticalScrollBarWidth : 0;
34
35 int newWidth;
36 foreach (object s in this.Items) //Loop through list items and check size of each items.
37 {
38 if (s != null)
39 {
40 newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
41 + vertScrollBarWidth;
42 if (width < newWidth)
43 width = newWidth; //set the width of the drop down list to the width of the largest item.
44 }
45 }
46 this.DropDownWidth = width;
47 }
48 catch
49 { }
50 finally
51 {
52 if (g != null)
53 g.Dispose();
54 }
55 }
56 }
57}