Thread: Edit controls of a dialog from another dialog...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    5

    Edit controls of a dialog from another dialog...

    Hello

    I'm having hard time with MFC. Can someone help me?

    My program have two dialogs in a resource (I generated it by using MFC App Wizard (EXE), then i added a second dialog).

    Dialog 1 contains:
    - A button with a caption "Add New String".
    - A ListBox

    Dialog 2 contains:
    - A button with a caption "Add"
    - An EditBox

    So, when the program is started the first dialog is shown (DoModal).
    When the user presses "Add New String", the second dialog come up where the user should enter it into the EditBox. Then when the "Add" button is pressed, the second dialog is closed (EndDialog) and the string is added to the ListBox on the first dialog.

    I created a member varable (using Class Wizard) called "m_List1" for the ListBox (on dialog 1). But i cannot access it from my second dialog (SecondDLG.cpp). Why?

    I tried to make an extern pointer to the first dialog, hoping to access the variable through it, but it doesn't work.

    Any ideas? MFC is really driving me crazy...

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Have you tried using modeless dialogs?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    No, I'm using modal ones. The user should not be able to access dialog 1 before closing dialog 2.
    Last edited by Snake; 06-30-2005 at 01:53 AM.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    when you call void CDialog::EndDialog( int nResult );, cast your string pointer to nResult.

    cast it back to a string pointer when CDialog::Domodal returns.

    depending on your implementation you will probably need to allocate with new the string to be passed back

    so to recap in pseudocode
    Code:
    CString* aString;
    aString = (CString*)myDialog.Domodal(...);// technically you should use reinterpret_cast
    ...
    myDialog::OnOK(...)
    {
         CString returnString = new CString;
         returnString = valueOfEditBox;
         EndDialog( (int)returnString); // technically you should use reinterpret_cast here too
    }

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Thanks for the help


    What if i need to return multiple strings, not just one? Like if i have more than one EditBox?

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    return a pointer to an array of strings or to a custom struct you create to contain each of the edit box strings

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Thanks. I was thiniking about this solution too.
    So, i can't access the first dialog anyhow, without ending the second one first (because it's in a DoModal loop, right)?

    TC

  8. #8
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I am not sure, I would have to test it. MSDN only says the user can't interact with other windows. I would guess that it would be able to get the value of an edit box for example.

    However, even if it can't, you could set a global or static variable with the values prior to calling domodal (MFC) or Dialogbox (API)

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Just found this on msdn in regard to your first question ...apparantly, you can get the values after the Dialog window is closed.

    For a modal dialog box, you can retrieve any data the user entered when DoModal returns IDOK but before the dialog object is destroyed. For a modeless dialog box, you can retrieve data from the dialog object at any time by calling UpdateData with the argument TRUE and then accessing dialog class member variables. This subject is discussed in more detail in Dialog Data
    http://msdn.microsoft.com/library/de...log_object.asp

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-23-2005, 08:00 AM
  2. edit controls
    By ZerOrDie in forum Windows Programming
    Replies: 11
    Last Post: 04-08-2003, 12:09 PM
  3. Lame question on dialog controls appearance
    By Templario in forum Windows Programming
    Replies: 2
    Last Post: 03-18-2003, 08:22 PM
  4. The relation between Font Size & Dialog Units
    By Templario in forum Windows Programming
    Replies: 4
    Last Post: 02-27-2003, 05:32 PM
  5. Dialog Edit box v's WindowsText Problem
    By simham_uk in forum Windows Programming
    Replies: 2
    Last Post: 06-10-2002, 07:28 AM