Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 More Controls Programming in C#. NET. 1- 2 Objectives Use text boxes, group boxes, check boxes, radio buttons, and picture boxes effectively.

Similar presentations


Presentation on theme: "Chapter 2 More Controls Programming in C#. NET. 1- 2 Objectives Use text boxes, group boxes, check boxes, radio buttons, and picture boxes effectively."— Presentation transcript:

1 Chapter 2 More Controls Programming in C#. NET

2 1- 2 Objectives Use text boxes, group boxes, check boxes, radio buttons, and picture boxes effectively Set the BorderStyle property to make controls appear flat or three-dimensional Select multiple controls and move them, align them, and set common properties

3 1- 3 Objectives cont. Make your projects easy for the user to understand and operate Clear the contents of text boxes and labels Change text color during program execution Concatenate (join) strings of text Make a control visible or invisible at run time by setting its Visible property

4 1- 4 Introducing More Controls Text boxes, group boxes, check boxes, radio buttons, and picture boxes are found in the toolbox Each class of controls has own properties To see control properties –Place control on form and view properties list or –Click on control and press F1 for Help

5 1- 5 Text Boxes Use a text box when you want the user to type some input Set the Text property in code to assign a literal for display Set the TextAlign property to change alignment of text in the box –HorizontalAlignment.Left –HorizontalAlignment.Right –HorizontalAlignment.Center Example: messageText.TextAlign=HorizontalAlignment.Left;

6 1- 6 Group Boxes Used as containers for other controls Make forms easier to understand Set Text property to words to appear on top edge of group box

7 1- 7 Check Boxes Allow user to select or deselect an option In a group of check boxes, any number may be selected The Checked property is set to true if checked and false if not checked CheckedChanged event triggers when the user clicks in a Check Box Text property sets text to appear next to the box

8 1- 8 Radio Buttons Use when only one button of a group may be selected Radio buttons placed directly on a form function as a group The Checked property is set to true if checked and false if not checked Both CheckedChanged and Click event triggers when the user clicks on a Radio Button

9 1- 9 Picture Boxes Used to hold an image Set Image property to the name of a graphic file The picture is saved in the form’s.resx file Set the SizeMode property to StretchImage to make picture fill the control

10 1- 10 Setting a Border and Style Labels, text boxes, and picture boxes have a BorderStyle property BorderStyle options –None –FixedSingle –Fixed3D

11 1- 11 Drawing a Line Draw a line using the Label control –Set Text property to blank –Set BorderStyle to None –Change Backcolor to color of line –Control size of line using Width and Height properties

12 1- 12 Selecting Multiple Controls Drag a selection box around the controls Click on one control to select it; Hold down the Ctrl or Shift key and click on other controls to select Use combination of above methods Click Select All on the Edit menu to select all controls on a form Click anywhere on form or on another control to deselect the controls

13 1- 13 Controls as a Group Move controls as a group by clicking on one of the selected controls and dragging the group to a new location Set common properties for group of controls using the Properties window Align group of controls using buttons on the Layout toolbar or items on the Format menu

14 1- 14 Designing the User Interface Design applications to match other Windows applications Follow industry standards for color, size, and placement of controls –Windows applications are mostly gray –Color can indicate what is expected of the user –Group controls to aid the user –Use sans serif font on forms and do not make them bold –Limit large font sizes to a few items

15 1- 15 Defining Keyboard Access Keys Use access keys (hot keys) for project response with keyboard Type an ampersand (&) in front of the character to be the access character Example: &OK for OK Use Windows-standard keys whenever possible Make sure you don’t give two controls the same access key

16 1- 16 Setting the Accept and Cancel Buttons It is sometimes preferable to use the Enter key or Esc key instead of moving the mouse Accept button acts as the Enter key –Set the AcceptButton property of form to button name Cancel button acts as the Esc key –Set the CancelButton property of form to button name

17 1- 17 Focus One control on the form always has the focus Focus may appear as a light dotted line on some controls such as buttons Insertion point appears inside a text box that has focus Some controls can receive focus and others cannot

18 1- 18 The Tab Order Controls that can receive focus have a TabStop property The TabIndex property determines the order the focus moves as the Tab key is pressed As controls are created, TabIndex is set in sequence When program starts, focus is on the control with lowest TabIndex

19 1- 19 The Tab Order cont. Label controls cannot receive focus Default value of TabStop for buttons and text boxes is true Default value of TabStop for radio buttons is false The tab key takes you to one radio button in a group; Use arrow keys to move to another radio button in the group

20 1- 20 Setting the Tab Order Set each control’s TabIndex property in Properties window Can also use Visual Studio.NET feature to set TabIndexes automatically –Open Design window –Select View / Tab Order –Click on controls in desired tab order –Sequence numbers representing tab order on controls are updated –Select View / Tab Order to hide sequence numbers

21 1- 21 Setting the Form’s Location on the Screen By default, the form appears in the upper-left corner of the screen Set form’s screen position with StartPosition property StartPosition options –Manual –CenterScreen –WindowsDefaultLocation –WindowsDefaultBounds –CenterParent

