Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8045523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:34:30+00:00 2026-06-05T05:34:30+00:00

This is a different approach to what I was doing earlier. In my combo

  • 0

This is a different approach to what I was doing earlier.

In my combo owner drawn combo box draw 3 lines(solid,dash,dashdot) to be drawn with the color selected from an 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 what I’m trying to do. Draw 3lines(solid,dash,dashdot) in the combo box.

I don’t see any lines in the combo box except some blue which is the selected color

Thank u

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:34:32+00:00Added an answer on June 5, 2026 at 5:34 am

    Try this.

    I started a new winforms application. Created a class based off of ComboBox added your code and modified it a bit. I think you major problem was in your bitmap part. You create a new bitmap, then draw on that, but you never use the bitmap you created. If you wish to keep the code you created you would have to add to the end of the method item.Picture=myBitmap. But I think that would call the ondrawitem again and you would be in an infinite loop. Instead of creating a graphics object based off the item.Picture, just use the graphics object created for you in the DrawItemEventArgs.

    e.Graphics
    

    here is what I did, I think I cleaned it up a bit. And you may already know but you should always wrap pens, brushes and graphics in using {….} like I 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 you int startX... endy... stuff, unless you want to keep it for readability there is 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 am not sure why you would want to call the base.OnDrawItem, feel free to uncomment it if you 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;
    
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Third try at fixing this tonight - trying a different approach than before now.
This is similar to (but different from) this question . Here is some simple
this is a different question concerning: add a connection to database not working, asp.net
[Please note that this is a different question from the already answered How to
I have tried this a few different ways and it always seems to fail.
What are the different between this two diff statement? Any reference document? '{<table width=100%
If I invoke this method with different dates, the decimal places of the result
This one boggles my mind. I've used this same script (different targets of course)
I'm re-asking this question but with a different framework this time. I have two
Similar, but different to this question : I use highlight occurrences a lot, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.