Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CComboBox & GetCurSel();

Status
Not open for further replies.

richsb

Technical User
Aug 2, 2001
34
NO
Hi,

In my HTMLView based app, there is a DialogBar resource template upon which there is a ComboBox control placed on it. In this Combo control exists defined choices. As you may know, no directly assigned variables are able to be created and assigned to the controls as happens in a normal dialog based app, as to get interaction with the controls on the user created toolbar the toolbar is assoiciated with the HTMLView class that is created rather than a class based on CDialog. My problem is that I need to access the selected data as an int. But the only way I am led to believe to get access to the combobox index is via something like this:-

CComboBox* pCBox = (CComboBox*)m_ComboBox.GetCurSel();
//where m_ComboBox is a ComboBox type variable

This compiles okay, but gives an assert failure, and further I am unable to find an answer on how to convert the pointer into something more usable anyway.

Any suggestions as to a solution most welcome
R
 
Further to the above, I have been using the wizard created message CBN_ONSELCHANGE which creates my function as :-

void CTestHTMLView::OnSelchangeComboNav()

It is within this that I am trying to get the index of the selected data. This function does work.

Cheers
 
Yeah it's been a long time since i worked with dialogbar but i remember that the messages will NOT be passed to the MFC Framework as they are with standard resources like menus and toolbars etc. There are articles on MSDN that explain this and provide several options for developing your desired behavior.

Now with all that out of the way… when your trying to get the selected index of the combobox I don’t known how you came up with this code:
Code:
CComboBox* pCBox = (CComboBox*)m_ComboBox.GetCurSel();

It’s just so wrong I don’t know where to begin. CComboBox::GetCurSel() returns an int or long or something like that so how/why are you trying to cast it to a CComboBox pointer? [bugeyed]
Code:
int nCurSel = m_ComboBox.GetCurSel()

Now that will work assuming you have subclassed the control correctly. The subclassing is easily handled using the class wizard in VC6 or from the new IDE "Add Variable" command in VC7.



-pete
 
Hey Pete, thanks for replying.

some further thoughts!

The code :-
CComboBox* pCBox = (CComboBox*)m_ComboBox.GetCurSel();
was my last attempt to try to extracate something from the programme before I went mad and before I resorted to posting here. I got the initial concept from the CTLBARS tutorial and in another form of that it can be used to set the text of the combo box to whatever index number that one cares for on initial execution of the programme, the exact code being:-
void CMainFrame::OnPalette(UINT nID)
{
CComboBox* pCBox = (CComboBox*)m_wndDlgBar.GetDlgItem(IDC_PALETTE);
pCBox->SetCurSel((int)(nID - ID_PALETTE_BASE));
OnSelChangePalette();
}


I am aware that in a normal Dialog app' one would use what you suggested in the form of:-

int nCurSel = m_ComboBox.GetCurSel()

to get the chosen index from a combobox. Off course I tried all these formats and others from my own imagination which came to no avail.
The results all tended to be the same which was an assert failure on programme run even the code compiled okay.

I then had the forethought to see what happens when compiling and running the code in the release version rather than the debug version and hey presto - result... almost.

In the code I had created a messagebox to see the value of selected combobox index, but now all that shows is that the value is always the same which tends to be 0 when using the standard form of code, or some type of hex when using the pointer - the first item. However, as I have just sat here and typed this lot in I have just remembered whether it needs an UpdataData() call?

I'll let you know.
 
Pete,

Further to your comments above, could you expand on whats below:-

"Now that will work assuming you have subclassed the control correctly. The subclassing is easily handled using the class wizard in VC6"

Could you enlighten me as to this process of subclassing using the CW. Yet again I find the MSDN on the subject woefully inadequate.

Cheers
Richard
 
Well i don't have VC6 installed only 7 so this will be from memory.

So you have a control dropped on a dialog resource and a class for the dialog CMyDialog lets say. You start Class Wizard and select your dialog class (CMyDialog) in the classes combobox.

Then go to the "Variables" tab. Find the control in the list of controls. There may be an "Add" or "New" button you have to click. In the "Control Type" combobox select "Control". The other combobox should now show the base class for your control, like CComboBox. Finish the variable name, let's say "_choices" in your case. Now click the correct button to finish creating the variable (Save, Finish, OK… whatever). Now when the Dialog class is a valid window the variable “_choices” is a valid CComboBox object attached to the combobox window on the dialog. Therefore you can use it to access all the member functions like
Code:
_choices.AddString(“Hello”);



-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top