Click to See Complete Forum and Search --> : Want ComboBox text to change with Edit box text


Sangeeta
May 25th, 1999, 11:57 PM
I have a dialog box with a combobox and an edit box. What I am trying to do is taht when i change the text in Edit Box , the text of the selected item in Combobox should change simultaneously.For this I have written code fro EN_CHANGE on edit box

void CDlg::OnChangeEdit1()
{
// TODO: Add your control notification handler code here
m_cedit1.GetWindowText(m_vedit1);
m_ccombo1.SetWindowText(m_vedit1);
}

where m_cedit1 and m_ccombo1 are Control strings for the edit and combo box resp. m_vedit1 is value string for edit box. Now what I want is that next time I open thsi dialog box then the value changed in previous session should be in the drop down list of the combobox.

Jason Teagle
May 26th, 1999, 01:45 AM
In order to be able to remember the strings that need to be displayed in the combo box, you will need to store them in an array which is part of the app (or the window which calls the dialogue box), a CStringArray for example (if you want them remembered even after closing the whole program, you are going to need to store them to disk - Serialize(), for example).

Then, whenever you display the dialogue box, add strings to the combo from the CStringArray.

Does this help?

Sangeeta
May 26th, 1999, 04:22 AM
Where should I declare this array ?? in Mainfem.h or <Application Name>.h
Actually I am very new to VC++ so this question may sound stupid :)

Jason Teagle
May 26th, 1999, 04:50 AM
No, it is not a stupid question. Where you store things is largely up to you, but I recommend putting the array as a member of the main frame, or the window which opens the dialogue box, if they are not the same.

Sangeeta
May 26th, 1999, 05:28 AM
I have just done the same. In MainFrm.h ,I have declared
public:
CStringArray m_array;
int nIndex;

and then in Dlg.cpp (the source file for the dialogbox that contains teh combobox), I have done

#include "MainFrm.h"

Just for testing I am try to store the value contained in the edit box to the array when user presses OK on the dialog box.

void CDlg::OnOK()
{
// TODO: Add extra validation here
m_array.Add(m_vedit1);
CDialog::OnOK();
}

I try to compile it and gives following error
error C2065: 'm_array' : undeclared identifier
error C2227: left of 'Add' must point to class/struct/union

What is wrong ???

I ma using VC++4.2

Regards,
Sangeeta

\

Gaetan Frenoy
May 26th, 1999, 05:46 AM
Easy to correct :
m_array is not a member variable of the CDlg class.

You should try to get the info stored in the dialog after the DoModal() call in your mainframe window.
Something like :

CMainFrame::OnLaunchDialog()
{
CDlg dlg;
if (dlg.DoModal() == IDOK)
{
m_array.Add(m_vedit1);
}
}




Regards,


--
Gaetan Frenoy (aka Gaff)

Jason Teagle
May 26th, 1999, 06:03 AM
The problem is that you have declared m_array as a member of your CMainFrame, but you are trying to access it directly from the dialogue class. m_array is not a member variable of CDlg, so the compiler doesn't know what it means.

What you need to do is add four little methods to your CMainFrame class, as follows:

---int CMainFrame::Add(CString strText)
{
return m_array.Add(strText);
}

int CMainFrame::GetSize(void)
{
return m_array.GetSize();
}

CString CMainFrame::GetAt(int iIndex)
{
CString strText = _T("");

if (iIndex < GetSize() )
strText = m_array.GetAt(iIndex);

return strText ;
}

void CMainFrame::RemoveAll(void)
{
m_array.RemoveAll();
}




---

This allows your CMainFrame class to simulate being an array. From your CDlg class you now need to do the following:

---void CDlg::OnOK()
{
CMainFrame *pWndMain ;

// TODO: Add extra validation here

pWndMain = (CMainFrame *)AfxGetMainWnd();
if (pWndMain != NULL)
pWndMain->Add(m_vedit1);

CDialog::OnOK();
}




---

You will have to make sure that you #include "MainFrm.h" for this code file now.

Whenever you want to manipulate the array, get hold of the main frame's pointer as above and call the same function you would call directly on a CxxxArray.

Does that help?

Sangeeta
May 26th, 1999, 06:13 AM
I did the following

void CDlg::OnOK()
{
// TODO: Add extra validation here
CMainFrame* mainfrm;
mainfrm->OnLaunchDialog();
CDialog::OnOK();
}




void CMainFrame::OnLaunchDialog()
{
CDlg dlg;
if (dlg.DoModal() == IDOK)
{
m_array.Add(m_vedit1);
}
}


It gives this error on compiling
error C2065: 'm_vedit' : undeclared identifier

Sangeeta
May 26th, 1999, 06:40 AM
Thanks a lot. Its' working fine.
One more thing I want to ask u. Are there any sample projects available on the web for the beginners.

Regards
Sangeeta

Jason Teagle
May 26th, 1999, 06:57 AM
Same problem - m_vedit1 is a member of CDlg, not CMainFrame. If you want to do it like this, you would add a method to CDlg, like this:

---CString CDlg::GetStringToAdd(void)
{
return m_vEdit1 ;
}




---

Now, after launching the dialogue box, your main frame can call dlg.GetStringToAdd() to retrieve the string it needs to add to the array (and then call m_array.Add(), of course).

The only drawback with this method is if you want to be able to add more than one string to the list from the same dialogue box. If you are only able to retrieve one string (the latest one), you can't add them all to the list.

If it is possible to make the dialogue box as independent as possible (i.e., not calling the main frame routines), this is better design; but because you want to add the list of items to the combo box within the dialogue box when you next show it, you are still going to have to store an array outside the dialogue box. You might as well call the main frame from the dialogue.

Jason Teagle
May 26th, 1999, 07:00 AM
I'm sure there are many code samples out there to help you, but I'm afraid I don't know any off the top of my head. Your best bet is to resort to a search engine. There are always people here who are willing to help you, though.

Sangeeta
May 26th, 1999, 11:42 PM
What I am trying to do now is that when user presses OK on dialog box then I save the contents of the edit box to array. When next time user opens the dialog box I ma trying to populate the combobox with the contents of array as follows

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

// TODO: Add extra initialization here
CComboBox* c;
c=(CComboBox*)GetDlgItem(IDC_COMBO2);

CMainFrame *pWndMain ;
pWndMain = (CMainFrame *)AfxGetMainWnd();
if (pWndMain != NULL)
n=pWndMain->GetUpperBound();
m=n;
if (n != -1)
{
CString temp=pWndMain->GetAt(n);
c->AddString(temp);
//c->InsertString(n,temp);
}

void CDlg::OnOK()
{
// TODO: Add extra validation here
CMainFrame *pWndMain ;
pWndMain = (CMainFrame *)AfxGetMainWnd();
if (pWndMain != NULL)
pWndMain->Add((LPCTSTR)m_vedit);
CDialog::OnOK();
}


If I use c->AddString(temp); in the OnInitDialog() then only the last entry(made to edit box in previous session) is shown in the list box.

If I use c->InsertString(n,temp); in the OnInitDialog() then second time when I open dialog box, it shows entry made in previous session. But when I open the dialog box for third, fourth and subsequent times, nothing is shown in the combobox-list.

I fail to understand what is happening. Please help..

Regards
Sangeeta

Jason Teagle
May 27th, 1999, 01:54 AM
You're almost there. (By the way, did you change the function name from GetSize() to GetUpperBound()? The function name I quoted previously was GetSize(). The main frame only supports SOME CxxxArray methods because we deliberately created the routines to imitate them).

1) The 'if (pWndMain != NULL)' should enclose ALL statements that refer to getting strings from the main frame, so you should have an open brace ('{') immediately after the above line, and the closing brace should be after the last call to AddString() on the combo box. Also, when you perform an 'if' that may or may not set the value of a variable, if you are going to use that variable afterwards regardless of the previous 'if' test then you should make sure you have initialised the variable before the test, otherwise it will contain garbage. I.e.:

--- int n = -1 ; // This guarantees that if the 'if' below
// fails, n still has a valid value in it
// for use afterwards.

if (pWndMain != NULL)
n = pWndMain->GetUpperBound();

// Now use n...




---

2) When you have got the size of the array stored in the main frame, you then need to loop through ALL of the items, like this:

--- .
.
.
if (pWndMain != NULL)
{
iSize = pWndMain->GetSize();
for (n = 0 ; n < iSize ; n++)
{
strTemp = pWndMain->GetAt(n);
c->AddString(strTemp);
}
}
.
.
.




---

This will then add ALL strings entered on previous runs to the combo box. By only adding the result of GetUpperBound(), you were only adding the LAST string stored.

By the way, you do not need to cast m_vedit to LPCTSTR - CStringArray::Add() accepts a CString.

Sangeeta
May 27th, 1999, 02:13 AM
Thanks I got it working.

Regards
Sangeeta