Setting the background color in wxChoice

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
pkl
Knows some wx things
Knows some wx things
Posts: 36
Joined: Mon Jan 30, 2017 11:46 pm

Setting the background color in wxChoice

Post by pkl »

As an indication that the user needs to set a value, I would like to set the background colour on certain input-controls to e.g. yellow.
This works fine for wxTextCtrl, but for wxChoice the colour remains the same.
Anyone out there with an idea of how to fix this - or perhaps give the user a hint that a wxChoice needs to be filled out?

BR
Peter
Anil8753
Experienced Solver
Experienced Solver
Posts: 93
Joined: Sat Jan 16, 2016 5:57 am
Location: India

Re: Setting the background color in wxChoice

Post by Anil8753 »

I feel there is no direct API to do it.
But you can use the following trick implement the desire behaviour.

Code: Select all

   
   #include <wx/artprov.h>
   
   
    m_pSizer = new wxBoxSizer(wxHORIZONTAL);

    wxChoice* pChoice = new wxChoice(this, wxID_ANY);
    pChoice->AppendString("One");
    pChoice->AppendString("Two");
    pChoice->AppendString("Three");
    m_pSizer->Add(pChoice);

    pChoice->Bind(wxEVT_PAINT, [=](wxPaintEvent& event){

        wxPaintDC dc(pChoice);
        wxRect rect(0, 0, dc.GetSize().GetWidth(), dc.GetSize().GetHeight());

        dc.SetBrush(*wxYELLOW_BRUSH);
        dc.SetPen(*wxGREY_PEN);
        dc.DrawRectangle(rect);

        dc.DrawBitmap(wxArtProvider::GetBitmap(wxART_GO_DOWN, wxART_OTHER, wxSize(10, 10)), wxPoint(rect.GetWidth() - 15, (rect.GetHeight() / 2) - 5));
        const wxString sel = pChoice->GetStringSelection();

        if (!sel.IsEmpty())
        {
            dc.DrawLabel(sel, rect, wxALIGN_CENTER_VERTICAL);
        }
    });
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Setting the background color in wxChoice

Post by evstevemd »

pkl wrote:As an indication that the user needs to set a value, I would like to set the background colour on certain input-controls to e.g. yellow.
This works fine for wxTextCtrl, but for wxChoice the colour remains the same.
Anyone out there with an idea of how to fix this - or perhaps give the user a hint that a wxChoice needs to be filled out?

BR
Peter
This is not what you asked for but as an alternative, you could disable all other control that depend on that wxChoice until user selects it. Also you could enable them and when user tries to access something that depends on that choice control send them to choice control and show a tooltip that they cannot do that until they fill the choice.

I would go for the latter as it is user friendly.

NOW to answer your question is some native controls don't allow such kind of changes. There are generic widgets that are not native and you can have look at them and see if it is what you want: https://github.com/wxWidgets/wxWidgets/ ... wx/generic
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7467
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Setting the background color in wxChoice

Post by ONEEYEMAN »

Hi,
What is your platform/toolkit?
Is it possible to change it to use wxComboBox?

Thank you.
pkl
Knows some wx things
Knows some wx things
Posts: 36
Joined: Mon Jan 30, 2017 11:46 pm

Re: Setting the background color in wxChoice

Post by pkl »

Thank you for your replies. I have been on vacation and not had time to persue this further before now. I will try to implement the solution suggested by Amil or perhaps OneEyeMan.
EvStevemds solution can not be used in my application because it is more complex than that. A choice might be part of a group of controls where either all or no controls should be filled out. Thus, filling out a text-field or a listcontrol might require the choice to need input.
My platform is Linux and Windows.

Best regards
Peter
pkl
Knows some wx things
Knows some wx things
Posts: 36
Joined: Mon Jan 30, 2017 11:46 pm

Re: Setting the background color in wxChoice

Post by pkl »

Hello again,

So amils suggestion does not work well enough - the control becomes clearly non-standard. But thank you for the suggestion.
I will check the documentation for wxComboBox next.

Best regards
Peter
Post Reply