Problems adding member variables to controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • radders
    New Member
    • Jan 2007
    • 13

    Problems adding member variables to controls

    I've just started working through Sams publishing's "Teach yourself Visual C++ 4 in 21 days", using VC++ version 4.0 on a windows xp machine. My problem occurs when I attempt to add member variables to controls that I have added to a dialog box through the Class Wizard, Member Variables tab. I can only see the Control IDs of the two Push Button Controls that are initially created by the MFC App Wizard - Ok and Cancel, despite having deleted these two controls and added three new Push Button Controls, two Check Box Controls and an Edit Box Control. Is there something that I have to do to refresh the list of Control IDs in the Member Variables tab, as currently I cannot assign variables to any controls?
    Thank you very much,
    radders
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by radders
    I've just started working through Sams publishing's "Teach yourself Visual C++ 4 in 21 days", using VC++ version 4.0 on a windows xp machine. My problem occurs when I attempt to add member variables to controls that I have added to a dialog box through the Class Wizard, Member Variables tab. I can only see the Control IDs of the two Push Button Controls that are initially created by the MFC App Wizard - Ok and Cancel, despite having deleted these two controls and added three new Push Button Controls, two Check Box Controls and an Edit Box Control. Is there something that I have to do to refresh the list of Control IDs in the Member Variables tab, as currently I cannot assign variables to any controls?
    Thank you very much,
    radders
    Hi are you adding control variables via the dialog box from right clicking your dialog box class in the class view pane?

    Comment

    • radders
      New Member
      • Jan 2007
      • 13

      #3
      Originally posted by willakawill
      Hi are you adding control variables via the dialog box from right clicking your dialog box class in the class view pane?
      Hi willakawill
      No, I hadn't been adding the control variables that way.The way it was described in the manual was to add a new variable to the Control ID of the control with which I am trying to use it, through the Class Wizard

      I have now added a variable named: m_TestEdit of type: CString to my dialogue box class, but it still doesn't show up in the Member Variables tab of the Class Wizard, so how do I go about attaching the newly added variable (m_TestEdit) to the edit box I created with ID: IDC_TEST_EDIT so that I can control what is read from/written to the edit box with the variable m_TestEdit?

      Thanks, Radders

      Comment

      • willakawill
        Top Contributor
        • Oct 2006
        • 1646

        #4
        Originally posted by radders
        Hi willakawill
        No, I hadn't been adding the control variables that way.The way it was described in the manual was to add a new variable to the Control ID of the control with which I am trying to use it, through the Class Wizard

        I have now added a variable named: m_TestEdit of type: CString to my dialogue box class, but it still doesn't show up in the Member Variables tab of the Class Wizard, so how do I go about attaching the newly added variable (m_TestEdit) to the edit box I created with ID: IDC_TEST_EDIT so that I can control what is read from/written to the edit box with the variable m_TestEdit?

        Thanks, Radders
        Did you add it as a control variable?

        Comment

        • radders
          New Member
          • Jan 2007
          • 13

          #5
          I right clicked on the class name and chose: Add Variable. This produced a window where I had to specify Variable Type and Variable Declaration, which I set as CString and m_TestEdit respectively. Could you please explain to me how I would specify this variable as a control variable?
          Thanks, Radders

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            Originally posted by radders
            I right clicked on the class name and chose: Add Variable. This produced a window where I had to specify Variable Type and Variable Declaration, which I set as CString and m_TestEdit respectively. Could you please explain to me how I would specify this variable as a control variable?
            Thanks, Radders
            Oops. Just read your first post again. You are using version 4. OK have a look in your dialog cpp for this function and write in your variables here along with the names of your controls.

            Code:
            void CmymfcDlg::DoDataExchange(CDataExchange* pDX)
            {
            	CDialog::DoDataExchange(pDX);
            	DDX_Control(pDX, lblMessage, c_Message);
            	DDX_Control(pDX, txtLeftItem, c_LeftItem);
            	DDX_Control(pDX, txtRightItem, c_RightItem);
            	DDX_Control(pDX, txtStartSection, c_StartSection);
            	DDX_Control(pDX, txtEndSection, c_EndSection);
            	DDX_Control(pDX, txtDenominator, c_Denominator);
            	DDX_Control(pDX, txtDirectoryOut, c_DirectoryOut);
            	DDX_Control(pDX, txtDirectoryIn, c_DirectoryIn);
            	DDX_Control(pDX, txtMinCoef, c_MinCoef);
            	DDX_Control(pDX, lblTestRig, c_Testrig);
            }

            Comment

            • radders
              New Member
              • Jan 2007
              • 13

              #7
              Originally posted by willakawill
              Oops. Just read your first post again. You are using version 4. OK have a look in your dialog cpp for this function and write in your variables here along with the names of your controls.

              Code:
              void CmymfcDlg::DoDataExchange(CDataExchange* pDX)
              {
              	CDialog::DoDataExchange(pDX);
              	DDX_Control(pDX, lblMessage, c_Message);
              	DDX_Control(pDX, txtLeftItem, c_LeftItem);
              	DDX_Control(pDX, txtRightItem, c_RightItem);
              	DDX_Control(pDX, txtStartSection, c_StartSection);
              	DDX_Control(pDX, txtEndSection, c_EndSection);
              	DDX_Control(pDX, txtDenominator, c_Denominator);
              	DDX_Control(pDX, txtDirectoryOut, c_DirectoryOut);
              	DDX_Control(pDX, txtDirectoryIn, c_DirectoryIn);
              	DDX_Control(pDX, txtMinCoef, c_MinCoef);
              	DDX_Control(pDX, lblTestRig, c_Testrig);
              }
              Hi again willakawill!
              That has helped a huge amount as I have now registered the necessary variables to the controls. I've also defined the variables by right-clicking on the class name and adding the variables, one as CString and two as BOOL. However when I try to build the programme I get the following error messages:

              ...error 2664: 'DDX Control' : Cannot convert parameter 3 from 'class CString' to class 'CWnd &' (new behavior; please see help)
              Could you please point out to me where I'm going wrong? Thanks, Radders

              Comment

              • radders
                New Member
                • Jan 2007
                • 13

                #8
                Originally posted by radders
                Hi again willakawill!
                That has helped a huge amount as I have now registered the necessary variables to the controls. I've also defined the variables by right-clicking on the class name and adding the variables, one as CString and two as BOOL. However when I try to build the programme I get the following error messages:

                ...error 2664: 'DDX Control' : Cannot convert parameter 3 from 'class CString' to class 'CWnd &' (new behavior; please see help)
                Could you please point out to me where I'm going wrong? Thanks, Radders
                I ran out of time to edit this, so had to post again. I've found that the cause of my problem was I had to insert "(CWnd&)" before the variable name for the DDX_Controls to parse. Insertion of this has solved the problem, but I don't quite understand how it works - could someone please explain to me why it will only work with (CWnd&) in front of the name - I realise that the variables are of a different type otherwise, but why does that give an error message?
                TIA, Radders

                Comment

                • radders
                  New Member
                  • Jan 2007
                  • 13

                  #9
                  Originally posted by radders
                  Hi again willakawill!
                  However when I try to build the programme I get the following error messages:

                  ...error 2664: 'DDX Control' : Cannot convert parameter 3 from 'class CString' to class 'CWnd &' (new behavior; please see help)
                  Could you please point out to me where I'm going wrong? Thanks, Radders
                  Having now found more time to continue writing this programme I find that the variables initially defined do not function as they should do - ie the code written using them does nothing, but the programme builds. I presume this is caused by my 'bodge' of (CWnd&) to remove the parsing error described above. I guess that this in fact converts them from type BOOL/CString to CWnd& and hence they do not function as I had hoped they would. This bodge also prevents the ClassWizard from working as it then gives a parsing error for the lines where the DDX Controls are defined.
                  So, how do I get the programme to build, without changing the variable type?
                  Thanks, Radders

                  Comment

                  • willakawill
                    Top Contributor
                    • Oct 2006
                    • 1646

                    #10
                    Originally posted by radders
                    Having now found more time to continue writing this programme I find that the variables initially defined do not function as they should do - ie the code written using them does nothing, but the programme builds. I presume this is caused by my 'bodge' of (CWnd&) to remove the parsing error described above. I guess that this in fact converts them from type BOOL/CString to CWnd& and hence they do not function as I had hoped they would. This bodge also prevents the ClassWizard from working as it then gives a parsing error for the lines where the DDX Controls are defined.
                    So, how do I get the programme to build, without changing the variable type?
                    Thanks, Radders
                    These are not string variables. They are control variables so your declaration of them in your class.h file should be CEdit not string. You can then write the ddx code as I showed without the extra stuff. Then you can use them this way.

                    Code:
                    CString stTemp;
                    long InputValue;
                    char message[20];
                    
                    m_MyEditBox.GetWindowTextA(stTemp);
                    InputValue = atol(stTemp);
                    sprintf_s(message, 20, "Hello World");
                    mMyEditBox.SetWindowTextA(message);
                    // and so on...

                    Comment

                    • radders
                      New Member
                      • Jan 2007
                      • 13

                      #11
                      Originally posted by willakawill
                      These are not string variables. They are control variables so your declaration of them in your class.h file should be CEdit not string. You can then write the ddx code as I showed without the extra stuff. Then you can use them this way.

                      Code:
                      CString stTemp;
                      long InputValue;
                      char message[20];
                      
                      m_MyEditBox.GetWindowTextA(stTemp);
                      InputValue = atol(stTemp);
                      sprintf_s(message, 20, "Hello World");
                      mMyEditBox.SetWindowTextA(message);
                      // and so on...
                      Sorry, I think I might not have explained clearly enough what I'm trying to do.
                      I would like to assign the contents of the edit box as a variable and the settings of two check boxes as two more variables. Hence I had one variable of type CString and two of type BOOL.
                      I would then like to use the settings of the check boxes to control the edit box: if the "visible" check box is checked then the edit box is visible, if not then the edit box is invisible. Similarly, if the "enabled" box is checked then the edit box is enabled, if not then the edit box is disabled. Typing various strings into the edit box have different results: paint executes the programme Paint.exe, clock executes the programme Clock.exe and Exit terminates my programme.
                      Do I still define the three variables as type CEdit or should I use some opther definition? Thanks for all your help, radders

                      Comment

                      • willakawill
                        Top Contributor
                        • Oct 2006
                        • 1646

                        #12
                        Originally posted by radders
                        Sorry, I think I might not have explained clearly enough what I'm trying to do.
                        I would like to assign the contents of the edit box as a variable and the settings of two check boxes as two more variables. Hence I had one variable of type CString and two of type BOOL.
                        I would then like to use the settings of the check boxes to control the edit box: if the "visible" check box is checked then the edit box is visible, if not then the edit box is invisible. Similarly, if the "enabled" box is checked then the edit box is enabled, if not then the edit box is disabled. Typing various strings into the edit box have different results: paint executes the programme Paint.exe, clock executes the programme Clock.exe and Exit terminates my programme.
                        Do I still define the three variables as type CEdit or should I use some opther definition? Thanks for all your help, radders
                        As you can see from the code, I have assigned the contents of and editbox to a variable, 'InputValue'. This covers your first requirement.

                        What you have to do is create control variables. Variables that represent your controls in code and then use DDX to communicate with the controls on your form. The control variables that you create will have a lot of methods etc that you would normally see in intellisense but I don't know if you have that in your version of C++.

                        Try it out with just one CEdit control variable and assign values to and from it as in the code I submitted. Show it in DDX as per my earlier submission and get it to work. When you have it working you can move on to a checkbox control variable and so on.

                        Comment

                        Working...