22 1- 22 Creating ToolTips Add a ToolTip component to a form ToolTip control is placed in component tray at bottom of the Form Designer Component tray hold controls that do not have a visual representation at run time Each control on form has new property: ToolTip on ToolTip1 (assume name of control is ToolTip1)

23 1- 23 Coding for the Controls You can change properties as a project executes Examples include clearing out contents of a text box or label, resetting the focus, and changing the color of text

24 1- 24 Clearing Text Boxes and Labels Set Text property to an empty string Empty string shown as “” Use the Clear() method Set the Text property to string.Empty Examples: nameTextBox.Text = “”; messageLabel.Text = “”; dataEntryTextBox.Clear(); messageLabel.Text = string.Empty;

25 1- 25 Resetting the Focus Use the Focus method to reset the focus to a particular control Example: nameTextBox.Focus();

26 1- 26 Set the Checked Property of Radio Buttons and Check Boxes Select or deselect a control in code by setting the Checked property to true or false Must use lowercase for true or false Examples: redRadioButton.Checked = true; //Select button displayCheckBox.Checked = true; //Make box checked displayCheckBox.Checked = false; //Make box unchecked

27 1- 27 Setting Visibility at Run Time Use Visible property to set visibility Example: messageLabel.Visible = false ; Can make visibility depend on selection user makes in a check box or radio button Example: messageLabel.Visible = displayCheckBox.Checked;

28 1- 28 Changing the Color of Text Use ForeColor property to change the color of the text Use BackColor property to change the color around the text The Color class contains color constants Type keyword Color and a period to see full list of available colors Examples: nameTextBox.ForeColor = Color.Red; messageLabel.ForeColor = Color.White;

29 1- 29 Concatenating Text Join two strings of text using process called concatenation Use a plus (+) between the two strings Example: messageLabel.Text = “Your name is “ + nameTextBox.Text;

30 1- 30 Continuing Long Program Lines You can break a statement at any white space The end of a statement is determined by the semicolon To break in the middle of a literal, you must concatenate the strings to rejoin them Example: greetingsLabel.Text = “Greetings “ + nameTextBox.Text + “: You have been selected to win a free prize. “ + “Just send us $100 for postage and handling”;

31 1- 31 Writing Event Handlers Select the method to respond to an event in the Properties window –Click on Events button at top of window –Select the event and drop down list of available methods

32 1- 32 Your Hands-On Programming Example For this example you will write a program that uses many of the new controls and topics introduced in this chapter. The program will input the user’s name and a message and display the two items concatenated in a label. The user can change the color of the label’s text by selecting with radio buttons, and hide or display the output by checking a check box.

33 1- 33 Your Hands-On Programming Example cont. You will include buttons to display the message in the label, clear the text boxes and label, and exit. Include the keyboard access keys; make the Display button the accept button and make the Clear button the cancel button. Place a logo on the form. Actually, you will place two picture boxes with different sizes for the logo on the form. Each time the user clicks on the logo, it will toggle the large and small versions of the logo. Add a ToolTip to the logo that says “Click here”.

34 1- 34 Good Programming Habits Make the text in a text box right justified or centered using the TextAlign property Use the Checked property of a check box to set other properties that must be true or false Always test the tab order on your forms; Fix it if necessary using the TabIndex properties

35 1- 35 Good Programming Habits cont. Create multiple controls of the same type by Ctrl-clicking on the tool and drawing the needed controls Use text boxes when you want the user to enter or change the text Use label controls when you do not want the user to change data

36 1- 36 Summary Text boxes are used primarily for user input. Group boxes are used as containers for other controls and to group like items on a form. Check boxes and radio buttons allow the user to make choices. The current state of check boxes and radio buttons is stored in the Checked property. Picture box controls hold a graphic which is assigned to the Image property.

37 1- 37 Summary cont. The BorderStyle property determines whether the control appears flat or three-dimensional. Use a Label control to create a line on a form. You can select multiple controls and treat them as a group. Follow Windows standard guidelines for colors, control size and placement, access keys, default and cancel buttons, and tab order.

38 1- 38 Summary cont. Define keyboard access keys by including an ampersand in the Text property. Set the AcceptButton property so the user can press Enter, and set the CancelButton property so the user can press Esc. The focus moves from control to control as the user presses the Tab key. Add a ToolTip control to a form then set the ToolTip on ToolTip1 property for each control.

39 1- 39 Summary cont. Clear the Text property by setting it to an empty string. Use the Focus method to make a control have focus. You can set the Checked property of a radio button or checkbox at run time, and the Visible property of all controls. Change the color of text in a control with the ForeColor property. Use the color constants to change colors at run time. Concatenation is the joining of two strings with a plus sign.


Download ppt "Chapter 2 More Controls Programming in C#. NET. 1- 2 Objectives Use text boxes, group boxes, check boxes, radio buttons, and picture boxes effectively."

Similar presentations


Ads by Google