2
votes

I added a extra Dialog (TestDialog) in a MFC doc/frame project (Single doc, MFC, VC++2010 project) That dialog has a "EDIT Control" (IDC_EDIT1, m_EditBox1 etc) box. After starting the MFC program from the DOC/frame Menu I selected the testdialog which then popup or start.

But Whenver I start that Testdialog the EDIT Control box appear as empty and I have to type a starting value (say 100) so that I can press a button (inside testdialog) that runs a program which accept 100 as input.

How and where I can add a starting value say 100 to this Edit control so that when testdialog will open the EDIT control box already will have that default vale ( i,e 100).

TestDialog.cpp file shows

CTestDialog::CTestDialog(CWnd* pParent /*=NULL*/)
: CDialogEx(CTestDialog::IDD, pParent)
, testdlg(0)

{
}
5

5 Answers

6
votes

You can use OnInitDialog () to set any dialog values before the dialog displays. There are multiple ways to accomplish it. Here are two...

  1. You can use SetWindowText to insert a string value into the control.
  2. Define the control with a member variable that accepts a CString value. Assign a default value to the variable. OnInitDialog should handle updating the field.
5
votes

Go to InitDialog function of your test dialog class (if InitDialog() not already present, override it).

Then , Do this:

GetDlgItem(IDC_EDIT1)->SetWindowTextA("100");
0
votes

Thanks to all of you !! I finally did it like that a) In Testdialog.h file in CtestDialog class I added I declared OnInitDialog() by adding a line --> virtual BOOL OnInitDialog( )

    class CTestDialog : public CDialogEx
    {

    DECLARE_DYNAMIC(CTestDialog)

    virtual BOOL OnInitDialog( );

    public:

    CTestDialog(CWnd* pParent = NULL);   // standard constructor
    virtual ~CTestDialog();

    public:

    Private:
}

and then in TestDialog.cpp I added

BOOL CTestDialog::OnInitDialog()
{
   CDialog::OnInitDialog();

   m_EditBox1.SetWindowText(_T("100"));

  //  GetDlgItem(IDC_EDIT1)->SetWindowTextA("100");


     return TRUE;
}

And then everything worked.

0
votes

I am a newbe but this is a method I found working... (don't know how correct it is..) I add a variable to the "EDIT control" and use a constructor member initialisation list (here i_num_days(91) sets the i_num_days to 91:

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
  : CDialogEx(CMyDlg::IDD, pParent)
  , i_num_days(91)
  , ..
{
  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

and then in the following it is connected to the "Edit control":

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialogEx::DoDataExchange(pDX);
  DDX_Text(pDX, IDC_EDIT_NUMBER_OF_DAYS, i_num_days);
  DDV_MinMaxInt(pDX, i_num_days, 1, 366);
  ..
}

If someone knows this to be wrong, please correct me...

0
votes

If you use setwindowtext(L" "); then still the edit control's caret will be shown after a space...in order to get out of this situation..u can go for this option..place this code in the event handler of the edit control

CString name=L" ";
GetDlgItem(IDC_Editname,name);

if(name==L" ")
{
    CEdit* pname=(CEdit*)GetDlgItem(IDC_EDITNAME);
    pname->SetFocus();
    pname->SetSel(0,-1);
    pname->SetSel(0);
}