Solved

How Get Events ?

Posted on 2001-06-27
15
644 Views
Last Modified: 2013-11-20
Hi Experts,
In VB we get the KeyPress , MouseMove Events directly to Edit (Text Box) ctrls. How this will get in MFC programming (VC++) ?
0
Comment
Question by:anij
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6233672
That is also for VC. Using ClassWizard, View->ClassWizard->MFC ClassWizard dialog box appears, and you will find what you need here. Object IDs, Message...
0
 

Author Comment

by:anij
ID: 6233901
But for Edit ctrl there is no KeyPress events
0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6233930
Use EN_CHANGE for KeyPress event
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:peterchen092700
ID: 6234339
...and don't expect MFC to be like VB ;)
0
 
LVL 2

Expert Comment

by:Slordak
ID: 6235094
The premise for Visual C++/MFC event handling is that you define certain handler functions which will handle various events, either at the control level or at the parent level.  The relationship between the event code and the function which is executed can either be established by use of one of MFC's automated wizards, or manually by inserting the correct code.  In some cases, the handler function has a fixed name (determined ahead of time) which you do not customize, and in fact all you have to do is inform the system that you will be providing a handler, rather than having the system use the appropriate default handler (which is called for all events for which you do not provide a handler).

For example, consider if you have a class named CSomeEdit derived from a CEdit control, which is declared as follows in the header file:

class CSomeEdit : public CEdit
{
public:

protected:
  // Generated message map functions
  //{{AFX_MSG(CSomeEdit)
        afx_msg void OnChar(UINT nChar, UINT nRepCnt,
          UINT nFlags);
  //}}AFX_MSG
  DECLARE_MESSAGE_MAP()
};

This is one way of indicating to the system that your class will have a handler function named OnChar().  In
this case, this is the reserved name for handling the
event ON_WM_CHAR.  One knows this because if one looks at the MFC reference, it shows:

ON_WM_CHAR()   afx_msg void OnChar(UINT, UINT, UINT);

  In any case, much of this can be handled automatically for you by the MFC wizard if you prefer.  That's the header section, then in the code file:

BEGIN_MESSAGE_MAP(CSomeEdit, CEdit)
  //{{AFX_MSG_MAP(CSomeEdit)
       ON_WM_CHAR()
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

Thus, now you've indicated that the CSomeEdit class
will handle the ON_WM_CHAR() event (which is posted
when someone enters a character into your edit box).  All you have to do now is implement the OnChar() function to do whatever you want to do when someone enters a character into your derived edit control.

Consult the documentation for further information on the types of events which different windows and controls can generate and which can be handled by program code.
0
 

Author Comment

by:anij
ID: 6236468
Hi Slordak,
You are talking about the derived class of CEdit. How can the same thing is possible for CEdit class, or How to impliment this from the parent class CDialog
plz, help me.
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6236914
>> How to impliment this from the parent class CDialog
The Edit control sends certain notifications (like EN_CHANGE when contents changes) to the parent, where you can add a message handler for this. (usual way)

>> How can the same thing is possible for CEdit class
If you want to add additional message handling for a MFC class (e.g. the notifications are not enough), you need to dereive a class from CEdit and use this instead.

Note that MFC is only a small wrapper around Windows API here, this is roughly the way how windows works.
0
 

Author Comment

by:anij
ID: 6237868
Hi Slordak
After creating the derived class. How to set this class to the resource ctrl ?
I tried to create a variable with this class.
0
 
LVL 2

Accepted Solution

by:
Slordak earned 20 total points
ID: 6238357
You may not want to derive a new class, as Peter Chen has indicated.  In many cases, the default class behavior might be sufficient.  In my case, since I usually wind up heavily customizing the behavior of the control, this is what I do.

To answer your question, though, create the parent dialog as usual from the dialog resource template (IDD_WHATEVER), and have an EDITTEXT control inside that dialog.  In your parent dialog class, declare a protected member variable of the derived control type and override the OnInitDialog function:

class CSomeDialogClass : CDialog
{
public:
  CSomeDialogClass() { Create(IDD_WHATEVER); }
  ~CSomeDialogClass() { return; }

...
  //{AFX_VIRTUAL(CSomeDialogClass)
  virtual BOOL OnInitDialog();
  //}}AFX_VIRTUAL
...

protected:
  CSomeEdit m_myEdit;
...
}

Then in your OnInitDialog routine, subclass the dialog edit control to be of your specific derived type:

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

  m_myEdit.SubclassDlgItem(IDC_SOMEEDIT, this);
}

This informs MFC that the generic edit control should actually be of the derived type that you've specified.  Now when someone presses a key while this edit control has focus, your handler function OnChar() will be called.  This may be overkill for what you're doing, though.  I can post a simpler example without using a new derived edit class and using DDX, perhaps.
0
 
LVL 2

Expert Comment

by:Slordak
ID: 6238479
How about a simpler example?  By the way, much of this can be done through the ClassWizard.  I may be insane in that I often do it by hand.

Let's take the example where you just have a normal CEdit control inside a dialog class which you have created, and you're interested in having the parent dialog be notified when someone changes the contents of the CEdit control.

In the header file:

class CSomeOtherDialog : public CDialog
{
public:
  CSomeOtherDialog() { Create(IDD_MYDIALOG); }
  ~CSomeOtherDialog() { return; }

  //{{AFX_VIRTUAL(CSomeOtherDialog)
  virtual void DoDataExchange(CDataExchange *pDX);
  //}}AFX_VIRTUAL

protected:
  //{{AFX_MSG(CSomeOtherDialog)
  afx_msg void OnEnChange();
  //}}AFX_MSG
 
  CString m_editData;
};

In the code file:

BEGIN_MESSAGE_MAP(CSomeOtherDialog, CDialog)
  //{{AFX_MSG_MAP(CSomeOtherDialog)
  ON_EN_CHANGE(IDC_MYEDITCONTROL, OnEnChange)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CSomeOtherDialog::DoDataExchange(CDataExchange *pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CSomeOtherDialog)
  DDX_Text(pDX, IDC_MYEDITCONTROL, m_editData);
  //}}AFX_DATA_MAP
}

void OnEnChange()
{
  UpdateData(TRUE);

  /* Do whatever you want here... */
}


I think this is probably a better example for what you want to do, as this way you do not have to derive your own edit control class, but instead simply notify MFC that you wish to provide a handler for when the parent dialog receives an ON_EN_CHANGE event from the IDC_MYEDITCONTROL edit control.  In your handler function, you then use DDX to grab the updated data from the edit control and store it in m_editData, and you can then do any other processing which you like at this point.  Hopefully this makes sense.  Again, the AFX message map entries can be generated by using the ClassWizard if you prefer.
0
 

Author Comment

by:anij
ID: 6240054
Hi Slodark,
I am not checked ur code. I will try it.

0
 

Author Comment

by:anij
ID: 6243455
Hi Slodark,
But ur second method doesn't give the current key pressed.
which i need very much.
0
 
LVL 2

Expert Comment

by:Slordak
ID: 6243548
In the second method (where one doesn't derive a new CEdit class), simply check the contents of the CString variable "m_editData" when inside the OnEnChange() function.  The DDX should have loaded the contents of the edit control into this variable in response to the UpdateData() function call.
0
 

Author Comment

by:anij
ID: 6243955
Hi Slodak,

I want to know it is possible to recieve messaged from CEdit ctrl other than EN_ERRSPACE,EN_HSCROLL, EN_... (which is available in class wizards Message List for IDC_EDIT1 or ingeanaral for CEdit ctrl)
I want to get Messages like WM_CHAR,WM_MOUSEOVER etc... of CEdit ctrl.
I know it possible with a dirived class of CEdit. I don't want a dirived class I want it from CEdit itself any way ???
0
 
LVL 2

Expert Comment

by:Slordak
ID: 6248656
In order to do this, you have to overload the behavior of the CEdit class, as far as I know.  As such, you would need to derive a new class, as in my first example, because it's at a slightly lower level than simply having the parent respond to edit-type messages from the child control.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

690 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