初学者提问:如何在对话框中更新单个(编辑)控件? [英] Beginners question: how to update a single (edit) control in a dialog ?
问题描述
非常非常新的visual c ++(3天),我有一个问题,我似乎没有找到答案。
如何在对话框应用程序中更新单个编辑控件中的数据?
UpdateData可以解决问题对于整个对话框中的所有控件,但我需要更新一个(编辑)控件,同时让其他控件完全独立。
任何建议都非常感激。
嗨
您需要获取CEdit类型的指针并将其用于数据交换以设置文本或从编辑控件获取文本。以下链接可能有所帮助:
编辑框 [ ^ ]
MFC中的对话数据交换 [ ^ ]
还在MSDN中搜索有关基于MFC Dialog的应用程序编程的更多相关信息:
MSDN [ ^ ]
对话框窗口的每个控件都有一个唯一的ID(静态控件除外)。要访问控件,可以将此ID(请参阅资源视图中的对话框模板)传递给 GetDlgItem() [ ^ ]功能。一旦你有一个指向这个控件的指针,你可以访问成员函数,如SetWindwText()
,它将设置编辑控件的文本:
CEdit * pEdit =(CEdit *)GetDlgItem(IDC_MYEDIT);
pEdit-> SetWindwoText(_T( 新文字));
另一种选择是将成员变量分配给编辑控件。您可以使用资源编辑器执行此操作。然后使用此变量来更改编辑控件的文本(例如m_Edit1.SetWindowText(_T(New Text)))。这是推荐的方式。如果所有可能更改的控件都有这样的成员变量并使用它进行更新,则可以省略对UpdateData()
的调用。
< blockquote>
感谢您的建议。奇怪的是,我在发布问题后1分钟就找到了解决方案。
我基本上使用了Jochen建议的第一种方法,并且工作得很好。
很高兴知道第二个,但我可以看到一个在以后派上用场。
Chandanadhikari建议的第一个链接确实是正确,这是我找到解决方案的地方。
感谢您的帮助。
Hi,
Very very new to visual c++ (3 days) and I have a question I do not seem to find an answer to.
How do you update the data in an individual edit control in a dialog application ?
"UpdateData" does the trick for all controls in the whole dialog but I need to update just one (edit) control while leaving the other ones well alone.
Any suggestion much appreciated.
hi
you need to get a pointer of type CEdit and use it for data exchange to set the text or get the text from the edit control. Following links might help:
editbox[^]
Dialog Data Exchange in MFC[^]
also search in MSDN for more related information on MFC Dialog based application programming:
MSDN[^]
Each control of a dialog window has a unique ID (besides static controls). To access a control, you can pass this ID (see your dialog template in the resource view) to the GetDlgItem()[^] function. Once you have a pointer to this control, you can access member functions likeSetWindwText()
which will set the text for edit controls:
CEdit* pEdit = (CEdit*)GetDlgItem(IDC_MYEDIT); pEdit->SetWindwoText(_T("New text"));
Another option is to assign a member variable to the edit control. You can do this using the resource editor. Then use this variabale to change the text of your edit control (e.g. m_Edit1.SetWindowText(_T("New Text"))). This is the recommended way. If all controls that may be changed have such a member variable and use it for updating, you can omit the calls toUpdateData()
.
Hi,
Thanks for the suggestions. Strangely enough I found the solution myself 1 minute after posting the question.
I have basically used the first method suggested by Jochen and that works just fine.
Good to know about the second one though, I can see that one coming in handy at a later date.
The first link suggested by Chandanadhikari is indeed correct, that is where I found the solution.
Thanks for the help.
这篇关于初学者提问:如何在对话框中更新单个(编辑)控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!