Download presentation
Presentation is loading. Please wait.
Published byBernard Collins Modified over 8 years ago
1
Chapter 7 Controls
2
List box control
3
3 List Box Control(1/8) Listbox control: –Display lists of text strings called items –Optionally sort the items and have scroll bar Properties Single Selection List box Multiple Selection List box
4
CListBox Class CListBox class encapsulates list box controls Creating a List Box –Use CListBox::Create member function m_ListBox.Create (WS_CHILD ¦ WS_VISIBLE ¦ LBS_STANDARD, rect, this, ID); m_ListBox.Create (WS_CHILD ¦ WS_VISIBLE ¦ LBS_STANDARD, rect, this, ID);
5
List Box Styles StyleDescription LBS_STANDARDCreates a "standard" list box that has a border and a vertical scroll bar, notifies its parent window when th e selection changes or an item is double-clicked, and sorts items alphabetically. LBS_SORTSorts items that are added to the list box. LBS_NOSELCreates a list box whose items can be viewed but not selected. LBS_NOTIFYCreates a list box that notifies its parent when the selection changes or an item is double-clicked. LBS_DISABLENOSCROL L Disables the list box's scroll bar when it isn't needed. Without this style, an unneeded scroll bar is hidden r ather than disabled. LBS_MULTIPLESELCreates a multiple-selection list box. LBS_EXTENDEDSELAdds extended selection support to a multiple-selection list box. LBS_MULTICOLUMNCreates a multicolumn list box. LBS_OWNERDRAWVARI ABLE Creates an owner-draw list box whose items can vary in height. LBS_OWNERDRAWFIXE D Creates an owner-draw list box whose items are the same height. LBS_USETABSTOPSConfigures the list box to expand tab characters in item text. LBS_NOREDRAWCreates a list box that doesn't automatically redraw itself when an item is added or removed. LBS_HASSTRINGSCreates a list box that "remembers" the strings added to it. Conventional list boxes have this style by defa ult; owner-draw list boxes don't. LBS_WANTKEYBOARDI NPUT Creates a list box that sends its parent a WM_VKEYTOITEM or WM_CHARTOITEM message when a ke y is pressed. This style is used to customize the list box's response to keyboard input. LBS_NOINTEGRALHEIG HT Allows a list box to assume any height. By default, Windows sets a list box's height to a multiple of the ite m height to prevent items from being partially clipped.
6
6 List Box Notifications –LBN_DBLCLK, LBN_SELCHANGE and LBN_SELCANCEL messages require LBS_NOTIFY style setting NotificationSent WhenMessage-Map MacroLBS_NOTIFY Required? LBN_SETFOCUSThe list box gains the input foc us. ON_LBN_SETFOCUSNo LBN_KILLFOCUSThe list box loses the input foc us. ON_LBN_KILLFOCUSNo LBN_ERRSPACEAn operation failed because of insufficient memory. ON_LBN_ERRSPACENo LBN_DBLCLKAn item is double-clicked.ON_LBN_DBLCLKYes LBN_SELCHANG E The selection changes.ON_LBN_SELCHANGEYes LBN_SELCANCELThe selection is canceled.ON_LBN_SELCANCELYes
7
7 Editing items Add and Delete Items Select items m_list.AddString(_T(“apple”)); m_list.DeleteString(3); // For a single selection list box m_list.SetCurSel(2); // For a multiple selection list box m_list.SetSel(2); m_list.SetSel(3, FALSE);
8
8 Get selected items // For a single selection list box int nIndex = m_list.GetCurSel(); if(nIndex != LB_ERR){ CString str; m_list.GetText(nIndex, str); } // For a multiple selection list box int nIndex = m_list.GetCaretIndex(); if(nIndex != LB_ERR){ CString str; m_list.GetText(nIndex, str); }
9
Coding Practice Add buttons, edit box and List box to a dialog application Add and delete an item in the list box using button controls
10
Chapter 8 Dialog Boxes and Property Sheet
11
11 Dialog Box ? Dialog box –Window that pops up to solicit input from the user –Ex) file menu, print menu
12
12 Dialog based application: Dialog box itself is the main program (frame)
13
13 Today’s topic 1. How can design a dialog box 2. How to show or close any dialog box when necessary 3. How to give or take any value to or from the dialog box
14
14 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables the window until the dialog box is dismissed. –Modeless dialog It behaves more like a conventional window. It activates together with other windows.
15
15 How to create dialog box Design a dialog template by using dialog box edit tool in the resource view Edit resource file by your self. –Edit Resource script(*.RC) manually
16
16 A dialog box template Using a resource view to design a new dialog box
17
17 A dialog box template Add or delete controls –Using Toolbox menu
18
18 A dialog box template Align templates –Use dialog editor tool bar –Or use Format menu
19
19 A dialog box Template Tab order –Determine the order of the focus changing when tab key is pressed –Use [Format]->[Tab Order] menu
20
20 Two kinds of dialog boxes Dialog boxes –Modal dialog When appear, it takes all ownership of input. It disables the window until the dialog box is dismissed. –Modeless dialog It behaves more like a conventional window. It activates together with other windows.
21
21 Modal Dialog box MFC Class Heirarchy
22
22 Modal dialog box How to create and show it ① Design a dialog box template Resource View ② Create a CDialog derived class using the template Use [Project] [add class] menu ③ Call CDialog::DoModal() function to show the dialog box
23
23 Modal dialog box Main Virtual functions of CDialog class –WM_INITDIALOG message handler –When initializes the dialog box –Good place for initializing other controls –IDOK message handler (when pressing OK button) –Good place for updating variables before closing the dialog box, virtual BOOL CDialog::OnInitDialog ( ); virtual void CDialog::OnOK ( );
24
24 Modal dialog box Main Virtual functions of CDialog class –IDCANCEL message handler (when pressing cancel button) –Close the dialog box virtual void CDialog::OnCancel ( );
25
25 OnOK() and OnCancel() function Call EndDialog function to close the dialog box void CDialog::OnOK() { UpdateData(TRUE);// update the variables EndDialog(IDOK); } void CDialog::OnCancel() { EndDialog(IDCANCEL); }
26
26 DDX/DDV (1/8) How to connect your variables in the parent window with a dialog box: –Add the same variables and connect them to the controls Ex) create two variables and change them using a dialog box class CMyDialog : public CDialog {... CString m_str ; int m_color ;... }
27
27 DDX/DDV (2/8) What you have to: IDC_STR IDC_COLOR ①② Dialog box m_str m_color Dialog variables m_str m_color Parent variables IDC_STR IDC_COLOR ③ ④ Dialog box m_str m_color Dialog variables m_str m_color Parent variables When showing dialog box When pressing OK button
28
28 DDX/DDV (3/8) How to implement it: BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); SetDlgItemText(IDC_STR, m_str); SetDlgItemInt(IDC_COLOR, m_color); return TRUE; } void CMyDialog::OnOK() { GetDlgItemText(IDC_STR, m_str); m_color = GetDlgItemInt(IDC_COLOR); CDialog::OnOK(); }
29
29 DDX/DDV (4/8) An automatic way: –DDX(Dialog Data eXchange) IDC_STR IDC_COLOR ①② 대화상자 m_str m_color 대화상자 객체 m_str m_color 뷰 객체 IDC_STR IDC_COLOR ③ ④ 대화상자 m_str m_color 대화상자 객체 m_str m_color 뷰 객체 Automation?
30
30 DDX/DDV (5/8) OnInitDialog(), OnOK() implementation BOOL CDialog::OnInitDialog() {... UpdateData(FALSE);// Give the values to the controls... } void CDialog::OnOK() {... UpdateData(TRUE);// Retrieve the values // from the controls... }
31
31 DDX/DDV (6/8) What is CWnd::UpdateData() function? BOOL CWnd::UpdateData(BOOL bSaveAndValidate) {... CDataExchange dx(this, bSaveAndValidate); DoDataExchange(&dx);... }
32
32 DDX/DDV (7/8) Implementation of DDX –Connecting a variable with a control Use DDX_* MACRO void CMyDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMyDialog) DDX_Text(pDX, IDC_STR, m_str); DDX_Text(pDX, IDC_COLOR, m_color); //}}AFX_DATA_MAP }
33
33 DDX/DDV (8/8) DDV(Dialog Data Validation) –Automation of the validation of the data Check the range of the data Use DDV_* MACRO void CMyDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMyDialog) DDX_Text(pDX, IDC_STR, m_str); DDV_MaxChars(pDX, m_str, 10); DDX_Text(pDX, IDC_COLOR, m_color); DDV_MinMaxInt(pDX, m_color, 0, 255); //}}AFX_DATA_MAP }
34
Coding Practice Create a dialog box as shown below and show it when pressing mouse left button Type on the edit control and choose a text color value from the radio buttons
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.