11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5. Common Controls<br />

}<br />

ptrEdit->SetSel(0, -1);<br />

nSize=ptrCombo->GetCount();<br />

bHit=FALSE;<br />

for(i=0; iGetLBText(i, szStrLB);<br />

if(szStrLB == szStr)<br />

{<br />

bHit=TRUE;<br />

break;<br />

}<br />

}<br />

}<br />

if(bHit == FALSE)<br />

{<br />

ptrCombo->AddString(szStr);<br />

if(wParam == IDC_COMBO_DROPDOWN)<br />

{<br />

GetDlgItem(IDC_STATIC_DROPDOWN)->SetWindowText(szStr);<br />

}<br />

if(wParam == IDC_COMBO_SIMPLE)<br />

{<br />

GetDlgItem(IDC_STATIC_SIMPLE)->SetWindowText(szStr);<br />

}<br />

}<br />

return (LRESULT)TRUE;<br />

In this message handler, we first obtain a pointer to the combo box using the ID passed through WPARAP<br />

message parameter. Then we use above-mentioned method to get the pointer to the edit box (a child<br />

window of combo box), and assign it to variable ptrEdit. Then we use this pointer to call function CWnd::<br />

GetWindowText(…) to retrieve the text contained in the edit box window. If the edit box is not empty (this is<br />

checked by calling function CString::IsEmpty()), we select all the text in the edit box by calling function<br />

CEdit::SetSel(…), which has the following format:<br />

void CEdit::SetSel(int nStartChar, int nEndChar, BOOL bNoScroll = FALSE);<br />

The first two parameters of this function allow us to specify a range indicating which characters are to<br />

be selected. If we pass 0 to nStartChar and -1 to nEndChar, all the characters in the edit box will be<br />

selected. Then we use a loop to check if the text contained in the edit box is identical to any item string in<br />

the list box. In case there is no hit, we will add this string to the list box by calling function<br />

CComboBox::AddString(…). Finally, a TRUE value is returned before this function exits.<br />

Using this method, we can also trap other keystrokes such as DELETE, ESC to the combo box. This<br />

will make the application easier to use.<br />

5.10 Implementing Subclass for the Edit Box of a Combo Box<br />

Under certain conditions we may want to put restrictions on the contents of the list items. For example,<br />

sometimes we may want the combo box to hold only numerical characters (‘0’-‘9’), and sometimes we may<br />

expect it to hold only alphabetical characters (‘a’-‘z’, ‘A’-‘Z’). In these cases, we may want to customize<br />

the properties of the edit box so that only a special set of characters can be accepted. If we are creating an<br />

edit box resource in dialog template, this can be easily achieved by setting its customizable styles. But for<br />

the edit box of a combo box, we can not customize its styles before it is created, so the edit box contained<br />

in a combo box will have only the default styles.<br />

To customize the behavior of the edit box in a combo box, we need to use “subclass” technique. We<br />

can design our own class to intercept and process the messages sent to the edit box. Sample 5.10\CCtl<br />

demonstrates how to customize the edit box that belongs to a combo box. It is based on sample 5.9\CCtl,<br />

with two combo boxes customized as follows: combo box IDC_COMBO_SIMPLE allows only numerical<br />

characters to be input into the edit box; combo box IDC_COMBO_DROPDOWN accepts only alphabetic<br />

characters.<br />

111

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!