Visual C++ - MFC how to change disablity of a button using edit box - visual-c++

I'm currently working with MFC, and I want to make a simple account managment.
I made a login button which is set to disabled from the start and 2 edit box which each one of them is a user id and a password.
I want to make a simple thing : if one of the edit box has no value at all then make the loggin button disabled, else..make the button available.
However, the code doesn't work at all.
this is the code :
part of the header file
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
private:
// Value of the "Username" textbox
CString m_CStr_UserID;
// Control variable of the "Username" textbox
CEdit m_CEdit_ID;
// Value of the "Password" textbox
CString m_CStr_UserPass;
// Control variable of the "Password" textbox
CEdit m_CEdit_PASS;
// Control variable of the "Login" button
CButton m_Btn_Login;
public:
afx_msg void OnEnChangeEditId();
afx_msg void OnEnChangeEditPass();
proceed to the .cpp
.....
void CTestDlg::OnEnChangeEditId()
{
// 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
m_CEdit_ID.GetWindowTextW(m_CStr_UserID);
if(!m_CStr_UserID.IsEmpty() && !m_CStr_UserPass.IsEmpty())
m_Btn_Login.EnableWindow(TRUE);
m_Btn_Login.EnableWindow(FALSE);
}
void CTestDlg::OnEnChangeEditPass()
{
// 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
m_CEdit_PASS.GetWindowTextW(m_CStr_UserPass);
if(!m_CStr_UserPass.IsEmpty() && !m_CStr_UserID.IsEmpty())
m_Btn_Login.EnableWindow(TRUE);
m_Btn_Login.EnableWindow(FALSE);
}
What's wrong with the code?

In both handlers it's always being enabled FALSE. I think you're missing an else
You either need to return after the EnableWindow(TRUE) or use an else.

acraig5057 explained the problem with your code and showed you how to work around it. I will just add that you can also do this: map the EN_CHANGE for both edit controls to one handler, let's call it OnEnChangeEditIdOrPass:
void CTestDlg::OnEnChangeEditIdOrPass()
{
m_Btn_Login.EnableWindow((m_CEdit_ID.GetWindowTextLength() != 0) &&
(m_CEdit_PASS.GetWindowTextLength() != 0));
}
The function GetWindowTextLength returns the number of characters in the specified edit control and 0 if the edit control is empty.
The logic of the above code is this: if both the edit boxes have characters in them, the && will return TRUE and the login button will be enabled. If at least one of them does not, the && will return FALSE and the login button will be disabled.
Of course, this code not save the values of the username and the password edit controls into string variables, like your code does, but you can always call GetWindowText from inside the handler of the login button.

Related

Conditionally stopping a CPropertySheet from closing from the page OnOK button handler

I have just encountered an issue with CPropertyPage.
I have been trying to use the OnOK handler to do some validation:
void CCalendarSettingsGooglePage::OnOK()
{
bool bHandle = false;
UpdateData(TRUE);
// AJT v20.2.0 — We need to pass "true" so that the error message will display!
if (ValidSettings(true))
{
bHandle = true;
SaveSettings();
}
if (bHandle)
CMFCPropertyPage::OnOK();
}
The problem is, the sheet still closes down. I had hoped that preventing the CMFCPropertyPage::OnOK would have stopped the sheet closing. But it doesn't.
I understand from here that the sheet's OnOK is making a EndDialog(IDOK) call. But i don't want to complicate my sheet. The testing is here in this page. so I need a was for the sheet to know if it should close or not when user clicks the OK button.
You need to override the OnCommand handler of your property page's parent property sheet class and intercept the clicks for the IDOK command (which will be given in the wParam parameter). If you do not call the base class OnCommand but still return TRUE to indicate that your have handled the command, then the property sheet will not close:
BOOL MyPropertySheet::OnCommand(WPARAM wParam, LPARAM lParam)
{
if (wParam == IDOK) { // OK button clicked...
if (!ValidSettings(true)) return TRUE; // NOT valid, prevent further processing.
}
// You can also intercept the "Apply" command by testing for ID_APPLY_NOW
// Everything is OK, so continue processing ...
return CMFCPropertySheet::OnCommand(wParam, lParam);
}
Note that I have assumed your parent is derived from CMFCPropertySheet, but the same works for the 'older' CPropertySheet.

Enable/Disable Edit box with help of Check-box control (MFC)

I have a checkbox and an edit control. I want to disable the Edit control when Check-box is 'not checked', and enable Edit control when Check-box is 'checked.
OnBnClickedCheck1 gets called when I check/uncheck the check-box. m_CHECK1_VARIABLE tells me if the check box is checked or un-checked. If-else part is executed correctly but m_TEXT1_CONTROL.EnableWindow(FALSE/TRUE) doesn't seem to work.
Below is the code.
void CPreparationDlg::OnBnClickedCheck1()
{
UpdateData(TRUE);
if (m_CHECK1_VARIABLE)
{
m_TEXT1_CONTROL.EnableWindow(TRUE);
}
else if (m_CHECK1_VARIABLE)
{
m_TEXT1_CONTROL.EnableWindow(FALSE);
}
}
There are 2 cases.
When Edit-box is disabled by default when dialog pops up.
If the Edit-box is enabled by default (I set 'Disabled' behavior in dialog properties to 'False'), Edit-box stays enabled throughout the operation. (check and uncheck operation on check-box)
When Edit-box is enabled by default when dialog pops up.
When I disable the Edit-box by default (I set 'Disabled' behavior in dialog properties to 'True'), Edit-box becomes enabled on 'first' 'check' on the Check-box but stays enabled throughout the rest of the operation. (check and uncheck operation on check-box).
What is it that I am missing here?
The following code example will implement the required logic.
Header file:
public:
int m_Check;
CEdit m_EditBox;
afx_msg void OnBnClickedCheck1();
Class implementation source:
CMfcApplicationDlg::CMfcApplicationDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMfcApplicationDlg::IDD, pParent)
, m_Check(0) // Default checkbox state
{
// ...
}
void CMfcApplicationDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, m_EditBox);
DDX_Check(pDX, IDC_CHECK1, m_Check);
m_EditBox.EnableWindow(m_Check);
}
void CMfcApplicationDlg::OnBnClickedCheck1()
{
UpdateData();
}
All required functionality can be implemented inside the DoDataExchange() method. First time the edit box control state set according to the m_Check default value. And each next time the edit box control state will be triggered by OnBnClickedCheck1() event.
IMHO using DoDataExchange(..) to maintain the state of a dialog is dicey at best. Add a member like UdateState( ) and use that. Stage the dialog in OnInitDialog( ) with anything that doesn't easily initialize in the constructor and call UpdateState( ).
Use DoDataExchange(..) only to do what it sounds like, exchange data between the dialog and objects. This way you won't paint yourself into a corner as the Dialog evolves.
//....h
CEdit m_EditBox;
CButton m_CheckBox;
//...cpp
BOOL MyDialog::OnInitDialog( )
{
if( ! CDialogEx::OnInitDialog( ) )
return FALSE;
//do more stuff then
UpdateState( );
return TRUE;
}
void MyDialog::UpdateState( )
{
m_EditBox.EnableWindow( m_CheckBox.GetCheck( ) == BST_CHECKED );
//more state stuff...
}
void MyDialog::OnBnClickedCheck1( )
{
UpdateState( );
}

Crashing while executing GetParent(). Closing of a modeless dialog box

I am creating a modeless dialog box. The dialog box is called from the menu item of main frame window.
MainFrm.h
CModeless* modeless;
bool modelessDlgOpen;
MainFrm.cpp
void CMainFrame::OnDatabaseMLdlg()
{
// TODO: Add your command handler code here
if (modelessDlgOpen == TRUE)
return;
modelessDlgOpen = TRUE;
modeless = new CModeless(this);
//modeless->Create(IDD_MLDLG, GetDesktopWindow());
modeless->Create(IDD_MLDLG, this);
mbPoll->ShowWindow(SW_SHOW);
}
When menu item is clicked, OnDatabaseMLdlg() function is called and a modeless dialog box with resource ID IDD_MLDLG appears.
The issue is while closing the modeless dialog box.
I am not able to find out the correct method to have a clean closure / destroy of this modeless dialog box. Upon clicking the cross button in right-top corner, which message gets
generated?
My current code which I have tried is as follows. (producing code related only to the closure of the dialog box)
MLDLG.h
#pragma once
#define WM_MLDLG_CLOSED (WM_USER + 555)
// CModeless dialog
class CModeless : public CDialog
{
DECLARE_DYNAMIC(CModeless)
public:
CModeless(CWnd* pParent = NULL); // standard constructor
virtual ~CModeless();
// Dialog Data
enum { IDD = IDD_MLDLG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
virtual BOOL Create(UINT nIDTemplate, CWnd* pParentWnd = NULL);
afx_msg void OnNcDestroy();
virtual void PostNcDestroy();
CWnd* mParent;
afx_msg void OnBnClickedCancel();
};
MLDLG.cpp
void CModeless::OnNcDestroy()
{
CDialog::OnNcDestroy();
// TODO: Add your message handler code here
}
void CModeless::PostNcDestroy()
{
CDialog::PostNcDestroy();
GetParent()->PostMessage(WM_MLDLG_CLOSED,0,0); // **CRASHES HERE**
delete this;
}
void CModeless::OnBnClickedCancel()
{
// TODO: Add your control notification handler code here
//CDialog::OnCancel();
DestroyWindow();
}
Not able to understand what am I doing wrong or what am I missing?
I can provide additional details in case required.
Thanks in advance.
EDIT-20130612: Additional information:
My constructor is as follows:
CModeless::CModeless(CWnd* pParent /*=NULL*/)
: CDialog(CModeless::IDD, pParent)
{
mParent = pParent;
if (mParent == NULL)
{
MessageBox(L"mParent is NULL");
}
else
{
MessageBox(L"mParent is not NULL");
}
}
Here, I have verified that mParent is not NULL.
PostNCDestroy() is called VERY late and most of the useful state of the MFC window is not valid at that point. GetParent() is probably returning NULL, which will cause a crash the way you are using it.
Try moving the PostMessage call to OnDestroy() before calling the base class implementation there.
Another option is to cache the parent's hWnd and call ::PostMessage() on that hWnd;

Selecting content of CEdit control in MFC when clicking the control

How could I select the content of CEdit control just when I click the text of the CEdit.
I could select the content with this code:
m_ctrlEdit.SetFocus();
m_ctrlEdit.SetSel(0, -1, FALSE);
and i put the code in ON_EN_SETFOCUS message handler, but the code doesn't work there.
Create a custom CEdit control and in the custom class add the handler OnLButtonDown in that put the following code
void CMyEdit::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CEdit::OnLButtonDown(nFlags, point);
SetSel(0, -1, FALSE);
}

MFC menu item checkbox behavior

I'm trying to add a menu item such that it acts like a check mark where the user can check/uncheck, and the other classes can see that menu item's check mark status. I received a suggestion of creating a class for the menu option (with a popup option), however, I can't create a class for the menu option when I'm in the resource layout editor in Visual Studio 2005. It would be great to hear suggestions on the easiest way to create menu items that can do what I have described.
You should use the CCmdUI::SetCheck function to add a checkbox to a menu item, via an ON_UPDATE_COMMAND_UI handler function, and the ON_COMMAND handler to change the state of the checkbox. This method works for both for your application's main menu and for any popup menus you might create.
Assuming you have an MDI or SDI MFC application, you should first decide where you want to add the handler functions, for example in the application, main frame, document, or view class. This depends on what the flag will be used for: if it controls application-wide behaviour, put it in the application class; if it controls view-specific behaviour, put it in your view class, etc.
(Also, I'd recommend leaving the menu item's Checked property in the resource editor set to False.)
Here's an example using a view class to control the checkbox state of the ID_MY_COMMAND menu item:
// MyView.h
class CMyView : public CView
{
private:
BOOL m_Flag;
afx_msg void OnMyCommand();
afx_msg void OnUpdateMyCommand(CCmdUI* pCmdUI);
DECLARE_MESSAGE_MAP()
};
// MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
ON_UPDATE_COMMAND_UI(ID_MY_COMMAND, OnUpdateMyCommand)
END_MESSAGE_MAP()
void CMyView::OnMyCommand()
{
m_Flag = !m_Flag; // Toggle the flag
// Use the new flag value.
}
void CMyView::OnUpdateMyCommand(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_Flag);
}
You should ensure the m_Flag member variable is initialised, for example, in the CMyView constructor or OnInitialUpdate function.
I hope this helps!
#ChrisN's approach doesn't quite work for MFC Dialog applications (the pCmdUI->SetCheck(m_Flag); has no effect). Here is a solution for Dialog apps:
// MyView.h
class CMyView : public CView
{
private:
BOOL m_Flag;
CMenu * m_menu;
virtual BOOL OnInitDialog();
afx_msg void OnMyCommand();
DECLARE_MESSAGE_MAP()
};
// MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
END_MESSAGE_MAP()
BOOL CMyView::OnInitDialog()
{
m_menu = GetMenu();
}
void CMyView::OnMyCommand()
{
m_Flag = !m_Flag; // Toggle the flag
if (m_flag) {
m_menu->CheckMenuItem(ID_MENUITEM, MF_CHECKED | MF_BYCOMMAND);
} else {
m_menu->CheckMenuItem(ID_MENUITEM, MF_UNCHECKED | MF_BYCOMMAND);
}
}
References:
http://www.codeguru.com/forum/showthread.php?t=322261
I ended up retrieving the menu from the mainframe using GetMenu() method, and then used that menu object and ID numbers to call CheckMenuItem() with the right flags, as well as GetMenuState() function.

Resources