Objectives Chapter 3 Controls for Windows Forms 2004 Art Gittleman Add controls to Make selections Add links Add images Choose a date or number Time events Make menu choices Respond to dialogs RadioButton Drag radio buttons to form May display text, an image, or both Radio buttons from a group user must select exactly one Example3-1 has three radio buttons Each represents a color User selection changes Label text to the selected color Example3-1 Configure Properties BackColor Font Bold Text Anchor (Name) Red True Red Top redbutton (Similarly for the other two radio buttons)
Configuring the Label BorderStyle Fixed3D Font Size 14 Text The text color is black TextAlign MiddleCenter Anchor Left, Right Name display The BorderStyle property Used for the Label None FixedSingle Fixed3D The default Frames the label with black rectangle Recesses the label Handling a radio button click private void redradio_checkedchanged display.forecolor = Color.Red; display.text = "The color is red"; Changes the foreground color and text of the label The LinkLabel control Lets us add links to web sites This links to a site with information about George Washington Handle a Link Click Use the Start method of the Process class to start the Internet Explorer browser with the desired web site Event handling code: private void presidentlink_linkclicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e) System.Diagnostics.Process.Start( IExplore, http://www.whitehouse.gov/history/presidents/gw1.html ); Handle a Link Click Use the Start method of the Process class to start the Internet Explorer browser with the desired web site Event handling code: private void presidentlink_linkclicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e) System.Diagnostics.Process.Start(e.Link.LinkData.ToString( ));
Add Radio Buttons to select Handling a radio button selection When the user selects a radio button we want to do three things Show the president s name in the link Underline the link Associate the link with the site containing the president s biography. The event handling code private void george_checkedchanged presidentlink.text = "George Washington"; presidentlink.linkbehavior = LinkBehavior.AlwaysUnderline; presidentlink.links[0].linkdata = "http://www.whitehouse.gov/history/presidents/gw1.html"; A Collection Of Links A LinkLabel may have a collection of links. The Links property holds that collection Links[0] refers to the first link in the collection. The LinkLabel in Example3-2 has only one link The LinkData specifies the web site for the link Grouping Radio Buttons Drag a GroupBox to the Form Add each RadioButton to the GroupBox GroupBox has a border with a title Change its Text property to Choose a President To change the title in the group box border A Form may have more than one group
Selecting a Style The Border Style group box Choose a border style for the link label The user chooses exactly one radio button from each group one to choose the president and one to choose the border style Border Style event handling private void singleborder_checkedchanged presidentlink.borderstyle = BorderStyle.FixedSingle; private void threedborder_checkedchanged presidentlink.borderstyle = BorderStyle.Fixed3D; The PictureBox control The SizeMode property Normal StretchImage AutoSize CenterImage Places the image in the upper-left corner, clipped if too large Stretches or shrinks the image to fit Resizes the PictureBox to the size of the image Displays the image in the center if the PictureBox is larger than the image. Centers and clips the image if it is larger. Size modes in Example3-3 The CheckBox control Left Right (top to bottom) AutoSize Normal CenterImage StretchImage Make multiple selections Or select nothing May have a label, an image, a background image Can set border styles too None, FixedSingle, or Fixed3D May appear with a box to check or like a button
Example3-4 Steps to add an image Click on the View, Properties menu item to show the list of properties. Click on the Image property to display a small button at the right. Click on the small button to pop up a window to open a file. Browse to locate the desired image and click the Open button. Alignment ImageAlign property -- place the image at different positions within a CheckBox. CheckAlign property functions in the same way for the position of the box that the user checks. TextAlign property determines the placement of the text Steps to set alignment Click on the View, Properties menu item to show the list of properties. Click on the ImageAlign, CheckAlign, or TextAlign property to display a small button at the right. Click on the small button to pop up a grid of three rows and three columns that allows us to choose the alignment position. Click to choose one of the nine positions. The three vertical positions Top, Middle, and Bottom combine with the three horizontal positions, Left, Center, and Right to give the nine choices. FlatStyle property for CheckBox Event-handling code Flat Popup Standard System The box appears flat Appears flat until the mouse passing over makes it appear three-dimensional Appears three-dimensional Appearance determined by the user s operating system private void berrybox_checkedchanged display.text = "Strawberry " + berrybox.checked; Label shows the last box checked or unchecked
ListBox and ComboBox Example3-5 ListBox shows all the elements ComboBox pops them up Click on Items property to display a String Collection Editor to enter the items SelectedItem property refers to the selected item String Collection Editor ListBox SelectionMode None One MultiSimple MultiExtended No items can be selected One item can be selected Multiple items can be selected Multiple items can be selected and the user can use the shift, control, and arrow keys to make the selection Selecting a list item When the user selects an item, say hamburger Program displays message in a label hamburger has 9 letters build this expression in four parts hamburger the selected item has 9 the length of the selected word letters ListBox event handler private void foodlist_selectedindexchanged display.text = foodlist.selecteditem.tostring() + " has + foodlist.selecteditem.tostring().length.tostring() + " letters";
ComboBox event handler private void drinkscombo_selectedindexchanged display.text = "It's " + foodlist.selecteditem.tostring() + " and " + drinkscombo.text; Display a message showing the food and drink selected Keeping Track Various controls help us keep track of dates, time, and numbers DateTimePicker NumericUpDown StatusBar Timer Example3-6 Example3-6 operation DateTimePicker at top of form User chooses a date NumericUpDown in the middle User selects a number Button at bottom User presses to display date in a label and number selected in another label StatusBar at bottom shows current time Timer updates it every second, and changes label colors DateTimePicker Format Long Tuesday, January 28, 2003 Short 1/28/03 Time 10:32:56 AM Button Click handling private void showbutton_click datedisplay.text = "Date selected: " + date.value.tolongdatestring(); numberdisplay.text = "Number selected " + number.value.tostring();
A Timer control Timer Tick event every second private void timer1_tick currentdatetime.text = DateTime.Now.ToString(); number.backcolor = datedisplay.backcolor; datedisplay.backcolor = numberdisplay.backcolor; numberdisplay.backcolor = number.backcolor; Copies time to status bar Permutes background colors to give flashing effect Menus Drag a MainMenu control Add a File menu Add menu items File menu Open, Save, Print Format menu Font, Color menu Foreground Background Click on menu item Event handler pops up a dialog
Open menu click event handler private void openmenu_click opendialog.showdialog(); displaybox.loadfile(opendialog.filename, RichTextBoxStreamType.PlainText); Pops up a dialog to select the file Loads the selected file in a RichTextBox Save Click event handler private void savemenu_click savedialog.showdialog(); displaybox.savefile(savedialog.filename, RichTextBoxStreamType.PlainText); Pops up a dialog to enter a file name Saves file in RichTextBox using that name Foreground Menu Click private void foregroundmenu_click colordialog.showdialog(); displaybox.forecolor =colordialog.color; Pops up a dialog to choose a color Changes the RichTextBox text color
Font Menu Click private void fontmenu_click fontdialog.showdialog(); displaybox.font = fontdialog.font; Pops up a font dialog to choose a font Changes the font of the RichTextBox text Example3-7