Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Disable a textbox based on combo box choice

Disable a textbox based on combo box choice

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lolici
    wrote on last edited by
    #1

    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);

                 }
    }
    
    J 1 Reply Last reply
    0
    • L lolici

      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);

                   }
      }
      
      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      To disable a control you have to call EnableWindow(FALSE) but in your code snippet you are passing TRUE. I understand also that the text box is part of the dialog and not of your view. Then you have to use

      if (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)").

      L 1 Reply Last reply
      0
      • J Jochen Arndt

        To disable a control you have to call EnableWindow(FALSE) but in your code snippet you are passing TRUE. I understand also that the text box is part of the dialog and not of your view. Then you have to use

        if (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)").

        L Offline
        L Offline
        lolici
        wrote on last edited by
        #3

        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 is CString type and its control type is combobox. Variable m_Layers is int variable.

        J 1 Reply Last reply
        0
        • L lolici

          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 is CString type and its control type is combobox. Variable m_Layers is int variable.

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          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) or Create() and ShowWindow() (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 in OnInitDialog() for the initial display and into handlers upon changings (e.g. by handling the CBN_SELCHANGE notification code (Windows)[^] )

          L 1 Reply Last reply
          0
          • J Jochen Arndt

            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) or Create() and ShowWindow() (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 in OnInitDialog() for the initial display and into handlers upon changings (e.g. by handling the CBN_SELCHANGE notification code (Windows)[^] )

            L Offline
            L Offline
            lolici
            wrote on last edited by
            #5

            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 in OnInitDialog()?I tried different cases but still didn't manage to make it work. Thank you in advance for your time and help!!

            J 1 Reply Last reply
            0
            • L lolici

              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 in OnInitDialog()?I tried different cases but still didn't manage to make it work. Thank you in advance for your time and help!!

              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              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 of OnInitDialog() will call CWnd::UpdateData[^] to set the controls from the corresponding member variables. Override OnInitDialog() 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);
              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups