How do I get notification from a `CEdit` box? - windows

I have a CEdit box where a user can enter relevant information. As soon as he\she starts writing in the box, I need a notification so that I can call doSomething() to perform some other task. Does Windows provide a callback, and if so, how do I use it?

With MFC there's no callback as such, rather you do this by implementing a handler for the appropriate event. You need to handle one of two events: WM_CHAR or EN_CHANGE
Handle the dialog's EN_CHANGE for example duplicating in realtime the entered text elsewhere on the dialog. You need to firstly add an entry in the dialog's message map, and secondly override the appropriate handler:
BEGIN_MESSAGE_MAP(CstackmfcDlg, CDialog)
ON_EN_CHANGE(IDC_EDIT1, &CstackmfcDlg::OnEnChangeEdit1)
END_MESSAGE_MAP()
void CstackmfcDlg::OnEnChangeEdit1()
{
CString text;
m_edit.GetWindowText(text);
m_label.SetWindowText(text); // update a label control to match typed text
}
Or, handle the editbox class's WM_CHAR for example preventing input of certain characters, e.g. ignore anything other than a digit for numerical entry. Derive a class from CEdit, handle the WM_CHAR event of that class (not the dialog) and make your edit control an instance of that class.
BEGIN_MESSAGE_MAP(CCtrlEdit, CEdit)
ON_WM_CHAR()
END_MESSAGE_MAP()
void CCtrlEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// Do nothing if not numeric chars entered, otherwise pass to base CEdit class
if ((nChar >= '0' && nChar <= '9') || VK_BACK == nChar)
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
Note that you can use the VS IDE to put in stubs for the handler overrides by using the Properties bar with the mouse selection in the message map block.
EDIT: Added example code, and corrected explanation of WM_CHAR which I had wrong.

If you double click on the edit box in the resource editor it automatically creates the OnEnChanged event for you.

The following assumes that you have an MFC dialog application.
The class wizard can be started with a right-click:
Double-click the Control ID (has an icon with a small green plus) of the new edit control to add the corresponding member variable to the class.
The class and event wizards will update the class definition and add a CEdit member:
afx_msg void OnEnChangeEdit1(); // Added by event wizard
CEdit m_edit1; // member added by class wizard
The class wizard will update the function:
void CMFCApplication5Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, m_edit1); // new variable added with class wizard
}
Double-clicking the control or right-clicking and selecting the add event wizard will update the message map and create the function declaration and definition:
BEGIN_MESSAGE_MAP(CMFCApplication5Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_EDIT1, &CMFCApplication5Dlg::OnEnChangeEdit1) // new event handler added with wizard
END_MESSAGE_MAP()
Finally the code may be updated to interact with the edit control:
void CMFCApplication5Dlg::OnEnChangeEdit1()
{
// TODO: Add your control notification handler code here
CString text;
m_edit1.GetWindowText(text);
//m_edit1.SetWindowText(text);
}

Related

VisualStudio customize paste operation for my component at design-time

I created WinForm component and I want to replace some properties of my component when developer copy and then paste(not when copy but when paste) component from clipboard at design time.
VisualStudio creates new copy of component and assign properties so it became copy of the source component.
I need to replace some properties on paste operation depending on the selected component.
It is very similar to standard Copy/Paste operation with Control component. When designer change Parent of component if developer select other container (like Panel) before Paste Control.
I think that code to perform it should be somewhere in my ComponentDesigner class.
I explored ComponentDesigner methods but can't find any methods that controls clipboard operations.
You can override the OnParentChanged method of your component, which is executed when the component is pasted onto the form. Then test the DesignMode property to make sure you are in design mode:
public class MyComponent : Label
{
protected override void OnParentChanged(EventArgs e)
{
if (DesignMode) {
// Change properties as desired.
Text = "Design";
}
base.OnParentChanged(e);
}
}
When the component is dropped from the Toolbox, this code is not executed. (I can't explain why, but it happens to be exactly what we need.)
If you derived your component from System.ComponentModel.Component, you can override the property Site; however, this will require some more logic to check whether the component has been pasted.
public override ISite Site
{
get {
return base.Site;
}
set {
base.Site = value;
if (value?.Container is IDesignerHost dh &&
dh.TransactionDescription == "Paste components") {
MessageBox.Show("Pasted");
}
}
}
But probably the transaction description is localized, because it is the text that you see in the drop-down of the Undo button on the toolbar of Visual Studio after having pasted the component.

How to Enable Main Items & Button in Application Button in MFC

I'm developing an MFC application dedicated to Windows 7 GUI on visual studio 2013. What I've done so far is I've added Main Item and Button into Application Button by getting some help from http://msdn.microsoft.com/en-us/library/ee354414.aspx as you can see in below image.Now, I want to know that how can I add Event Handler into it.
Sorry, I'm giving answer of my own question.I've done this by adding few line of code show below.In MainFrm.h I've declare two public method
afx_msg void OnMainItemKasim();
afx_msg void OnButtonKasim();
In MainFrm.cpp. Define empty stub of both method
void CMainFrame::OnMainItemKasim()
{
// TODO: Add your command handler code here
}
void CMainFrame::OnButtonKasim()
{
// TODO: Add your command handler code here
}
finally, write following lines in BEGIN_MESSAGE_MAP
ON_COMMAND(ID_FILE_KASIM, &CMainFrame::OnMainItemKasim)
ON_COMMAND(ID_APP_KASIM, &CMainFrame::OnButtonKasim)
where, ID_FILE_KASIM is Main Item below Save As & ID_APP_KASIM is button next to Exit button
finally it looks like this

How to make an auto hiding menu bar with Qt4

I am trying to make a Qt application which has an auto hiding menu bar. How can i do that?
That's an interesting task ! Ok, lets see... I'd suggest you putting a code that keeps track of mouse cursor movement in QMainWindow::centralWidget(). You need to call QWidget::setMouseTracking(true) first to be able to keep track of your mouse movement (they are turned off by default). The code can look like this:
QMainWindow *mainWindow = new QMainWindow;
MyWidget * myWidget = new MyWidget(mainWindow);
myWidget->setMouseTracking(true);
mainWindow->setCentralWidget(myWidget);
And then in your widget QWidget::mouseMove() event you need to detect whether you are in the correct area. The code can look like this:
void MyWidget::mouseMoveEvent(QMouseEvent * event) {
bool menuVisible = inCorrectArea(event->pos());
mainWindow->menuBar()->setVisible(menuVisible);
...
}
There are several ways to get access to "mainWindow" in your MyWidget. One of them is to store a pointer in MyWidget private variable when you pass MainWindow in its MyWidget constructor. You can also issue a signal from your MyWidget and handle it in MainWindow.
Hope this helps.

Let my MFC dialog receive keystroke events before its controls (MFC/Win32 equivalent of WinForms "KeyPreview")

I have an MFC dialog containing a dozen or so buttons, radio buttons and readonly edit controls.
I'd like to know when the user hits Ctrl+V in that dialog, regardless of which control has the focus.
If this were C#, I could set the KeyPreview proprety and my form would receive all the keystrokes before the individual controls - but how do I do that in my MFC dialog?
JTeagle is right. You should override PreTranslateMessage().
// Example
BOOL CDlgFoo::PreTranslateMessage( MSG* pMsg )
{
// Add your specialized code here and/or call the base class
if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN )
{
int idCtrl= this->GetFocus()->GetDlgCtrlID();
if ( idCtrl == IDC_MY_EDIT ) {
// do something <--------------------
return TRUE; // eat the message
}
}
return CDialog::PreTranslateMessage( pMsg );
}
Add a handler to override PreTranslateMessage() in the dialog class, and check the details of the MSG struct received there. Be sure to call the base class to get the right return value, unless you want to eat the keystroke to prevent it going further.

Key Preview and Accept Button

Using winforms, I have set the KeyPreview property to true and have event handles for the proper key events within the base form as well.
Within the forms that inherit from it, I set the AcceptButton property based on the requirements of the application.
There are certain cases in which I want the enter key to have functionality different than that of the AcceptButton.
I was hoping to capture the enter key press within my base form and check for the special cases where I do not want the AcceptButton event to fire.
It appears though, that the AcceptButton click is fired before any of the key events within my basef form. I could write functionality into the click events of the possible acceptbuttons, but, in my opinion, that would be a hack.
Any suggestions?
Thanks.
Another way to handle this is to override the form's ProcessDialogKey() method where you can suppress the accept and/or cancel buttons. For example, I have an application with a filter editor that filters a grid based on user input. I want the user to be able to hit the return key when the filter editor control has the focus to apply the filter. The problem is the accept button code runs and closes the form. The code below resolves the issue.
protected override bool ProcessDialogKey(Keys keyData)
{
// Suppress the accept button when the filter editor has the focus.
// This doesn't work in the KeyDown or KeyPress events.
if (((keyData & Keys.Return) == Keys.Return) && (filterEditor.ContainsFocus))
return false;
return base.ProcessDialogKey(keyData);
}
You can take this even further by dropping the following code in a base dialog form. Then you can suppress the accept button for controls in subclasses as necessary.
private readonly List<Control> _disableAcceptButtonList = new List<Control>();
protected override bool ProcessDialogKey(Keys keyData)
{
if (((keyData & Keys.Return) == Keys.Return) && (_disableAcceptButtonList.Count > 0))
{
foreach (Control control in _disableAcceptButtonList)
if (control.ContainsFocus)
return false;
}
return base.ProcessDialogKey(keyData);
}
protected virtual void DisableAcceptButtonForControl(Control control)
{
if (!_disableAcceptButtonList.Contains(control))
_disableAcceptButtonList.Add(control);
}
As our workaround, we captured the enter and leave event for the control that we wanted to have override the acceptbutton functionality. Inside the enter event, we held the current accept button in a private variable and set the acceptbutton to null. On leave, we would reassign the acceptbutton back to the private variable we were holding.
The KeyPreview events could have done something similar to the above. If anyone has a more elegant solution, I would still love to know.
Thanks.

Resources