Results 1 to 8 of 8

Thread: Change Text in a control? NewB

  1. #1

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335

    Change Text in a control? NewB

    OK im using a MFC appwizard and created a dialog based program. Now i am trying to change the text in a EDIT control i have placed on the form. How do i do this?
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  2. #2

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    I guess i should have searched harder i learned it
    Here :

    VB Code:
    1. CClipboardT1Dlg::SetDlgItemText(IDC_EDIT1,"Jason");
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Given that you use MFC you could also create a control variable and use it's SetWindowText method.

    Or better, create a control value variable and use DDX.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    What is DDX and how do i create a control variable?
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    With the wizard, or by looking at how the wizard does it.

    DDX stands for "Dynamic Data eXchange" and it's MFC's mechanism to bind variables to controls. It's built around the virtual method CDialog::DoDataExchange. This method is called indirectly by the CDialog::UpdateData method. You must override DoDataExchange and insert calls to the various (global) DDX_* and DDV_* functions. The most important of those are DDX_String (or DDX_Text, I forgot), which binds a string variable to an edit control and DDX_Control, which binds a control to a variable of the appropriate CWnd-derived control class.
    Other functions retrieve an int that is the index of the selected radio button in a group of radio buttons or limit the text length of an edit control to a maximum.
    Last edited by CornedBee; Jan 21st, 2003 at 09:27 AM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    that sounds so confusing. and complicated i rather do it my way but thanks alot ill look into it
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's very simple. But have it your way. It's better to do something complicated that you're familiar with than something easy that you have to learn (that is, under some circumstances).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    So assuming you have an edit box with the ID IDC_NAME (it's a good idea to give your controls IDs with a meaning) and you want to bind it to a member variable called m_strName.

    You'd do these steps:
    1) Add the variable to the class declaration:
    Code:
    class CClipboardT1Dlg : public CDialog
    {
      // ...
      CString m_strName;
      // ...
    };
    2) Add an override of DoDataExchange (if there isn't already one):
    Code:
    class CClipboardT1Dlg : public CDialog
    {
      // ...
      CString m_strName;
      // ...
      virtual void DoDataExchange(CDataExchange *pDX);
      // ...
    };
    
    // In the .cpp file:
    void CClipboardT1Dlg::DoDataExchange(CDataExchange *pDX)
    {
      CDialog::DoDataExchange(pDX);
    
      // Bind the string variable to the control
      DDX_Text(pDX, IDC_NAME, m_strName);
    }
    Now the variable is bound. What's complicated about that?

    Ok, now how to use that?
    In a modal dialog it's very easy (assuming m_strName is public):
    Code:
    CClipboardT1Dlg dlg;
    dlg.m_strName = strInitialValue;
    INT_PTR res = dlg.DoModal();
    if(res == IDOK)
    {
      AfxMessageBox(dlg.m_strName);
    }
    That's because the data is automatically updated when creating the dialog and when the user hits ok.

    Another possibility is to do such things in the dialog. Then you'll have to call UpdateData yourself:
    Code:
    // Assuming this function is bound to a button
    // click event via the message map:
    void CClipboardT1Dlg::OnCleanupClick()
    {
      // This function cleans up the string in IDC_NAME
      // Get the data
      UpdateData();
      // Clean up the string.
      m_strName.Trim();
      // Write back to edit control
      UpdateData(FALSE);
    }
    As simple as that.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width