Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am trying to make the background of a combobox hatched. I just want to have the Textbox part of the box hatched, not the button or the dropdown list.

I have written the following code, and it works quite well, despite the fact, that it is hatched only for like 100ms and then the combobox appearance is resettet to the normal style.

C#
public class HatchComboBox : System.Windows.Forms.ComboBox
{
    private bool _HATCHED = false;
        
    public HatchComboBox()
    {
    }

    public bool HATCHED
    {
        get
        {
            return _HATCHED;
        }

        set
        {
            _HATCHED = value;
            OnPaint(new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (HATCHED) 
        {
            Graphics G = e.Graphics;
            G.Clear(this.BackColor);
            HatchBrush HBrush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.Blue, this.BackColor);
            G.FillRectangle(HBrush, 0, 0, this.Width, this.Height);
            HBrush.Dispose();
            //G.Dispose();
        }
    }
}


Maybe you know which property I have to deactivate or so...
thanks in advance

What I have tried:

I have tried to use my code above, but as I said -> the combobox refreshes itself and looks like before within like 100ms.
Posted
Updated 8-Dec-16 3:40am

1 solution

 
Share this answer
 
Comments
PZero1992 8-Dec-16 15:11pm    
Hello Richard,
thank you for your answer, but the problem stays the same... Any other ideas?
Richard MacCutchan 9-Dec-16 7:00am    
I have tried a number of experiments but the OnPaintBackground method never gets called. We need a .NET expert to explain why.

It works for the main window (Form) but not for the ComboBox.
Richard MacCutchan 9-Dec-16 7:20am    
I thinkl the only way to do this is to use one of the OwnerDraw methods as described at ComboBox.DrawMode Property (System.Windows.Forms)[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900