Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. ComboBox Custom OnPaint

ComboBox Custom OnPaint

Scheduled Pinned Locked Moved .NET (Core and Framework)
graphicshelpquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ian
    wrote on last edited by
    #1

    I have a control that is derived from the standard ComboBox. I have an override for the OnPaint method that paints a border, drop down arrow button, background, and foreground. The only problem is that something (some other event/subcontrol) is drawing a black text box with a large font over top of the text box region of the combo box control. I am drawing a background in my OnPaint event, and it is being draw on top of. I can see that the background is drawn because there is a border around the text box region. My code looks like the following: protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawBorder(); DrawBackground(); DrawArrowButton(); DrawForegroundText(); } What am I missing here??? By the way, this control works just fine if the DropDownStyle is set to DropDownList, however I'm need it to work as a DropDown (being able to edit the text box). --Ian;

    D 1 Reply Last reply
    0
    • I Ian

      I have a control that is derived from the standard ComboBox. I have an override for the OnPaint method that paints a border, drop down arrow button, background, and foreground. The only problem is that something (some other event/subcontrol) is drawing a black text box with a large font over top of the text box region of the combo box control. I am drawing a background in my OnPaint event, and it is being draw on top of. I can see that the background is drawn because there is a border around the text box region. My code looks like the following: protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawBorder(); DrawBackground(); DrawArrowButton(); DrawForegroundText(); } What am I missing here??? By the way, this control works just fine if the DropDownStyle is set to DropDownList, however I'm need it to work as a DropDown (being able to edit the text box). --Ian;

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      In calling all of your painting functions, are you passing the Graphics object sent inside the PaintEventArgs or are you creating another one?? You should be passing the Graphics given to you by the PaintEventArgs. Dave Kreskowiak Microsoft MVP - Visual Basic

      I 1 Reply Last reply
      0
      • D Dave Kreskowiak

        In calling all of your painting functions, are you passing the Graphics object sent inside the PaintEventArgs or are you creating another one?? You should be passing the Graphics given to you by the PaintEventArgs. Dave Kreskowiak Microsoft MVP - Visual Basic

        I Offline
        I Offline
        Ian
        wrote on last edited by
        #3

        Sorry for my simple example. Yes, I am passing the PaintEventArgs that is passed to the event. So the GC is being used to do all the painting. --Ian;

        D 1 Reply Last reply
        0
        • I Ian

          Sorry for my simple example. Yes, I am passing the PaintEventArgs that is passed to the event. So the GC is being used to do all the painting. --Ian;

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          That's why we always suggest Copy-'n-Paste the code instead of retyping it. You said that this text is being painted OVER what your code painted? Dave Kreskowiak Microsoft MVP - Visual Basic

          I 1 Reply Last reply
          0
          • D Dave Kreskowiak

            That's why we always suggest Copy-'n-Paste the code instead of retyping it. You said that this text is being painted OVER what your code painted? Dave Kreskowiak Microsoft MVP - Visual Basic

            I Offline
            I Offline
            Ian
            wrote on last edited by
            #5

            I'm painting the background a green with a FillRectangle, and when it is displayed to the screen, I see a 2 or 3 pixel border of green surrounding a black text box region (that was not drawn by me). This is what I mean when I say that it was drawn over top of what I had drawn. The sample class below does not contain all the code, however it will give you an idea of the problem. Just drop this class into your a project, compile, and drop it the ComboDraw control onto a form. using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; namespace ComboTest { class ComboDraw : ComboBox { public ComboDraw() { DrawMode = DrawMode.OwnerDrawFixed; DropDownStyle = ComboBoxStyle.DropDown; FlatStyle = FlatStyle.Flat; SetStyle(ControlStyles.UserPaint, true); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.FillRectangle(new SolidBrush(Color.Green), ClientRectangle); } } } --Ian; -- modified at 9:19 Thursday 13th July, 2006

            D 1 Reply Last reply
            0
            • I Ian

              I'm painting the background a green with a FillRectangle, and when it is displayed to the screen, I see a 2 or 3 pixel border of green surrounding a black text box region (that was not drawn by me). This is what I mean when I say that it was drawn over top of what I had drawn. The sample class below does not contain all the code, however it will give you an idea of the problem. Just drop this class into your a project, compile, and drop it the ComboDraw control onto a form. using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; namespace ComboTest { class ComboDraw : ComboBox { public ComboDraw() { DrawMode = DrawMode.OwnerDrawFixed; DropDownStyle = ComboBoxStyle.DropDown; FlatStyle = FlatStyle.Flat; SetStyle(ControlStyles.UserPaint, true); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.FillRectangle(new SolidBrush(Color.Green), ClientRectangle); } } } --Ian; -- modified at 9:19 Thursday 13th July, 2006

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              OK. I see what you're looking at. You're seeing the TextBox being drawn by the underlying ComboBox class. The bad news is, as far as I can tell, you can't get rid of it and can't override it. It's completely hidden behind "private" everything. I've also never had to custom draw the entire Combobox. I've only done the drop down list and that turned out to be pretty easy. Why are you trying to custom paint the entire thing? Dave Kreskowiak Microsoft MVP - Visual Basic

              I 1 Reply Last reply
              0
              • D Dave Kreskowiak

                OK. I see what you're looking at. You're seeing the TextBox being drawn by the underlying ComboBox class. The bad news is, as far as I can tell, you can't get rid of it and can't override it. It's completely hidden behind "private" everything. I've also never had to custom draw the entire Combobox. I've only done the drop down list and that turned out to be pretty easy. Why are you trying to custom paint the entire thing? Dave Kreskowiak Microsoft MVP - Visual Basic

                I Offline
                I Offline
                Ian
                wrote on last edited by
                #7

                Why? I ask myself that question all the time. The answer is the same old story of having all controls behave consistently to the color scheme that has been chosen. Sometimes you can't choose your battles, you just have to fight. I have a few more hacks to try in the meantime. --Ian;

                D 1 Reply Last reply
                0
                • I Ian

                  Why? I ask myself that question all the time. The answer is the same old story of having all controls behave consistently to the color scheme that has been chosen. Sometimes you can't choose your battles, you just have to fight. I have a few more hacks to try in the meantime. --Ian;

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  I have no idea how to get past this one. If it wasn't such a PITA composite control, it'd be real easy! Dave Kreskowiak Microsoft MVP - Visual Basic

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups