Combo Box access

  • Thread starter Thread starter Manjree Garg
  • Start date Start date
M

Manjree Garg

Hi

I am trying to access a combobox of a dialogbox. The code is someting like
that:


void CDatabaseView::OnAddExpdata()
{
CExpDataDlg dlg;
CComboBox* pCmbBox = static_cast<CComboBox*>

(dlg.GetDlgItem(IDC_COMBOExpId));
}

where IDC_COMBOExpId is a combobox in the dialogbox dlg (CExpDataDlg).

The problem is that it is throwing following exception in CWND::GetDlgItem()

"There is no source code available for the current location."

Any suggestions will be appreciated.

Thanks

Manj.
 
I am trying to access a combobox of a dialogbox. The code is someting like
that:


void CDatabaseView::OnAddExpdata()
{
CExpDataDlg dlg;
CComboBox* pCmbBox = static_cast<CComboBox*>

(dlg.GetDlgItem(IDC_COMBOExpId));
}

Presumably you're a bit lost in what you're doing?

I'd guess that you've been told to add some extra functionality to
someone else's application?

Since the (Windows) dialog has not been created at that point, there
is no combo box to get. Although the dialog class instance exists, a
visible dialog (with child controls) doesn't exist until you've called
DoModal (for a modal dialog) or Create (for a modeless dialog).

Is this an existing dialog box?
Is it a modeless one, or a modal one?
What's the relationship between your view class and the dialog?

Dave
 
Hi Dave

Thanks for your reply. Actually to shorten the code I deleted the lines in
between. Please accept my apologies. The actual code is:

void CDatabaseView::OnAddExpdata()
{

CExpDataDlg dlg;
INT_PTR nRet = -1;
nRet = dlg.DoModal();
do{
if (nRet == IDOK){
CWaitCursor wc;
//Assign values to the parameters
insCmd->Parameters["@dataID"]->Value = gcnew String
(dlg.m_DataID.GetString());

CComboBox* pCmbBox = static_cast<CComboBox*>(dlg.GetDlgItem(IDC_COMBOExpId));
int i = pCmbBox->GetCurSel();
int n = pCmbBox->GetLBTextLen(i);
CString str;
pCmbBox->GetLBText(i,str.GetBuffer(n));
insCmd->Parameters["@expID"]->Value = gcnew String(str.GetString());
int rec = insCmd->ExecuteNonQuery();
}
}while(nRet == IDOK);
}

As I am calling GetDlgItem() after dlg.DoModal() The dilogbox do exist. But
it is still giving the same exception.

I am trying to populate the database (expData table) using the dialogbox
(CExpDataDlg) as instantiated above in the OnAddExpdata() function of the
view class. If I am not using the Combo box it works fine.

Cheers.

Manjree
 
Thanks for your reply. Actually to shorten the code I deleted the lines in
between. Please accept my apologies. The actual code is:

void CDatabaseView::OnAddExpdata()
{

CExpDataDlg dlg;
INT_PTR nRet = -1;
nRet = dlg.DoModal();
do{
if (nRet == IDOK){
CWaitCursor wc;
//Assign values to the parameters
insCmd->Parameters["@dataID"]->Value = gcnew String
(dlg.m_DataID.GetString());

CComboBox* pCmbBox = static_cast<CComboBox*>(dlg.GetDlgItem(IDC_COMBOExpId));

By the time your dialog has exited DoModal, the child controls no
longer exist.

The neatest solution is to use MFCs DDX mechanism to copy the dialog
control data to public member variables of your dialog class - that
way they'll still exist after the dialog has been closed.

Dave
 
Hi Dave

Thanks or the reply. I've tried using the member control variable like:

void CDatabaseView::OnAddExpdata()do{
if (nRet == IDOK){
CWaitCursor wc;
//Assign values to the parameters
insCmd->Parameters["@dataID"]->Value = gcnew String
(dlg.m_DataID.GetString());

CComboBox pCmbBox = static_cast<CComboBox>(dlg.m_expIdCmb);

where m_expIdCmb is the control variable added to the combo box. it is
giving the following error:

error C2248: 'CObject::CObject' : cannot access private member declared in
class 'CObject'


I don't get why the Dialog box is exited at this point? It is still there as
I am entering the values in different fields.

Cheers

Manjree


David Lowndes said:
Thanks for your reply. Actually to shorten the code I deleted the lines in
between. Please accept my apologies. The actual code is:

void CDatabaseView::OnAddExpdata()
{

CExpDataDlg dlg;
INT_PTR nRet = -1;
nRet = dlg.DoModal();
do{
if (nRet == IDOK){
CWaitCursor wc;
//Assign values to the parameters
insCmd->Parameters["@dataID"]->Value = gcnew String
(dlg.m_DataID.GetString());

CComboBox* pCmbBox = static_cast<CComboBox*>(dlg.GetDlgItem(IDC_COMBOExpId));

By the time your dialog has exited DoModal, the child controls no
longer exist.

The neatest solution is to use MFCs DDX mechanism to copy the dialog
control data to public member variables of your dialog class - that
way they'll still exist after the dialog has been closed.

Dave
 
Just to add to my previous reply m_expIdCmb is declared public as follows in
the CExpDataDlg class:
public:
CComboBox m_expIdCmb;



David Lowndes said:
Thanks for your reply. Actually to shorten the code I deleted the lines in
between. Please accept my apologies. The actual code is:

void CDatabaseView::OnAddExpdata()
{

CExpDataDlg dlg;
INT_PTR nRet = -1;
nRet = dlg.DoModal();
do{
if (nRet == IDOK){
CWaitCursor wc;
//Assign values to the parameters
insCmd->Parameters["@dataID"]->Value = gcnew String
(dlg.m_DataID.GetString());

CComboBox* pCmbBox = static_cast<CComboBox*>(dlg.GetDlgItem(IDC_COMBOExpId));

By the time your dialog has exited DoModal, the child controls no
longer exist.

The neatest solution is to use MFCs DDX mechanism to copy the dialog
control data to public member variables of your dialog class - that
way they'll still exist after the dialog has been closed.

Dave
 
Just to add to my previous reply m_expIdCmb is declared public as follows in
the CExpDataDlg class:
public:
CComboBox m_expIdCmb;

Regardless, it won't work.

Although the C++ object wrappers exist after DoModal has returned, the
underlying Windows controls don't - you have to copy any data from the
controls into public data variables (not control variables) so they
persist past the lifetime of the Windows dialog box.

If you're not aware of these issues, have a work through the Scribble
sample - that will teach you the DDX fundamentals.

Dave
 
Thanks Dave. I was in the impression that adding a simple data variable with
combo box won't work though I was using the same method accessing edid box's
values. It worked.

Thanks again.

Manj.
 
CComboBox pCmbBox = static_cast said:
where m_expIdCmb is the control variable added to the combo box. it is
giving the following error:

error C2248: 'CObject::CObject' : cannot access private member declared in
class 'CObject'

You can't copy a CComboBox into a local variable that way, use a pointer or
reference instead.
 
Back
Top