Disable a textbox based on combo box choice
-
Hello!I'm trying to disable a text box (named m_EarthPermeability) and I want to disable it if a certain value is in Combobox (named m_Layers which gets two values "1" and "2"). Both textbox and Combobox are on the same Dialog window. I have written the code below but the textbox is still in "use" after choosing value "2" in Combobox. Here is the code:
CInputView::CInputView()
: m_Layers(0)
, m_EarthPermeability(0)void CInputView::OnLinefeaturesFeatures()
{
// TODO: Add your command handler code here
CInputDoc* pDoc = GetDocument();
CFeaturesDialog DialogWindow;
DialogWindow.m_DialogLayers = m_Layers;
DialogWindow.m_DialogEarthPermeability = m_EarthPermeability;
{
if (m_Layers == 2) {
GetDlgItem(IDC_Permeability)->EnableWindow(FALSE);} }
-
Hello!I'm trying to disable a text box (named m_EarthPermeability) and I want to disable it if a certain value is in Combobox (named m_Layers which gets two values "1" and "2"). Both textbox and Combobox are on the same Dialog window. I have written the code below but the textbox is still in "use" after choosing value "2" in Combobox. Here is the code:
CInputView::CInputView()
: m_Layers(0)
, m_EarthPermeability(0)void CInputView::OnLinefeaturesFeatures()
{
// TODO: Add your command handler code here
CInputDoc* pDoc = GetDocument();
CFeaturesDialog DialogWindow;
DialogWindow.m_DialogLayers = m_Layers;
DialogWindow.m_DialogEarthPermeability = m_EarthPermeability;
{
if (m_Layers == 2) {
GetDlgItem(IDC_Permeability)->EnableWindow(FALSE);} }
To disable a control you have to call
EnableWindow(FALSE)
but in your code snippet you are passingTRUE
. I understand also that the text box is part of the dialog and not of your view. Then you have to useif (m_Layers == 2) {
DialogWindow.GetDlgItem(IDC_Permeability)->EnableWindow(FALSE);
}It is also not clear if the member variables of your dialog are controls (
CEdit
,CComboBox
) or values (CString
,int
). Your code is correct for value types but not for control types (which is indicated by "a text box (named m_EarthPermeability)"). -
To disable a control you have to call
EnableWindow(FALSE)
but in your code snippet you are passingTRUE
. I understand also that the text box is part of the dialog and not of your view. Then you have to useif (m_Layers == 2) {
DialogWindow.GetDlgItem(IDC_Permeability)->EnableWindow(FALSE);
}It is also not clear if the member variables of your dialog are controls (
CEdit
,CComboBox
) or values (CString
,int
). Your code is correct for value types but not for control types (which is indicated by "a text box (named m_EarthPermeability)").I changed the errors you mentioned but still didn't work. So I made some more changes concerning the combobox but didn't work either. Here is the code:
void CInputView::OnLinefeaturesFeatures()
{
// TODO: Add your command handler code here
CInputDoc* pDoc = GetDocument();
CFeaturesDialog DialogWindow;
CString buffer;
buffer.Format(L"%d", m_Layers);
DialogWindow.m_DialogLayers = buffer;
DialogWindow.m_DialogEarthPermeability2 = m_EarthPermeability;
if (m_Layers == 2) {
DialogWindow.GetDlgItem(IDC_EarthPermeability2)->EnableWindow(FALSE);
}
}where
m_DialogLayers
isCString
type and its control type is combobox. Variablem_Layers
isint
variable. -
I changed the errors you mentioned but still didn't work. So I made some more changes concerning the combobox but didn't work either. Here is the code:
void CInputView::OnLinefeaturesFeatures()
{
// TODO: Add your command handler code here
CInputDoc* pDoc = GetDocument();
CFeaturesDialog DialogWindow;
CString buffer;
buffer.Format(L"%d", m_Layers);
DialogWindow.m_DialogLayers = buffer;
DialogWindow.m_DialogEarthPermeability2 = m_EarthPermeability;
if (m_Layers == 2) {
DialogWindow.GetDlgItem(IDC_EarthPermeability2)->EnableWindow(FALSE);
}
}where
m_DialogLayers
isCString
type and its control type is combobox. Variablem_Layers
isint
variable.It can not work that way because the dialog is not shown so far. Before you can change the visibility of a dialog's control you must show the dialog using
DoModal()
(for a modal dialog) orCreate()
andShowWindow()
(for a modeless dialog). But your general approach is wrong. You should put the code to disable the control into the dialog class. Do it there inOnInitDialog()
for the initial display and into handlers upon changings (e.g. by handling the CBN_SELCHANGE notification code (Windows)[^] ) -
It can not work that way because the dialog is not shown so far. Before you can change the visibility of a dialog's control you must show the dialog using
DoModal()
(for a modal dialog) orCreate()
andShowWindow()
(for a modeless dialog). But your general approach is wrong. You should put the code to disable the control into the dialog class. Do it there inOnInitDialog()
for the initial display and into handlers upon changings (e.g. by handling the CBN_SELCHANGE notification code (Windows)[^] )Could you please give me some more information on how to use
CBN_SELCHANGE
cause I am not very familiar with that. You mean to use it inOnInitDialog()
?I tried different cases but still didn't manage to make it work. Thank you in advance for your time and help!! -
Could you please give me some more information on how to use
CBN_SELCHANGE
cause I am not very familiar with that. You mean to use it inOnInitDialog()
?I tried different cases but still didn't manage to make it work. Thank you in advance for your time and help!!In your view you create an instance of the dialog, set the members for the control as already done and finally call
DoModal()
to show the dialog (I will not handle modeless dialogs here). When having a modal dialog the default implementation ofOnInitDialog()
will call CWnd::UpdateData[^] to set the controls from the corresponding member variables. OverrideOnInitDialog()
to do additional initialisation:BOOL CFeaturesDialog::OnInitDialog()
{
// Call default implementation
BOOL bRet = CDialog::OnInitDialog();
// Add specific initialisation here
if (m_DialogLayers == 2)
GetDlgItem(IDC_Permeability)->EnableWindow(FALSE);
return bRet;
}If you want to enable/disable the control when the user changes the combo box selection, you have to add a corresponding handler. Open the dialog resource in the resource editor, right click on the combo box to open the context menu and select the option to add a handler. That will open a dialog that allows you to choose the notification. Select
CBN_SELCHANGE
and apply the settings. That will create a function declaration in the header file, a function body in the source file, and open the source file at the created function. See also Adding an MFC Message Handler[^]. You should become familiar with that because you have to use it frequently with MFC applications. In your case get the pointer to the control and use that to get the index (untested):CFeaturesDialog::OnCbnSelchange()
{
CComboBox *pCombo = (CComboBox*)GetDlgItem(IDC_LAYERS);
GetDlgItem(IDC_Permeability)->EnableWindow(pCombo->GetCurSel() != 2);
}