2013-02-26 3 views
0

다음과 같은 사용자 지정 콤보 상자를 개발하여 항목 높이를 늘 렸습니다. 끝나면 스크롤바가있을 때 드롭 다운 메뉴의 끝에 빈 공간이 나타납니다. enter image description here 어떻게 문제를 해결할 수 있습니까?OwnerDrawVariable Combobox DropDown 빈 공간

class MyComboBoxXX : ComboBox 
{ 
    public MyComboBoxXX():base() 
    { 
     this.DrawMode = DrawMode.OwnerDrawVariable; 
     this.DropDownStyle = ComboBoxStyle.DropDownList; 
     this.MaxDropDownItems = 5; 
     this.IntegralHeight = false;   
    } 

    protected override void OnMeasureItem(MeasureItemEventArgs e) 
    { 
     e.ItemHeight = 40; 
     this.DropDownHeight = 40 * 5;   
    } 
    protected override void OnDrawItem(DrawItemEventArgs e) 
    {   
     e.DrawBackground();   
     var index = e.Index;  
     if (index < 0 || index >= Items.Count) return;  
     using (var brush = new SolidBrush(e.ForeColor)) 
     {    
      Rectangle rec = new Rectangle(e.Bounds.Left, e.Bounds.Top + ((e.Bounds.Height - ItemHeight)/2), e.Bounds.Width, ItemHeight); 
      e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(this.ForeColor), rec); 
     } 
     e.DrawFocusRectangle(); 
    }  
} 

답변

1

자세히 보면 DropDown 영역의 위쪽과 아래쪽에 1 픽셀 테두리가있는 것으로 보입니다. DropDownHeight에 2 픽셀을 추가하여 공간을 제거 할 수 있습니다.

protected override void OnMeasureItem(MeasureItemEventArgs e) 
{ 
    e.ItemHeight = 40; 
    this.DropDownHeight = (40 * 5) + 2; //add 2 pixels to include the border 
} 



결과 :
Combobox without the space at the end

0

나는 그것이 당신의 OnMeasureItem 메서드 재정의 최대 수보다 작의 경우 항목의 해당 번호로 DropDownHeight 값을 감소한다고 생각합니다.

+0

DropDownList에 세로 스크롤 막대가있는 경우에만 문제가 발생합니다. – Maanu