Connecting Tech Pros Worldwide Help | Site Map

Custom Combo Box

  #1  
Old December 24th, 2008, 11:58 AM
Newbie
 
Join Date: Dec 2008
Posts: 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.

Last edited by Frinavale; December 30th, 2008 at 08:04 PM. Reason: moved to C# forum
  #2  
Old December 24th, 2008, 02:22 PM
vekipeki's Avatar
Expert
 
Join Date: Nov 2007
Posts: 231

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.  
  #3  
Old December 26th, 2008, 05:10 AM
Newbie
 
Join Date: Dec 2008
Posts: 4

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.
  #4  
Old December 26th, 2008, 03:12 PM
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,569

re: Custom Combo Box


Please use [code] tags when you post code.

MODERATOR
  #5  
Old December 29th, 2008, 04:17 AM
Newbie
 
Join Date: Dec 2008
Posts: 4

re: Custom Combo Box


Thanks Alias.for your Suggestion
  #6  
Old December 30th, 2008, 08:04 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940
Provided Answers: 8

re: Custom Combo Box


I've moved your thread to the C# forum.
  #7  
Old December 30th, 2008, 08:19 PM
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,362

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?
  #8  
Old December 31st, 2008, 04:33 AM
Newbie
 
Join Date: Dec 2008
Posts: 4

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
setting selected item of a combo box filled with objects Smokey Grindle answers 6 July 18th, 2007 09:35 PM
Custom Combo Box re-draw problem Duncan Barnes-Ceeney answers 9 February 10th, 2006 03:35 AM
DataGrid: Custom combo box column question TT (Tom Tempelaere) answers 0 November 17th, 2005 01:55 AM
Strange problem when doing databinding to a combo box - Please Help CGuy answers 0 July 19th, 2005 05:19 AM