Selection Lists
[Previous] [Main] [Next]

 
Checkboxes and radio buttons are useful for relatively short, well-defined lists of options. However, they can become cumbersome for large lists or crowded forms. The htmlInputSelect and htmlOption classes allow you to add drop-down and scrollable listboxes to forms, from which a user can select one or more choices.  
 
By default, a user will only be able to select a single option from the list. The MultiSelect() method allows you to enable selection of multiple options from the list. Usually, the user does this by holding down the shift key while clicking on each option. Non-adjacent options can usually be selected by holding down the Control key and clicking.  
 
The Size() method controls the display size of the selection list, letting you specify the number of options in the list that are displayed at one time. For multi-selection lists, the default behavior of most browsers is to display some (perhaps all) of the options. For single-selection lists, most browsers display the list as a single-line, drop-down menu.  
 
When the form containing the selection list is submitted to the server, a name/value pair is sent for each selected option in the list.  
 
Options may be added to the selection list using the Add() method or the << insertion operator. For convenience, these methods over overloaded to accept strings as well, so it's usually not neccessary to use htmlOption objects unless you need to pre-select one of the list items.  
 
List options may also be stored in a text file, one option per line, and added as a group using the AddFile() method.  
 
 
Example
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlCgi   server ;  
   htmlPage  page( "html++ example application" ) ;  
 
   //  Create a selection list of a few states  
   htmlInputSelect   sel( "STATE" ) ;  
   sel << "Maine"   
       << "New Hampshire"  
       << "Vermont"  
       << "Massachusetts"  
       << "Rhode Island"  
       << htmlOption"Connecticut", SELECTED )  
       << "New York"  
       << "New Jersey"  
       << "Pennsylvania" ;  
 
   //  Create a simple form to hold our controls  
   htmlForm  form( "/cgi-bin/script" ) ;  
 
   //  First, output a single-select version of  
   //  the list. It will appear as a drop-down control.  
   form << sel ;  
 
   //  Now output a multi-select version of the  
   //  same list. It will appear as an in-screen control.  
   sel.MultiSelect() ;  
   form << sel ;  
 
   //  Include a submit button to be complete  
   form << htmlParagraph()   
        << htmlCenter( htmlInputSubmit() ) ;  
 
   page << form ;  
 
   server << page ;  
   return 0 ;  
}  
 
Program Output
 
Content-Type: text/html  
<HTML>  
<HEAD>  
<TITLE>html++ example application</TITLE>  
</HEAD>  
<BODY>  
 
<FORM ACTION="/cgi-bin/script" METHOD="POST">  
 
<SELECT NAME="STATE"><OPTION>Maine  
<OPTION>New Hampshire  
<OPTION>Vermont  
<OPTION>Massachusetts  
<OPTION>Rhode Island  
<OPTION SELECTED>Connecticut  
<OPTION>New York  
<OPTION>New Jersey  
<OPTION>Pennsylvania  
</SELECT>  
 
<SELECT NAME="STATE" MULTIPLE><OPTION>Maine  
<OPTION>New Hampshire  
<OPTION>Vermont  
<OPTION>Massachusetts  
<OPTION>Rhode Island  
<OPTION SELECTED>Connecticut  
<OPTION>New York  
<OPTION>New Jersey  
<OPTION>Pennsylvania  
</SELECT>  
<P></P>  
<CENTER><INPUT TYPE="SUBMIT"></CENTER>  
</FORM>  
 
</BODY>  
</HTML>  
 
 

select.gif  



 


©1998 DC Micro Development. All rights reserved.
No portion of this document may be c opied or reproduced without expressed written consent.
html++ is a trademark of DC Micro Development.