CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2010
    Posts
    4

    MFC - How to set value of edit box on initialisation of dialog

    This feels like it should be the simplest thing in the world, but after days of searching the web, and reading through tens of tutorials/technical papers, I'm still none the wiser about how to do this.

    Basically, there is functionality in my application to edit the member data of objects stored in a doubly linked list. The add functionality is absolutely fine as the controls start blank, but for the edit I need to be able to populate the edit controls with the current data from the object.

    For example, I have an IDC_EDIT control, described in the DoDataExchange function as:
    Code:
    DDX_Text(pDX, IDC_EDIT3, manufacturer);
    this has the variable manufacturer assigned to it as a CString value. Not all of the edit boxes on the page are this type, but I just include this for example.

    I just need someone to point me in the right direction on how to set the values of these edit boxes so that they show up in the edit boxes on the .DoModal() call. I'm at my wit's end with this one!

    Thanks in advance.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: MFC - How to set value of edit box on initialisation of dialog

    Assign manufacturer whatever value you want to appear in the edit control. Do it after the dialog object is created, but before CDialog:oModal is called. One typical implementation may look like
    Code:
    CMyDialog dlg;
    dlg.manufacturer = "Acme";
    dlg.DoModal();

  3. #3
    Join Date
    May 2010
    Posts
    4

    Re: MFC - How to set value of edit box on initialisation of dialog

    Aah, you sir are a hero! I will try this now, thanks

  4. #4
    Join Date
    May 2010
    Posts
    4

    Re: MFC - How to set value of edit box on initialisation of dialog

    Ooh another problem. What you suggested worked a treat with the edit boxes, is there something special I need to do for 'CComboBox' as CComboBox::SetCurSel is throwing assert errors for me.

    In my add dialog, I get the selection made by the user with CComboBox::GetCurSel() which gives me an integer and the whole process is fine, but SetCurSel with the same integer isn't working on initialisation.

    Thanks for your help before!

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: MFC - How to set value of edit box on initialisation of dialog

    Quote Originally Posted by chrisvarnz View Post
    Ooh another problem. What you suggested worked a treat with the edit boxes, is there something special I need to do for 'CComboBox' as CComboBox::SetCurSel is throwing assert errors for me.

    In my add dialog, I get the selection made by the user with CComboBox::GetCurSel() which gives me an integer and the whole process is fine, but SetCurSel with the same integer isn't working on initialisation.

    Thanks for your help before!
    You can only perform actually access the control after windows has created the control. Directly accessing the combo, or any other control, can only be done after the call to CDialog::OnInitDialog() which is called from your own dialog's OnInitDialog() method.

  6. #6
    Join Date
    May 2010
    Posts
    4

    Re: MFC - How to set value of edit box on initialisation of dialog

    Thanks again, VS2008 doesn't generate an OnInitDialog by default in my code, but originally I had a play with it trying to get my original problem to work. Am I to understand then that if I manually define MyDialog::OnInitDialog() and within that make a call to CDialog::OnInitDialog() (kind of like OnOK() makes a call to CDialog::OnOK() to run the non-overridden implementation), I can then within the same definition, change the selection of the combo box? Also assuming that OnInitDialog is run automatically on the call to DoModal.

    To avoid asking too many questions in the future, can you point me the direction of some beginners/advanced reference material for MFC, as you clearly know your stuff :P. I've only been using it for a month but I'm getting there.

    Thanks!

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: MFC - How to set value of edit box on initialisation of dialog

    Quote Originally Posted by chrisvarnz View Post
    Thanks again, VS2008 doesn't generate an OnInitDialog by default in my code, but originally I had a play with it trying to get my original problem to work. Am I to understand then that if I manually define MyDialog::OnInitDialog() and within that make a call to CDialog::OnInitDialog() (kind of like OnOK() makes a call to CDialog::OnOK() to run the non-overridden implementation), I can then within the same definition, change the selection of the combo box? Also assuming that OnInitDialog is run automatically on the call to DoModal.

    To avoid asking too many questions in the future, can you point me the direction of some beginners/advanced reference material for MFC, as you clearly know your stuff :P. I've only been using it for a month but I'm getting there.

    Thanks!
    There should be some option in 2008 to add OnInitDialog into your class so you don't have to do it yourself. It will add the call to the base class version that actually creates the controls.

    I'm not really up on all the latest books. Look for something that's written for VS2008 and MFC if that's what you're using.

Tags for this Thread

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

Featured