Can't get text info to member variable in form CDialog MFC dialog form - visual-c++

I have small dialog form with Edit Text control:
#include "stdafx.h"
#include "MyDlg3.h"
#include "afxdialogex.h"
// MyDlg3 dialog
IMPLEMENT_DYNAMIC(MyDlg3, CDialog)
MyDlg3::MyDlg3(CWnd* pParent /*=NULL*/)
: CDialog(MyDlg3::IDD, pParent)
, m_edit(_T(""))
{
}
MyDlg3::~MyDlg3()
{
}
void MyDlg3::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_edit);
}
BEGIN_MESSAGE_MAP(MyDlg3, CDialog)
ON_BN_CLICKED(IDOK, &MyDlg3::OnBnClickedOk)
END_MESSAGE_MAP()
// MyDlg3 message handlers
void MyDlg3::OnBnClickedOk()
{
txt=m_edit;
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
And I have simple application that calls this dialog:
BOOL CPreparationApp::InitInstance()
{
MyDlg3 Dlg3;
Dlg3.DoModal();
CString strLine0=Dlg3.txt;
return true;
}
I cant't find why I can't get text that was entered in dialogs Text Control to txt and strLine0 variables.
I found that if I make MyDlg3 from CDialogEx (not from CDialog like it is now) - everything goes fine. Where is problem?

The text is transferred between the control and the member variable in DoDataExchange. DoDataExchange is called when you do CDialog::OnOK as you can see in the documentation for UpdateData. By copying the value before then you're not getting any results. You could fix this by moving the copy after the call to OnOK, or you could skip the copy altogether as suggested by another answer and use the member variable directly.

It's the call to CDialog::OnOK() in MyDlg3::OnBnClickedOk() that sets up the member variable m_edit, so you can write logic like
MyDlg3 Dlg3;
Dlg3.DoModal();
CString strLine0 = Dlg3.m_edit;
while making sure that m_edit is declared as public.

Try this:
BOOL CPreparationApp::InitInstance()
{
MyDlg3 Dlg3;
if(Dlg3.DoModal()==IDOK);
{
CString strLine0=Dlg3.txt;
return true;
}
return false;
}

Related

C++ CLI Invoke issues

I have a MainForm class (as you'd expect, it is a form) that has a text box on it. I also have another class called 'Application_Server' That does a load of other stuff (not just form-background related, quite a lot of network based stuff etc.).
The Application_Server class runs in it's own thread, but needs to be able to update the controls on the form, for this question, we will stick with just the textbox.
The problem is that even though I am executing the command to set the text of the textBox control via 'Invoke' I am still getting the following exception during runtime:
Additional information: Cross-thread operation not valid: Control
'DebugTextBox' accessed from a thread other than the thread it was
created on.
What could be causing this? I am definitely invoking a delegate within MainForm.
Here are the relevant code segments (cut down for readability):
MainForm.h:
public ref class MainForm : public System::Windows::Forms::Form {
delegate void del_updateDebugText(String^ msg);
del_updateDebugText^ updateDebugText = gcnew del_updateDebugText(this, &MainForm::postDebugMessage);
private: void postDebugMessage(String^ message);
};
MainForm.cpp:
void EagleEye_Server::MainForm::postDebugMessage(String^ message)
{
Monitor::Enter(DebugTextBox);
if (this->DebugTextBox->InvokeRequired)
{
this->Invoke(updateDebugText, gcnew array<Object^> { message });
}
else
{
this->DebugTextBox->AppendText(message);
}
Monitor::Exit(DebugTextBox);
}
And finally, the code calling it:
void ServerAppManager::postDebugMessage(System::String^ message)
{
mainFormHandle->updateDebugText(message);
}
void ServerAppManager::applicationStep()
{
postDebugMessage("Starting\n");
// This is Run in seperate thread in MainForm.cpp
while (s_appState == ApplicationState::RUN)
{
postDebugMessage("Testing\n");
}
}
Thanks!
From background worker called bwSearch we do the call as following from the DoWork event handler:
private: System::Void bwSearch_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
//... logic
UpdateTxtOutput("Some message");
//... more logic
}
I have a RitchTextBox called txtOutput, also the windows form control containing this code is called frmMain, the UpdateTxtOutput is defined in three parts as follows:
delegate void UpdateTxtOutputDelegate(String^ text);
void UpdateTxtOutput(String^ text)
{
UpdateTxtOutputDelegate^ action = gcnew UpdateTxtOutputDelegate(this, &frmMain::Worker);
this->BeginInvoke(action, text);
}
void Worker(String^ text)
{
txtOutput->AppendText("\t" + text + "\n");
}
I managed to get it working by simplifying the method within the 'MainForm' class to:
void EagleEye_Server::MainForm::postDebugMessage(String^ message)
{
Monitor::Enter(DebugTextBox);
DebugTextBox->AppendText(message);
Monitor::Exit(DebugTextBox);
}
And then moving the 'Invoke' call to the method calling the delegate, not pretty but it works for now. I think the issue may have been caused by the form getting stuck inside an Invoke loop. I say this as I noticed that the form would lock up and stop responding after it hit the recursive Invoke statement.

MFC on form create event

I have simple FMC dialog form. Can't find how to manage event that should be called on form create moment. Something like onFormCreate like it is in VB or Delphi.
How to create such functionality?
My simple form header:
#pragma once
// CMFCApplicationUPTDlg dialog
class CMFCApplicationUPTDlg : public CDialog
{
// Construction
public:
CMFCApplicationUPTDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_MFCAPPLICATIONUPT_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// 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()
public:
afx_msg void OnBnClickedOk();
afx_msg void OnBnClickedButton1();
};
You want to handle it in the OnInitDialog() member function. At the point that OnInitDialog() is called, the dialog and all of its child windows will have been created. However, the dialog will still be invisible to the user. After exiting OnInitDialog(), the dialog will be shown to the user.
So, it is the place where you would want to do initialization of your dialog.
You need to implement OnInitDialog in your own derived class.
BOOL CMFCApplicationUPTDlg::OnInitDialog()
{
// this will create the controls you defined in the resources.
BOOL retValue = __super::OnInitDialog();
// initialize (or add new controls to ) your dialog content here.
// ...
return retValue;
}

Set MFC dialog form caption

I need to set dialog form caption. I was trying to create CString variable that might be binded to caption with Class Wizard. But there is no main form control in selection menu. What is the way of doing that?
This is my dialog form:
#include "stdafx.h"
#include "MyDlg.h"
#include "afxdialogex.h"
// MyDlg dialog
IMPLEMENT_DYNAMIC(MyDlg, CDialog)
MyDlg::MyDlg(CWnd* pParent /*=NULL*/)
: CDialog(MyDlg::IDD, pParent)
, m_edit(_T(""))
{
}
MyDlg::~MyDlg()
{
}
void MyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_edit);
}
BEGIN_MESSAGE_MAP(MyDlg, CDialog)
ON_BN_CLICKED(IDOK, &MyDlg::OnBnClickedOk)
END_MESSAGE_MAP()
// MyDlg message handlers
void MyDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
txt=m_edit;
}
This is code that creates dialog:
BOOL CPreparationApp::InitInstance()
{
MyDlg Dlg;
//how to tell Dlg to have form caption "BLABLABLA"?
Dlg.DoModal();
return TRUE;
}
Hoping I've understood your question in the right way:
// MyDlg.h
class MyDlg
{
public: // private is fine too if you're OOP nazi but you have to provide a SetDlgCaption method then.
CString m_strDlgCaption;
};
// MyDlg.cpp
BOOL MyDlg::OnInitDialog( )
{
SetWindowText( m_strDlgCaption );
}
BOOL CPreparationApp::InitInstance()
{
MyDlg Dlg;
Dlg.m_strDlgCaption = _T("A fancy caption for your dialog");
Dlg.DoModal();
}
If you have not already done it you first need to add an override in the dialog class for OnInitDialog. This is the first place you can execute code after the dialog and its control windows exist. You can put SetWindowText(_T("BLABLABLA")) in OnInitDialog to set the dialog caption.

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;

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

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.

Resources