ComboBox Help

Hi. I am making a program that has a combo box in it. It is a static drop-down list. I want to be able to open a certain window when "Test window" is selected in the box. How do I do this. I am programming this using Visual C++ 6.0. It is a MFC so if I could get any help that would be wonderful. Thanks.
[314 byte] By [ctimko] at [2007-12-19]
# 1

There is a selected index changed event on the combo box. Handle this event, and then check the text being displayed with GetWindowText(), if it's Test window', then act accordingly. You could also do it by index, if the index won't change, but if it does for some reason, your code will break later.

cgraus at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 2
Thank you for your reply....is it possible if you could give me a step by step, sort of, explination, because I am really lost. Thanks.
ctimko at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 3

ctimko wrote:
Thank you for your reply....is it possible if you could give me a step by step, sort of, explination, because I am really lost. Thanks.

  1. Add CComboBox m_Combo; to your dialog's header file
  2. In the dialog class's DoDataExchange override, add this : DDX_Control(pDX, IDC_COMBO1, m_Combo);
  3. Add afx_msg void OnComboChanged(); to your dialog header.
  4. In your dialog's BEGIN_MESSAGE_MAP section add the following : ON_CBN_SELCHANGE(IDC_COMBO1, &CDEL_DlgDlg::OnComboChanged)
  5. And in OnComboChanged, use the following to get the newly selected item : CString str; m_Combo.GetLBText(m_Combo.GetCurSel(), str);
  6. Now, do what you want depending on the selected item.
NishantSivakumar at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 4
So, if I understand this correctly, the CString str is the item that is selected in the combobox? Let me know if I am right. Thanks.
ctimko at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 5

ctimko wrote:
So, if I understand this correctly, the CString str is the item that is selected in the combobox? Let me know if I am right. Thanks.

Yes. That's correct.

NishantSivakumar at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 6
So, I would use a if..else statement for that. For instance
if (str=='Test Window\0')
{
TestWindow wnd;
wnd.DoModal();
}

Right? I am rather new to working in the Visual Studio. I normally write in Win32 API, or just command line programs. :-) I hope I got this right.

ctimko

ctimko at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 7

ctimko wrote:
So, I would use a if..else statement for that. For instance
if (str=='Test Window\0')
{
TestWindow wnd;
wnd.DoModal();
}

Right? I am rather new to working in the Visual Studio. I normally write in Win32 API, or just command line programs. :-) I hope I got this right.

ctimko

Yes, CString has an overloaded == operator, so you can do just that.

NishantSivakumar at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 8
Thank you for all the help.. I got it to work because of everyone's help. Thanks.
ctimko at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ Language...