简体   繁体   English

如何在添加到MFC Doc / Frame项目的对话框中为“编辑控件框”设置默认值

[英]How to set a default value for “Edit control box” in a Dialog that is added to MFC Doc/Frame project

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. 我在MFC doc / frame项目(Single doc,MFC,VC ++ 2010项目)中添加了一个额外的Dialog( TestDialog )。该对话框有一个“EDIT Control”( IDC_EDIT1, m_EditBox1 etc )框。 After starting the MFC program from the DOC/frame Menu I selected the testdialog which then popup or start. 从DOC / frame菜单启动MFC程序后,我选择了testdialog,然后弹出或启动。

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. 但是,当我启动Testdialog时,EDIT控制框显示为空,我必须输入一个起始值(比如说100),这样我就可以按下一个按钮(在testdialog中)运行一个接受100作为输入的程序。

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). 如何以及在哪里可以添加一个起始值,对这个Edit控件说100,这样当testdialog打开时,EDIT控件盒已经有了默认值(i,e 100)。

TestDialog.cpp file shows TestDialog.cpp文件显示

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

{
}

You can use OnInitDialog () to set any dialog values before the dialog displays. 您可以使用OnInitDialog()在对话框显示之前设置任何对话框值。 There are multiple ways to accomplish it. 有多种方法可以实现它。 Here are two... 这是两个......

  1. You can use SetWindowText to insert a string value into the control. 您可以使用SetWindowText将字符串值插入控件。
  2. Define the control with a member variable that accepts a CString value. 使用接受CString值的成员变量定义控件。 Assign a default value to the variable. 为变量分配默认值。 OnInitDialog should handle updating the field. OnInitDialog应该处理更新字段。

Go to InitDialog function of your test dialog class (if InitDialog() not already present, override it). 转到测试对话框类的InitDialog函数(如果Initdialog()尚未存在,则覆盖它)。

Then , Do this: 然后,这样做:

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

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( ) 我终于这样做了a)在CtestDialog类的Testdialog.h文件中我添加了我通过添加一行来声明OnInitDialog() - > 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 然后在TestDialog.cpp中我添加了

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

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

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


     return TRUE;
}

And then everything worked. 然后一切正常。

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: 我是一个newbe,但这是我发现工作的方法...(不知道它是多么正确..)我将一个变量添加到“EDIT控件”并使用构造函数成员初始化列表(这里i_num_days(91)i_num_days设置为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... 如果有人知道这是错的,请纠正我...

If you use setwindowtext(L" "); 如果你使用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM