EN_CHANGE and CEdit, simple question
-
A dilemma: I want a "done" button to be disabled until the user adds omething to the CEdit. Fine. I trapped the EN_CHANGE, and the button comes alive as soon as the user enters anything. Problem is, I dont ever want this field to be empty, and if after typing a letter in, the user goes back and deletes it, my "done" button is still enabled. It should only be enabled when theres something in the CEdit. How do I do this? ====================================================================================== My other tactic was to test when the "done' was pressed like this(below) but it freezes my program and doesnt give me a chance to enter something into the CEdit:
void CDlgMineName::OnButtonDone()
{CString UserEntry; m\_editMineName.GetWindowText(UserEntry); while (UserEntry =="") { m\_editMineName.SetFocus(); m\_editMineName.GetWindowText(UserEntry);`why cant I enter anything here?` } It freezes and I have to kill it from task manager. Is it obvious why ?
Thanks, ns
-
A dilemma: I want a "done" button to be disabled until the user adds omething to the CEdit. Fine. I trapped the EN_CHANGE, and the button comes alive as soon as the user enters anything. Problem is, I dont ever want this field to be empty, and if after typing a letter in, the user goes back and deletes it, my "done" button is still enabled. It should only be enabled when theres something in the CEdit. How do I do this? ====================================================================================== My other tactic was to test when the "done' was pressed like this(below) but it freezes my program and doesnt give me a chance to enter something into the CEdit:
void CDlgMineName::OnButtonDone()
{CString UserEntry; m\_editMineName.GetWindowText(UserEntry); while (UserEntry =="") { m\_editMineName.SetFocus(); m\_editMineName.GetWindowText(UserEntry);`why cant I enter anything here?` } It freezes and I have to kill it from task manager. Is it obvious why ?
Thanks, ns
ns wrote: I trapped the EN_CHANGE, So EN_CHANGE isn't sent when user deletes text from edit control? Tomasz Sowinski -- http://www.shooltz.com
Free your mind and your ass will follow.
-
A dilemma: I want a "done" button to be disabled until the user adds omething to the CEdit. Fine. I trapped the EN_CHANGE, and the button comes alive as soon as the user enters anything. Problem is, I dont ever want this field to be empty, and if after typing a letter in, the user goes back and deletes it, my "done" button is still enabled. It should only be enabled when theres something in the CEdit. How do I do this? ====================================================================================== My other tactic was to test when the "done' was pressed like this(below) but it freezes my program and doesnt give me a chance to enter something into the CEdit:
void CDlgMineName::OnButtonDone()
{CString UserEntry; m\_editMineName.GetWindowText(UserEntry); while (UserEntry =="") { m\_editMineName.SetFocus(); m\_editMineName.GetWindowText(UserEntry);`why cant I enter anything here?` } It freezes and I have to kill it from task manager. Is it obvious why ?
Thanks, ns
For the EN_CHANGE you would just write
CString name ;
m_editMineName.GetWindowText(name) ;
GetDlgItem(IDC_BUTTON_DONE)->EnableWindow(name.GetLength() > 0) ;Roger Allen Sonork 100.10016 I think I need a new quote, I am on the prowl, so look out for a soft cute furry looking animal, which is really a Hippo in disguise. Its probably me.
-
ns wrote: I trapped the EN_CHANGE, So EN_CHANGE isn't sent when user deletes text from edit control? Tomasz Sowinski -- http://www.shooltz.com
Free your mind and your ass will follow.
It probably is, but heres what I do in the function:
void CDlgMineName::OnChangeEditminename()
{m\_buttonminename.EnableWindow(TRUE);
}
If the change was of a "negative type" i.e. nothings left in the CEdit, I want to EnableWindow(FALSE), so should I do a GetWindowText in this event and see if its empty?
void CDlgMineName::OnChangeEditminename()
{
CString abc;
m_myEdit.GetWindowText(abc);
if(abc=="")
{
m_buttonminename.EnableWindow(FALSE);
}
else
{
m_buttonminename.EnableWindow(TRUE);
}}
What do you think about this? Too contrived? Thanks, ns:)
-
For the EN_CHANGE you would just write
CString name ;
m_editMineName.GetWindowText(name) ;
GetDlgItem(IDC_BUTTON_DONE)->EnableWindow(name.GetLength() > 0) ;Roger Allen Sonork 100.10016 I think I need a new quote, I am on the prowl, so look out for a soft cute furry looking animal, which is really a Hippo in disguise. Its probably me.
-
It probably is, but heres what I do in the function:
void CDlgMineName::OnChangeEditminename()
{m\_buttonminename.EnableWindow(TRUE);
}
If the change was of a "negative type" i.e. nothings left in the CEdit, I want to EnableWindow(FALSE), so should I do a GetWindowText in this event and see if its empty?
void CDlgMineName::OnChangeEditminename()
{
CString abc;
m_myEdit.GetWindowText(abc);
if(abc=="")
{
m_buttonminename.EnableWindow(FALSE);
}
else
{
m_buttonminename.EnableWindow(TRUE);
}}
What do you think about this? Too contrived? Thanks, ns:)
One line does it all:
m_button.EnableWindow(m_edit.GetWindowTextLength() > 0);
Tomasz Sowinski -- http://www.shooltz.com
Free your mind and your ass will follow.
-
A dilemma: I want a "done" button to be disabled until the user adds omething to the CEdit. Fine. I trapped the EN_CHANGE, and the button comes alive as soon as the user enters anything. Problem is, I dont ever want this field to be empty, and if after typing a letter in, the user goes back and deletes it, my "done" button is still enabled. It should only be enabled when theres something in the CEdit. How do I do this? ====================================================================================== My other tactic was to test when the "done' was pressed like this(below) but it freezes my program and doesnt give me a chance to enter something into the CEdit:
void CDlgMineName::OnButtonDone()
{CString UserEntry; m\_editMineName.GetWindowText(UserEntry); while (UserEntry =="") { m\_editMineName.SetFocus(); m\_editMineName.GetWindowText(UserEntry);`why cant I enter anything here?` } It freezes and I have to kill it from task manager. Is it obvious why ?
Thanks, ns
Hi, Please find below a possible way to do it using MFC. It is in your lines of thought and EN_CHANGE does it. When the application starts the button will be disabled. As you type in the edit box or delete text EN_CHANGE fires. Keep on measuring the length of the string. Code follows: Class declaration:
class CEditControlDlg : public CDialog { // Construction public: CEdit *m_pEdit; **// Added by Sayan** CButton *m_pButton; **// Added by Sayan** CEditControlDlg(CWnd* pParent = NULL); // standard constructor // ... // ... }
Function definition - InitDialog():BOOL CEditControlDlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_pEdit = (CEdit *)GetDlgItem(EF_TEXT); **// Added by Sayan** m_pButton = (CButton *)GetDlgItem(PB_DONE); **// Added by Sayan** m_pButton->EnableWindow(FALSE); // Added by Sayan return TRUE; // return TRUE unless you set the focus to a control }
Function definition - EN_CHANGE handler function:void CEditControlDlg::OnChangeText() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here CString strText; m_pEdit->GetWindowText(strText); if (!strText.GetLength()) { m_pButton->EnableWindow(FALSE); } else { m_pButton->EnableWindow(); } }
With best regards, Sayan Email:sayanmukherjee@indiatimes.com