How to add or remove textboxes by changing the options in the combobox in Visual c++?

126 Views Asked by At

I want to know that whether I can add a textbox or label by changing the options in combo box. For example, I have 2 options in Combobox. If I choose the #1, it must show me 2 textboxes, but if I choose #2 it must show 3 textboxes.Can I do something like this in Visual Studio C++?

2

There are 2 best solutions below

0
IInspectable On

This can be done in two ways:

  1. Dynamically create and destroy the edit controls using CreateWindowEx and DestroyWindow.
  2. Statically create your GUI with 3 edit controls and set the visibility of the controls based on the selection using ShowWindow.
9
Ajay On
  • Have as many text-boxes you want. But hide them.
  • Handle CBN_SELCHANGE (ON_CBN_SEL_CHANGE in MFC).
  • In the handler, show (or hide) the text boxes depending on selection.

Showing/hiding text boxes aren't good from UI perspective. You better enable/disable them appropriately. You may put alternate text when they are disabled, and bring back original change when they have to be enabled.

Creating textboxes at runtime, and then deleting them isn't' good approach. You will need to keep track of Win32 UI handle and/or MFC object. This approach will also need more of UI resource creation/deletion, parent-child relationship handling et. al.