winforms - C# combobox with lines -


this different approach doing earlier.

in combo owner drawn combo box draw 3 lines(solid,dash,dashdot) drawn color selected earlier colpr picker drop down

this.drawmode = drawmode.ownerdrawvariable;             this.dropdownstyle = comboboxstyle.dropdownlist;      protected override void ondrawitem(drawitemeventargs e)         {             e.drawbackground();               int startx = e.bounds.left + 5;             int starty = (e.bounds.y);             point p1=new point(startx,starty);             int endx = e.bounds.right - 5;             int endy = (e.bounds.y);              comboboxitem item = (comboboxitem)this.items[e.index];             point p2=new point(endx,endy);             base.ondrawitem(e);             pen solidmypen = new pen(item.forecolor, 1);             pen dashedpen = new pen(item.forecolor, 1);             dashedpen.dashstyle = system.drawing.drawing2d.dashstyle.dash;             pen dashdot = new pen(item.forecolor, 1);             dashdot.dashstyle = system.drawing.drawing2d.dashstyle.dashdot;            // pen dashedpen = new pen(item.forecolor, (int32)this.items[e.index]);              bitmap mybitmap = new bitmap(item.picture);             graphics graphicsobj;             graphicsobj = graphics.fromimage(mybitmap);             switch (e.index)             {                 case 0:                     graphicsobj.drawline(solidmypen, p1, p2);                     break;                 case 1:                     graphicsobj.drawline(dashedpen, p1, p2);                     break;                 case 2:                     graphicsobj.drawline(dashdot, p1, p2);                     break;               } 

here's i'm trying do. draw 3lines(solid,dash,dashdot) in combo box.

i don't see lines in combo box except blue selected color

thank u

try this.

i started new winforms application. created class based off of combobox added code , modified bit. think major problem in bitmap part. create new bitmap, draw on that, never use bitmap created. if wish keep code created have add end of method item.picture=mybitmap. think call ondrawitem again , in infinite loop. instead of creating graphics object based off item.picture, use graphics object created in drawitemeventargs.

e.graphics 

here did, think cleaned bit. , may know should wrap pens, brushes , graphics in using {....} demonstrated below.

public partial class form1 : form {     public form1()     {         initializecomponent();     } }  public class mycombobox : combobox {     public mycombobox()     {     this.drawmode = drawmode.ownerdrawvariable;         this.dropdownstyle = comboboxstyle.dropdownlist;     }     protected override void ondrawitem(drawitemeventargs e)     {         e.drawbackground();         //i removed int startx... endy... stuff, unless want keep readability there no need         point p1 = new point(e.bounds.left + 5, e.bounds.y + 5);         point p2 = new point(e.bounds.right - 5, e.bounds.y + 5);          //i not sure why want call base.ondrawitem, feel free uncomment if wish though         //base.ondrawitem(e);          switch (e.index)         {             case 0:                 using ( pen solidmypen = new pen(e.forecolor, 1))                 e.graphics.drawline(solidmypen, p1, p2);                 break;             case 1:                 using (pen dashedpen = new pen(e.forecolor, 1))                 {                     dashedpen.dashstyle = system.drawing.drawing2d.dashstyle.dash;                     e.graphics.drawline(dashedpen, p1, p2);                 }                 break;             case 2:                 using (pen dashdot = new pen(e.forecolor, 1))                 {                     dashdot.dashstyle = system.drawing.drawing2d.dashstyle.dashdot;                     e.graphics.drawline(dashdot, p1, p2);                 }                 break;           }     } } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -