Solved

MFC Programming(Edit Box Controls)

Posted on 2001-08-06
17
752 Views
Last Modified: 2013-11-20
I have a dialog based application. The dialog has two edit boxes and one command button. I want the command button to be active only when both of the edit boxes are not empty.(If both edit boxes empty the button is disabled)

I added a subclass to the project that drives from CEdit and are tied to the Edit boxes. I added the following
handle

void CEditText::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
     MessageBox("Key Pressed");
     CEdit::OnChar(nChar, nRepCnt, nFlags);
     
}


When a key is pressed "Key Pressed" is displayed as desired. But I can't now test the other input box to see if is empty.

What is the best approach for a solution.

Thank you.

0
Comment
Question by:jc64
  • 5
  • 3
  • 2
  • +6
17 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6356644
One solution that comes to mind would be to set a timer, say 250mS or so.  

At every tick of the timer check the contents of the two edit boxes.  

If they have a non-zero length string, enable the button.  Otherwise, disable it.
0
 
LVL 2

Expert Comment

by:Lockias
ID: 6356679
You can use either the EN_UPDATE or EN_CHANGE messages to determine when your edit box texts are about to change and have changed.  From msdn:

The EN_CHANGE notification message is sent when the user has taken an action that may have altered text in an edit control. Unlike the EN_UPDATE notification message, this notification message is sent after the system updates the screen. The parent window of the edit control receives this notification message through a WM_COMMAND message.

~Lockias
0
 
LVL 86

Expert Comment

by:jkr
ID: 6357049
I'd send a notification message to the window that holds the controls. It has a pointer to both the edit fields and the button, so it can decide whether to alter the button's state...
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6357227
I'd use EN_CHANGE and handle it in the dialog - the classwizard will let you map this in the dialog without having to subclass your own controls
0
 
LVL 1

Expert Comment

by:kkarunakar
ID: 6358439
Hi there!!
I agree with the people who has suggested using EN_CHANGE method for edit control. It will be simple and easy way to do it. Just you need to add EN_CHANGE event for both the edit control using class wizard or other method. On changing any of the edit control .check the other edit control and see that it is empty . If both are not empty just enable the button. Like
void CXXX::OnEditChange1()
{
CButton *pButton = GetDlgItem(IDC_BUTTON1);
ASSERT(pButton != NULL);
CString csText2;
CString csText1;

CEdit *pEdit1 = GetDlgItem(IDC_EDIT1);
csText1 = pEdit1->GetWindowText();

CEdit *pEdit2 = GetDlgItem(IDC_EDIT2);
csText2 = pEdit2->GetWindowText();

if(!csText1.IsEmpty()&& csText2.IsEmpty())
pButton->EnableWindow(true);


}
void CXXX:OnEditChange2()
{
CButton *pButton = GetDlgItem(IDC_BUTTON1);
ASSERT(pButton != NULL);
CString csText2;
CString csText1;

CEdit *pEdit1 = GetDlgItem(IDC_EDIT1);
csText1 = pEdit1->GetWindowText();

CEdit *pEdit2 = GetDlgItem(IDC_EDIT2);
csText2 = pEdit2->GetWindowText();

if(!csText1.IsEmpty()&& csText2.IsEmpty())
pButton->EnableWindow(true);

}

I guess it will help you.
thanks
Keshav
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6358517
kkarunakar - other people have also proposed the same solution - you have only padded someone elses solution (Lokias) - it is considered bad form to place an answer when people have already comented on the problem
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6358942
Just a comment regarding GUI design:

In my opinion it's not good to disable this button.
Better output a message box when the user clicks the button without filling the editboxes.
If you only disable the button the user will not know what to do to make the button enabled.
0
 
LVL 2

Expert Comment

by:vbk_bgm
ID: 6359795
Handle WM_KICKIDLE (include <afxpriv.h>) for your dialog.

Message map: ON_MESSAGE(WM_KICKIDLE, OnKickIdle)
//Add the code for OnKickIdle handler
void CMyDlg::OnKickIdle(WPARAM , LPARAM)
{
    UpdateDialogControls(this, FALSE);
}

//Add a update command handler for the button in message map
ON_UPDATE_COMMAND_UI(IDC_BUTTON, OnUpdateBtn)

void CMyDlg::OnUpdateBtn(CCmdUI* pCmdUI)
{
   CString str1,str2;
   pEdit1->GetWindowText(str1);
   pEdit2->GetWindowText(str2);

   pCmdUI->Enable(!(str1.IsEmpty() && str2.IsEmpty()));
}
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:kkarunakar
ID: 6360299
Hey ..
I have mentioned that in my answer.But i guess you need some form of code .So thought of putting some code with explanation.
Anyway, if it has helped you it is ok.Else regect the answer.
0
 
LVL 2

Expert Comment

by:Lockias
ID: 6360380
Fine, put some code.  Just post it as a comment.
0
 

Author Comment

by:jc64
ID: 6361058
I think the question is misunderstood. I am aware the existence of EN_UPDATE. I want to make a decision while I am in void CEditText::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) as explained in my original quation.
I have two input boxes and one button on the form. I have also another class CEditText which is subclass of CEdit. The two editboxes are tied to the  CEditText::OnChar of CEditText class. I want when a user presses a key in one of the Editboxes to evaluate the key see if the other EditBox is empty and then enable or disable the command button.

So my problem is how should I access the other controls of on the form when a user presses a key in one of the EdirBoxes and I am transferred to CEditText::OnChar,

Sorry for the misunderstanding.


Please continue to add you best comment and I will only accept the one that answers my question whether someone locks it or not,

Thank you all.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6361225
> I want to make a decision while I am in void CEditText::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) as explained in my original quation.

then in OnChar you will have to goto the parent window (the dialog) and look for the other window (GetDlgItem) becuase you are subclassing the control you will have to have 2 classes each one designed on the OnChar to look for the other one

This is not good design - you would be better of doing the work in the dialog since it knows about all the windows and gets all the relevant information and it will save you code and complication
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6361233
kkarunakar, I have rejected your proposed answer to return this question to the Open Question queue.  Please do not post information that has been previously stated by others as an answer.  This provides additional information in this regard.  

http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp
http://www.experts-exchange.com/jsp/infoMemberAgreement.jsp

As jc64 stated "Please continue to add you best comment and I will only accept the one that answers my question whether someone locks it or not," ... thus comments are preferred.  jc64 can always accept any comment given that best suits the need.

Thank you.
Moondancer
Community Support Moderator @ Experts Exchange
0
 

Author Comment

by:jc64
ID: 6366074
ShaunWilde, may be you have a better solution than I have now but I don't know what it is. I understand doing the work while on the dialog is better and easier but in my sitiation it is not possible as far as know.

Look, I want when someone presses a key in one of the editboxes to test the key and for instance if the key pressed is 'Y' to check if the other editbox is empty and if it is empty to ebale the command button if it is disabled.

You and everyone else agree that EN_UPDATE won't work as it is not cabable of passing the key pressed and other information.
That is why I used the subclass.

The EditBox controls are an array as      
CString     m_EditParam[2];
in the dialog class.
and here is the DoDataExchange
     DDX_Text(pDX, IDC_EDIT1, m_EditParam[0]);
     DDX_Text(pDX, IDC_EDIT2, m_EditParam[1]);

This is hand set and not from the ClassWizard. I mean I used the classWizard to create one and then I turned it to an array.

Now only one OnChar is called when either EditBox is keyed but when that happens control is transfered to the OnChar function which is out of the main Dialog class. But remember, I still need to access controls in the main dialog class. Now the quation is how. That is it.

Thank you.

Now


0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6367648
if you handle EN_CHANGE then you can look at the contents of the edit box and see if there is a Y in it and then enable or disable
0
 

Author Comment

by:jc64
ID: 6368018
ShaunWilde, I appreciate the effort but EN_CHANGE seems not to be able to do the job. How about if the Key is backspace or return key. The 'Y' was just an example I gave. I want to know the key pressed and make a decision.
Inside the EN_CHANGE is there a way I can determine the key pressed.

Thanks
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 100 total points
ID: 6368300
I see - hmmmm - OnChar is the only way to go for that - may be you could record the last key in the CMyEdit::OnChar(...) and access it from the dialog via a member during OnXXXChange
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now