C++
  Home arrow C++ arrow Page 6 - Using MFC in C++ Part 4: Controls, DDX and...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Using MFC in C++ Part 4: Controls, DDX and DDV
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 42
    2001-12-17

    Table of Contents:
  • Using MFC in C++ Part 4: Controls, DDX and DDV
  • Controls explained
  • The push button control
  • The check box control
  • The radio button control
  • The list box control
  • The group box control
  • DDX and DDV explained
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using MFC in C++ Part 4: Controls, DDX and DDV - The list box control


    (Page 6 of 9 )

    The list box control is used to display a number of options in a selectable, scrollable list format. You can allow the user to select one/more options inside of the list. The syntax for a list box control is shown below:

    LISTBOX [Control Id], [X Pos], [Y Pos], [Width], [Height], [Style Options]

    A sample declaration for a list box control is shown below:

    LISTBOX IDLB_NAMES, 10, 105, 100, 100, WS_VISIBLE | WS_CHILD | WS_TABSTOP | LBS_NOTIFY

    We have already talked about the first five options for this control: They are the same as those for the EDITTEXT control. Notice though, that we have added the LBS_NOTIFY macro to the list boxes’ style. This tells windows to send us notification messages whenever the list is changed, clicked on, etc.

    Simply creating a list box control isn’t enough, because it starts out empty. We can use the AddString method to add options to the list, like this:

    CListBox* pList = (CListBox*)GetDlgItem(IDLB_NAMES);



    pList->AddString("John");

    pList->AddString("Fred");

    pList->AddString("Mark");


    In our example above, we retrieve a pointer-to-CListBox reference to the list box. The CListBox class is used to communicate with a LISTBOX control. Next, we simply use the AddString method of the CListBox class to add three new options to our list. In this case, we are adding three names, as shown below:

    Adding names to our LISTBOX control

    As mentioned earlier, the support material included with this article contains a complete application demonstrating each of the controls we are discussing. In the included application, when you click on the “Add Names” button, three names are added to our list box control.

    To retrieve the value of the selected option, we use the GetText function. The GetText function takes two parameters: The index of the list item to retrieve the value from (list box indexes start from zero), and a reference to a CString variable, which windows will store the value of the option in. To retrieve a name from our example list (as pictured above), we would use the following code:

    CListBox* pList = (CListBox*)GetDlgItem(IDLB_NAMES);

    CString name;



    if(pList->GetCurSel() >=0)

    {

    pList->GetText(pList->GetCurSel(), name);

    MessageBox(name);

    }

    else

    MessageBox("Please select a name first!");


    We use the GetCurSel function to return the index of the currently selected item. If the index is equal to –1, then no option is selected, and we kindly inform the user to select an option. On the other hand, if the user has selected an option, we use the GetText function and save its value to the CString name variable.

    As with most of the controls discussed so far, there are several other functions that you can use to manipulate list boxes, and you should explore these further on your own.

    More C++ Articles
    More By Mitchell Harper


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek