Chapters 3, 12, 13
Using Radio Buttons in an interface
- Add a group box to a form using the GroupBox tool - Placing each group of radio buttons in a separate group box allows the user to select one button from each group - Selecting a radio button deselects any others in the group - The recommended maximum number of radio buttons in a group is seven - Windows applications normally have one of the radio buttons in each group already selected as a default radio button
Button guidelines
- Size buttons relative to each other: same height - If stacked horizontally, same width also - Most commonly used button should be first - Use book title capitalization for buttons
Borders, Sizing, and Text Alignment
-A control's border is determined by its BorderStyle property, which can be set to None, FixedSingle, or Fixed3D -A label control's AutoSize property determines whether the control automatically sizes to fit its current contents -A label control's TextAlign property determines the alignment of the text within the label •The TextAlign property can be set to nine different values, such as TopLeft, MiddleCenter, and BottomRight
Event
-A reaction to an occurrence in a program. -Occurs when something interesting happens to an object. -Actions that occur during program execution occur at runtime Events can be raised, fired, or triggered
Graphics, Fonts, and Colors
-Interfaces that contain a lot of different colors, fonts, and graphics become tiresome after a while -The human eye is attracted to pictures before text, so use graphics to either emphasize or clarify a portion of the screen -Keep the following three points in mind when deciding whether to include color in an interface: •People who have some form of either color blindness or color confusion will have trouble distinguishing colors •Color is very subjective: A color that looks pretty to you may be hideous to someone else. •A color may have a different meaning in a different culture
Using a Checked List Box in an Interface
A Checked List Box control is similar to a list box, with check boxes appearing to the left of each desired item
Button's caption naming
A buttons caption should be entered using book title capitalization, which means you capitalize the first letter in each word, except for articles, conjunctions, and prepositions that do not occur at either the beginning or end of the caption
Event Handler
A method that performs a task in response to an event.
Radio Buttons
Allow you to limit the user to only one choice from a group of two or more related but mutually exclusive choices Each radio button is labeled so the user knows the choice it represents The CheckedChange event occurs anytime the value of the Checked property changes
Access key
Allows the user to select an object using the Alt key in combination with a letter or number
Click event
An action is fired when a user clicks a button during program execution
Event driven
An event "drives" the program to perform a task
Event wiring
Connecting an event to its resulting actions
Updating Label Text value at Runtime
Most of the time, labels are used for displaying static text (that won't change), such as prompts and instructions which will not change during run-time however, in some cases labels can be used to display text that may update based on user-input or values pulled for data sources remember, it is important to always give controls meaningful names when you intend to reference them in code
Naming Forms
Provide reasonable Name property values for all the controls you place on a Form - Conventionally, start control names with a lowercase letter and use camel casing - Start Form names with an uppercase letter and use Pascal casing - A Form is a class, and C# class names conventionally start with an uppercase letter - Use the type of object in the name
Naming Controls
To rename a control after you have generated code for it, use the code refactoring feature - Which is the process of changing a program's internal structure without changing the way the program works
How to refactor the code after changing the button's name
View the code and right-click the name of the method button1_Click() From the menu, click Rename When you click Rename, the button1_Click method name is highlighted and a box appears with the heading Rename button1_Click Type the new method name When you finish, you can check the Preview Changes checkbox if you want to view all the changes before they are applied
Load event
a forms load event occurs when an application is started and the form is about to be displayed for the first time - any code contained in the Load event procedure is processed before the form is displayed on the screen
Designating the Cancel Button on a Form
activated by hitting the ESC key.
The Dock property
attaches a Control to the side of a container so that the Control stretches when the container's size is adjusted.
The Anchor property
causes a Control to remain at a fixed distance from the side of a container when the user resizes it.
Labels
controls that you use to display text to communicate with an application's user You can change its Text property to display any text value
TextBoxes
controls through which a user can enter input data into a GUI application when a user types a value into a TextBox, it becomes the value of the Text property, which is a string value by default
Focus
defines which control is active and will accept keyboard input - A button that has focus will have a bright line around it - A textbox that has focus will have a blinking cursor inside of it a control can be given by: - The user clicking on it - The user pressing the Tab key, which will set the focus on the next control as defined by the tab order
The MessageBox.Show Method
displays a message box that contains text, one or more buttons, and an icon; allows an application to communicate with the user while the application is running
Checked property
indicates if the check box is selected, evaluates to either true or False
Check Boxes
provide one or more independent and nonexclusive items from which the user can choose An interface can contain any number of check boxes, and any number of them can be selected or deselected at the same time Each check box should be labeled to make its purpose obvious
Accessing Items in a List Box pt2
pt2 •You can use either the SelectedItem property or the SelectedIndex property to determine whether an item is selected in a list box •When no item is selected, the SelectedItem property contains the empty string, and the SelectedIndex property contains the number −1 (negative 1) •Otherwise, the SelectedItem and SelectedIndex properties contain the value of the selected item and the item's index, respectively •The selected item is called the default list box item
Tab order
represented by a sequential number known as the TabIndex (View -> Tab Order) •Each control's TabIndex property contains a number that represents the order in which the control was added to the form (by default) •The first control added to a form has a TabIndex value of 0; the second control has a TabIndex value of 1; and so on •The TabIndex values determine the tab order, which is the order that each control receives the focus when the user either presses the Tab key or employs an access key while an application is running. •You can change a control's TabIndex value by changing the entry next to the TabIndex in the Properties list of the IDE.
The Padding property
specifies the distance between docked Controls and the edges of the Form.
Sentence capitalization
you capitalize only the first letter in the first word and in any words that are customarily capitalized
Using a Combo Box in an Interface
•A combo box is similar to a list box in that it allows the user to select from a list of choices -Unlike a list box, the full list of choices in a combo box can be hidden, allowing you to save space on the form -Unlike a list box, a combo box contains a text field -Depending on the style of the combo box, the text field may or may not be editable by the user -The style is controlled by the combo box's DropDownStyle property, which can be set to Simple, DropDown (the default), or DropDownList •To sort the items in the list portion of a combo box, you set the combo box's Sorted property to True -As you can with a list box, you can use the String Collection Editor window to specify the combo box items during design time -You can open the window by clicking Edit Items on the combo box's task list -You can also click the ellipsis button in the Items property in the Properties list. During run time, -You use the Items collection's Add method to add an item to a combo box, as shown in the code in
Using the KeyPress Event
•A control's KeyPress event occurs each time the user presses a key while the control has the focus -The character corresponding to the pressed key is sent to the event's 'e' parameter -The prevent a textbox from accepting an inappropriate character, you first use the 'e' parameter's KeyChar property to determine the pressed key -You then use the 'e' parameter's Handled property to cancel the pressed key if it is an inappropriate one •You cancel the key by setting the Handled property to True -> e.Handled = True •The KeyPress event procedure determines whether the value stored in the KeyChar property is inappropriate for the text box. •The KeyPress event automatically allows the use of the Delete key for editing
Coding the TextChanged Event Procedure
•A control's TextChanged event occurs when a change is made to the contents of the control's Text property. •To create event method code block click on the control and navigate to the controls events list. Double click in the text entry box next to the TextChanged event.
Assigning access keys
•Assign access keys to each of the controls (in the interface) that can accept user input -Exceptions to this rule are the OK and Cancel buttons, which typically do not have access keys in Windows applications •It is important to assign access keys to controls for the following reasons: -They allow a user to work with the application even when the mouse becomes inoperative -They allow users who are fast typists to keep their hands on the keyboard -They allow people who cannot work with a mouse, such as people with disabilities, to use the application
Accessing Items in a List Box
•Each item in the Items collection is identified by a unique number, which is called an index •The first item in the collection (which also is the first item in the list box) has an index of 0 •The second item's index is 1, and so on •The index allows you to access a specific item in the list box •The number of items in a list box is stored in the Items collection's Count property •Example: animalsListBox.Items.Count.ToString()); •The property's value is always one number more than the last index in the list box; this is because the first index in a list box is 0
Format Multi-Line String Output
•Environment.NewLine Property -When working with strings that contain multiple lines of text, each line is separated with a line break. Line breaks are often added by inserting carriage return and line feed escape characters. This can cause cross-platform compatibility problems. -To resolve this problem, the .NET framework includes a property that contains the correct escape sequence for a line break. This static property is found in the Environment class in the System namespace. The property returns a line feed string that is appropriate to the current operating system. string newLine = Environment.NewLine;
Event Handling Example: When a user clicks a button, you want to display a message
•Event sender -The object that generates an event (such as a Button) •Event receiver -The event-handling method
Handling Control Component Events
•Existing Control components already have events with names -To handle events generated by GUI Controls, you use the same techniques as when you handle any other events -The major difference is that when you create your own classes, like Student, you must define both the data fields and events you want to manage -Existing Control components, like Buttons and ListBoxes, already contain fields and public properties, like Text, as well as events with names, like Click
Using List Boxes in an interface
•List Boxes, Combo Boxes, and Checked List Boxes allow users to select choices from a list. They are classes that descend from the ListControl class. •A List Box displays a list of items from which the user can select zero items, one item, or multiple items •The number of items the user can select is controlled by the list box's SelectionMode property •The default value for the property, One, allows the user to select only one item at a time •The SelectedItem property contains the value of the item a user has selected.
Designating the Default Button on a Form
•Specify the default button by setting the form's AcceptButton property to the name of the button •A form can have only one default button but it does not have to have a default button
Adding items to a List Box
•The items in a list box belong to a collection called the Items collection •A collection is a group of individual objects treated as one unit •The first item in the Items collection appears as the first item in the list box •The second item in the collection appears as the second item in the list box, and so on •Rather than using the String Collection Editor window to add items to a list box, you can use the Items collection's Add method
Adding Functionality to a Form Button
•The runButton_Click() event method, like the Main() method, uses the void return type, indicating that the methods do not return a value. •While they don't return a value when called, void methods perform actions based on the code statements written inside the method code block. •The runButton_Click() event method also has a private access specifier, which is a keyword that dictates which types of outside classes can use a method. (method scope à restricted to containing class)
Deleting an Unwanted Event-Handling Method
•When you are working in the Form Designer, it is easy to inadvertently double-click a control and create an event-handling method you don't want -You can leave the event-handling method empty, but that's considered bad form -You should not just delete the method because, behind the scenes, other code will have been created that refers to the method -Instead, go to the Properties window of the control, and then click on its Events button, select the button you want to eliminate, and delete the method name