By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,870 Members | 2,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,870 IT Pros & Developers. It's quick & easy.

Custom Combo Box

P: 4
Hello Sir,
Please help me ?
I am createing a custom combo box control in c#.net 3.0 by inherit combo box. in this control i am using OnPaint and OnPaintBackground to paint combo button .
The problem came when paint event fire's, it Black the inner combo box color(is this is nonclient area?).and not show back color in combo's inner area.
and here dropdownstyle=dropdown.

But when i set dropdownstyle=dropdownlist then it will fine. now the problem with this is it will not show the text in combox after drop down list is closed.
Now couldn't find the way how proceds further.
Thanks.
Dec 24 '08 #1
Share this Question
Share on Google+
7 Replies


vekipeki
Expert 100+
P: 229
You could post some of your code to make it easier to find the problem. Something like this should work anyway (at least when talking about "black background":

Expand|Select|Wrap|Line Numbers
  1. public class CustomCombo : ComboBox
  2. {
  3.     public CustomCombo()
  4.     {
  5.         SetStyle(ControlStyles.UserPaint | 
  6.              ControlStyles.SupportsTransparentBackColor | 
  7.              ControlStyles.OptimizedDoubleBuffer, true);
  8.         this.BackColor = Color.Transparent;
  9.     }
  10.  
  11.     protected override void OnPaint(PaintEventArgs e)
  12.     {
  13.         // custom paint
  14.     }
  15.  
  16.     protected override void OnPaintBackground
  17.             (PaintEventArgs pevent)
  18.     {
  19.         base.OnPaintBackground(pevent);
  20.         // some custom paint code after this
  21.     }
  22. }
  23.  
Dec 24 '08 #2

P: 4
Thanks viki:
here is my code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10.  
  11. namespace ComboBoxControl
  12. {
  13.     public partial class ComboBoxControl1 : ComboBox
  14.     {
  15.         private static string strcmbo = "";
  16.  
  17.         #region Variables
  18.  
  19.         ControlStyles styleTrue = ControlStyles.AllPaintingInWmPaint|
  20.             ControlStyles.DoubleBuffer |
  21.             ControlStyles.FixedHeight |
  22.             ControlStyles.ResizeRedraw |  
  23.           ControlStyles.UserPaint;   
  24.      private Rectangle m_clickButton;
  25.         private Color _ButtonBackColor;
  26.  
  27.         #region Class Constructor
  28.         public ComboBoxControl1()
  29.         {
  30.             InitializeComponent();
  31.  
  32.             SetStyle(styleTrue, true);
  33.             SetStyle(ControlStyles.Selectable, false); //to recive focuse if true
  34.  
  35.         }
  36.         #endregion
  37.  
  38.         #region Override Events
  39.  
  40.  
  41.         protected override void OnPaintBackground(PaintEventArgs pevent)
  42.         {
  43.             base.OnPaintBackground(pevent);
  44.  
  45.             Graphics g = pevent.Graphics;
  46.             BorderWidth = comboBox1.Width;
  47.             Rectangle rect = new Rectangle(comboBox1.Location.X, comboBox1.Location.Y, BorderWidth, comboBox1.Height + 3);
  48.             ControlPaint.DrawBorder(g, rect, _ButtonBackColor, ButtonBorderStyle.Solid);
  49.  
  50.             if (this.Text != "")
  51.             {
  52.                 strcmbo = this.Text;
  53.                 comboBox1.Text = strcmbo;
  54.             }
  55.         }
  56.  
  57.         protected override void OnPaint(PaintEventArgs e)
  58.         {
  59.  
  60.             OnPaintBackground(e);
  61.             OnPaintComboButton(e);
  62.  
  63.             if (this.Text != "")
  64.             {
  65.                 strcmbo = this.Text;
  66.                 comboBox1.Text = strcmbo;
  67.             }
  68.  
  69.         }
  70.  
  71.  
  72.  
  73.         #endregion
  74.  
  75.         #region Virtual Events
  76.         protected virtual void OnPaintComboButtonBackground(System.Windows.Forms.PaintEventArgs pevent)
  77.         {
  78.             Graphics g = pevent.Graphics;
  79.             Rectangle rc = ClientRectangle;
  80.             g.FillRectangle(new SolidBrush(BorderColor), rc.Right - 17, rc.Y + 2, 15, rc.Height - 4);
  81.  
  82.         }
  83.  
  84.         #endregion
  85.  
  86.     }
  87. }
  88.  
this is main code when i paint combo right side button then it will black the inner color of combo?
please fine some solutions.
Is this non client area of combo box.
Dec 26 '08 #3

Curtis Rutland
Expert 2.5K+
P: 3,256
Please use [code] tags when you post code.

MODERATOR
Dec 26 '08 #4

P: 4
Thanks Alias.for your Suggestion
Dec 29 '08 #5

Frinavale
Expert Mod 5K+
P: 9,731
I've moved your thread to the C# forum.
Dec 30 '08 #6

RedSon
Expert 5K+
P: 5,000
Ok... I have no idea what you are asking about. Can you describe or take a screen shot of what it looks like now, and can you describe or make an example of what you want it to look like?
Dec 30 '08 #7

P: 4
Thanks Everybody, i find solution by redrawing the the text in the combo and overrids the Onpaint event in dropdown style.
Thank
Happy New Year
Dec 31 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.