Connecting Tech Pros Worldwide Help | Site Map

Custom Combo Box

Newbie
 
Join Date: Dec 2008
Posts: 4
#1: Dec 24 '08
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.
vekipeki's Avatar
Expert
 
Join Date: Nov 2007
Posts: 231
#2: Dec 24 '08

re: Custom Combo Box


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.  
Newbie
 
Join Date: Dec 2008
Posts: 4
#3: Dec 26 '08

re: Custom Combo Box


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.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#4: Dec 26 '08

re: Custom Combo Box


Please use [code] tags when you post code.

MODERATOR
Newbie
 
Join Date: Dec 2008
Posts: 4
#5: Dec 29 '08

re: Custom Combo Box


Thanks Alias.for your Suggestion
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,076
#6: Dec 30 '08

re: Custom Combo Box


I've moved your thread to the C# forum.
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,387
#7: Dec 30 '08

re: Custom Combo Box


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?
Newbie
 
Join Date: Dec 2008
Posts: 4
#8: Dec 31 '08

re: Custom Combo Box


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
Reply