Connecting Tech Pros Worldwide Help | Site Map

Custom Combo Box

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 24th, 2008, 10:58 AM
Newbie
 
Join Date: Dec 2008
Posts: 4
Default Custom Combo Box

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 07:04 PM. Reason: moved to C# forum
Reply
  #2  
Old December 24th, 2008, 01:22 PM
vekipeki's Avatar
Expert
 
Join Date: Nov 2007
Posts: 230
Default

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

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

Please use [code] tags when you post code.

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

Thanks Alias.for your Suggestion
Reply
  #6  
Old December 30th, 2008, 07:04 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: Canada :)
Posts: 4,157
Default

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

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

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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.