如何使comboBox下拉列表显示图片 如上。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先在你的ComboBox的Item里要包含图片信息或能表示成图片.然后重写如下的两个方法:OnMeasureItemOnDrawItem或者添加ComboBox的这两个事件并处理:MeasureItemDrawItem private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) {//绘制列表项 ComboBox MyCombox=(ComboBox)sender; if(e.Index==-1) return; if(sender==null) return; SolidBrush MyBrush=new SolidBrush(Color.Red); Graphics g=e.Graphics; Rectangle MyRect=e.Bounds; MyRect.Offset(2,2); MyRect.Width=50; MyRect.Height-=4; MyRect.Offset(1,1); MyRect.Width-=2; MyRect.Height-=2; //绘制图片 g.DrawImage(Image.FromFile(@""),MyRect); //绘制选定颜色的名称 g.DrawString(MyBrush.Color.Name.ToString(),Font,new SolidBrush(e.ForeColor),e.Bounds.X+60,e.Bounds.Y+1); } private void comboBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) {//设置列表项宽 e.ItemHeight=this.comboBox1.ItemHeight-2; } 在drawitem方法里面还要加: //如果已经进行选择,则绘制正确的背景颜色和聚集框 e.DrawBackground(); e.DrawFocusRectangle(); private void cmbImage_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { ArrayList fontArray = new ArrayList() ; fontArray.Add(new Font("Ariel" , 8,FontStyle.Bold)); fontArray.Add(new Font("Courier",8,FontStyle.Italic)); fontArray.Add(new Font("Veranda",8,FontStyle.Bold)); Graphics g = e.Graphics ; Rectangle r = e.Bounds ; Size imageSize = ImageList1.ImageSize; Font fn = null ; if ( e.Index >= 0 ) { fn = (Font)fontArray[0]; string s = (string)comboBox1.Items[e.Index]; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { e.Graphics.FillRectangle(new SolidBrush(Color.Red) , r); comboBox1.Draw(e.Graphics, r.Left, r.Top,e.Index); e.Graphics.DrawString( s , fn , new SolidBrush(Color.Black), r.Left+imageSize.Width ,r.Top); e.DrawFocusRectangle(); } else { e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r); ImageList1.Draw(e.Graphics, r.Left, r.Top,e.Index); e.Graphics.DrawString( s , fn , new SolidBrush(Color.Black),r.Left+imageSize.Width ,r.Top); e.DrawFocusRectangle(); } } }能看看我上面的這段代碼不,老是出錯:指定的论据是超出有效值的范围。参数名:‘3’不是一个有效值为‘索引’。我測試了一下,e.Index的值為-1 把你的if ( e.Index >= 0 ) 改成下面if(e.Index==-1) return; if(sender==null) return; Assembly使用 RSA加密和解密 请问C#能不能像Perl一样在字符串中解析其中的字符串变量 ListBox中的SelectedItem怎么写 求教了? dataset取值问题 c# 画曲线 发送鼠标右键???? 再问一个多线程的问题-100分 用C#开发PB winform webBrowser控件 自定义右键菜单 属性问题 如何让多个类使用同一个Socket收发数据?
然后重写如下的两个方法:
OnMeasureItem
OnDrawItem
或者添加ComboBox的这两个事件并处理:
MeasureItem
DrawItem
{//绘制列表项 ComboBox MyCombox=(ComboBox)sender;
if(e.Index==-1)
return;
if(sender==null)
return;
SolidBrush MyBrush=new SolidBrush(Color.Red);
Graphics g=e.Graphics;
Rectangle MyRect=e.Bounds;
MyRect.Offset(2,2);
MyRect.Width=50;
MyRect.Height-=4;
MyRect.Offset(1,1);
MyRect.Width-=2;
MyRect.Height-=2;
//绘制图片
g.DrawImage(Image.FromFile(@""),MyRect);
//绘制选定颜色的名称
g.DrawString(MyBrush.Color.Name.ToString(),Font,new SolidBrush(e.ForeColor),e.Bounds.X+60,e.Bounds.Y+1);
}
{//设置列表项宽
e.ItemHeight=this.comboBox1.ItemHeight-2;
}
//如果已经进行选择,则绘制正确的背景颜色和聚集框
e.DrawBackground();
e.DrawFocusRectangle();
{
ArrayList fontArray = new ArrayList() ;
fontArray.Add(new Font("Ariel" , 8,FontStyle.Bold));
fontArray.Add(new Font("Courier",8,FontStyle.Italic));
fontArray.Add(new Font("Veranda",8,FontStyle.Bold)); Graphics g = e.Graphics ;
Rectangle r = e.Bounds ;
Size imageSize = ImageList1.ImageSize;
Font fn = null ;
if ( e.Index >= 0 )
{
fn = (Font)fontArray[0];
string s = (string)comboBox1.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red) , r);
comboBox1.Draw(e.Graphics, r.Left, r.Top,e.Index);
e.Graphics.DrawString( s , fn , new SolidBrush(Color.Black), r.Left+imageSize.Width ,r.Top); e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r);
ImageList1.Draw(e.Graphics, r.Left, r.Top,e.Index);
e.Graphics.DrawString( s , fn , new SolidBrush(Color.Black),r.Left+imageSize.Width ,r.Top);
e.DrawFocusRectangle();
}
}
}能看看我上面的這段代碼不,老是出錯:指定的论据是超出有效值的范围。参数名:‘3’不是一个有效值为‘索引’。我測試了一下,e.Index的值為-1
改成下面
if(e.Index==-1)
return;
if(sender==null)
return;