Home > database >  C mfc ComboBox grayed out selection
C mfc ComboBox grayed out selection

Time:06-22

I have a question and need help. I have 2 ComboBox, if the user picks A from ComboBox1, I want A from ComboBox2 to be grayed out and unselectable. Is it possible to do so? How can I do that?

BOOL CEngravingCalculatorDlg::OnInitDialog()
{
   CComboBox* empcombo1 = static_cast<CComboBox*>(GetDlgItem(IDC_COMBO1));
   empcombo1->AddString(L"A");
   empcombo1->AddString(L"B");
   empcombo1->AddString(L"C");
   empcombo1->SetCurSel(0);
   UpdateData(FALSE);

   CComboBox* empcombo2 = static_cast<CComboBox*>(GetDlgItem(IDC_COMBO2));
   empcombo2->AddString(L"A");
   empcombo2->AddString(L"B");
   empcombo2->AddString(L"C");
   empcombo2->SetCurSel(0);
   UpdateData(FALSE);
}

CodePudding user response:

A standard ComboBox has no concept of disabled/unselectable items.

You can either:

  1. simply add/remove items to/from a ComboBox dynamically as needed. For instance, if the user picks "A" in ComboBox1 then remove "A" from ComboBox2, and if the user picks anything else in ComboBox1 then re-add "A" to ComboBox2.

  2. custom-draw the ComboBox to make unselectable items appear disabled. If the user tries to select a "disabled" item, ignore it, and/or just select a nearby item that is not "disabled".

CodePudding user response:

TL;DR: Resist the temptation to be helpful. It leads to a catastrophic UX. Instead, follow these principles in designing your UI:

  • Allow the user to enter any data at any time
  • Prevent the UI from submitting invalid data to the application by en-/disabling the "Ok"/"Submit" button in response to input
  • [Bonus: Provide visual cues near the points of interaction for immediate visual feedback of invalid/inconsistent data in addition to the disabled "Ok"/"Submit" button]

What the question is asking for is not supported by a standard combo box control, nor does MFC provide any utility implementations to accomplish that (more easily). Even if it were possible, it would likely produce a poor interface that users will perceive as hostile.

Reading between the lines here, I'm guessing the question is asking about input validation, and the rule being that the same entry cannot be selected from both combo boxes at the same time. While that is a valid constraint, disallowing the user to temporarily violate that constraint is what makes for poor UX (Raymond Chen remarks on issues with this approach in his blog entry WM_KILLFOCUS is the wrong time to do field validation).

Just to illustrate how this would work out in practice, consider a user trying to enter A B, but halfway through deciding to select B A instead. This is their story:

  • User selects A from CB1 (planning to enter A B)
  • User changes their mind, and now decides to go with B A instead while they're interacting with CB2
  • User realizes that they cannot select A from CB2, so they're picking the next closest, B
  • User moves back to CB1 and realizes that they cannot pick B (their final goal), so they choose the only entry available: C
  • User now goes back to CB2 to enter A (which they had planned on doing all along)
  • User navigates back to CB1 to enter B

What should have taken 3 steps turned into a confusing scavenger hunt, with the UI fighting back at every single step along the way.

That's not the UI anyone wants to interact with, so it's best to just scrap this idea. A far better approach would be to do non-intrusive input validation: Whenever the user picks an entry from either combo box, verify that the input is valid, and disable the "Ok"/"Submit"/whatever button.

This prevents the application from receiving invalid input, but still allows the user to temporarily enter invalid data. You could also consider placing visual cues such as a warning icon (⚠️) next to the control(s) that still contain invalid data, so that the user receives immediate feedback.

Since this question carries the tag: MFC offers its own system for dialog data validation (DDV). While this is convenient to use it fails to deliver immediate visual feedback, and has no predefined routines to validate input that needs to be consistent across multiple controls.

  • Related