360 likes | 521 Views
Pick up your free drink below to your left. 1 per person please. Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com. Pre-Lecture Topics. Web site resources Lecture & Materials http://microsoft.mike95.com MSDN AA program http://www.cs.fiu.edu/MSDNAA/
E N D
Pick up your free drink below to your left. 1 per person please. Michael OliveroMicrosoft Student Ambassador for FIU mike@mike95.com
Pre-Lecture Topics • Web site resources • Lecture & Materials • http://microsoft.mike95.com • MSDN AA program • http://www.cs.fiu.edu/MSDNAA/ • Clarification / Questions from Previous Lecture • “out” parameter • VB.NET vs. C# • Lab #1?
Topics Covered Today • Basic User Interface Design Principles • .NET Forms • Controls & Components • Menus • Validation • Give Away
Basic UI Elements • Forms • Generally contain information or options in order to proceed. • A Form is a class inherited fromSystem.Windows.Forms.Form • Controls • make information and options accessible to users • Some are display only such as labels and picture boxes • Some display and receive input such as text boxes. • Menus • Expose available commands to the users of your application
Things to keep in mind when designing • Simplicity • Grouping Related Controls & Default Values • Common mistake: reproducing real world form • Position of controls • Position of Required vs. Optional • Submit below information • Consistency • Forms should follow a pattern
.NET Forms After this lesson, you will be able to: • State the role of forms in an application • Explain how to add forms to your application • Explain how to set the start-up form and the start-up location • Explain how to set the visual appearance of your form • Explain how to use form events
.NET Forms • Enable interaction between user and application • A way to prompt for or display information without cluttering main UI. • Modal & Non Modal forms.
Common Form Methods • Form.Show() • Loads an instance into memory and makes it visible. • Form.ShowDialog() • Same as Show() except must be closed before control returns to main Form. • Form.Activate() • Brings form to the front and gives it focus. • Flashes task bar if app doesn’t have focus. • Form.Hide() • Hides the form from view. Form still is loaded though. • Form.Close() • Removes form from memory and all related resources.
Events in Forms • Something interesting happens • Think of it as verbs or actions • Form -> Activated • Form -> VisibleChanged • Form -> Move • You can write custom code for any event.
Typical Lifetime Events • Load • Fired when form is loaded into memory • Activate / Deactivate • Fired when form receives focus or looses focus respectively – only among within the current Application. • VisibleChanged • Fired when form is hidden or shown. • Closing • Fired when form is closing • Closed • Fired after form is closed.
Demo • Adding forms to a project • Inheriting existing forms • Setting Start-up Form • Startup location attribute • Appearance Settings • Opacity • Show() vs. ShowDialog() • Handling an Event
Topics Covered Today • Basic User Interface Design Principles • .NET Forms • Controls & Components • Menus • Validation • Give Away
Controls & Components After this lesson, you will be able to: • Explain the difference between controls and components • Explain how to set the control tab order • Describe which controls can contain other controls and how they are used • Describe docking and anchoring, and explain their use with controls • Describe the process for adding controls to the Toolbox • Describe how to create event handlers for controls • Explain what an extender is and how to use one
Controls & Components Are used to enhance functionality of form. • Component vs. Control? • Control has a Visual Interface • Ex. Button • Component has no Visual Interface • Ex. Timer • Components are added to the Components Tray
Container Controls Allows you to include other controls within them. • Ex. Group Box, Panel, etc. • You can manipulate all the controls together via the container control • Ex. Container.Visible = false
Docking & Anchoring • Specifies behavior of contained controls when form is resized. • Docking • Control is bound to a section of the form: Top, Bottom, Middle, etc. • Anchor • Control is bound by specific distance to edge.
Extender Property Components Components which extend the properties of existing components. • Ex. ToolTip & ErrorProvider • Once added to Form, all controls on form have a new property for definition.
Demo • Component Tray • Tab Order • Docking / Anchoring • Adding Controls to ToolBox • ErrorProvider Extender Control
Topics Covered Today • Basic User Interface Design Principles • .NET Forms • Controls & Components • Menus • Validation • Give Away
Using Menus After this lesson, you will be able to: • Describe the process of creating a menu using the MainMenu component • Describe the process of creating a context menu using the ContextMenu component • Explain how to enable or disable a menu item • Explain how to create shortcut keys for menu items • Explain how to display a check mark or a radio button on a menu item • Explain how to make menu items invisible • Explain how to dynamically add items to a menu • Explain how to dynamically clone a menu
Creating Menus in Design • Use the MainMenu from the toolbox • Create new menus • Set the properties of each menu • Assign event handler to each menu • Hints • To create a separator, use the hyphen • Modify other options through the property window while the menu is selected
Creating Menus in Design • Access Key (i.e. ALT-F for File) • Other properties through property window • Checked – displays checkmark next to menu • Radio – displays bullet next to menu
Modifying Menus Dynamically Adding Menu Programatically • MenuItem myItem; • myItem = new MenuItem("Item 1", new EventHandler(ClickHandler)); • fileMenuItem.MenuItems.Add(myItem); Merging Menus into One • fileMenuItem.MergeMenu(myContextMenu);
Context Menus • Special menu that appears as a pop-up menu when the right mouse button is clicked. • Can be created similar to a MainMenu using ContextMenu component. • Can be assigned to any Control via ContextMenu property. • Context menu can be cloned from an existing Menu to avoid duplicating code. • myContextMenu.MenuItems.Add(fileMenuItem.CloneMenu());
Things to Know • Menus are like regular controls: • Can make: Visible, Disabled, • You can Merge a menu
Demo • Adding Menu • Adding a Context Menu • Reusing a existing Menu for a Context Menu • Handling an Event for a menu
Topics Covered Today • Basic User Interface Design Principles • .NET Forms • Controls & Components • Menus • Validation • Give Away
Validating User Input After this lesson, you will be able to: • Explain the difference between form-level and field-level validation • Direct the focus using control methods and events • Implement form-level validation for your form • Implement field-level validation for your form
Form & Field Level Validation • Form Level • Validates all data at once. • Ex. User fills out entire form and clicks Submit. Validation occurs at the moment of submission. • Field Level • Validates as user enters data
Common Validation Properties • MaxLength • Maximum length textbox can have. • PasswordChar • The character shown to hide actual password characters • ReadOnly • Cannot make changes to control
Common Validation Events • KeyDown, KeyUp, KeyPress Events • Raised when key is pressed. • KeyPress is fired only if key pressed can be represented in by ASCII character. • Ex. ( a-z, A-Z, 0-9, Enter, Backspace ) • KeyEventArgs • Parameter passed to Events • Has properties to determine all possible combination of a key event.
Typical Validation Event Handler private void textBox1_KeyUp(object sender, KeyEventArgs e){ if (e.Alt == true) MessageBox.Show("The ALT key is down"); }
Character Validation Char data type can be used to inspect character pressed in KeyPress Event • Char.IsDigit • Char.IsLetter • Char.IsLetterOrDigit • Char.IsPunctuation • Char.IsLower • Char.IsUpper
Validating & Validated Events • Validating • Fired before control looses focus. • Validated • Fired after successfully firing Validating • The Validation can be canceled by • e.Cancel = true inside of the handler.
Form Level Validation • All fields are validated at once • Simpler than one may expect • Submit Button Event Handler simply checks controls individually or iteratively. • If one fails the check, then can use ErrorProvider and give it focus.
Topics Covered Today • Basic User Interface Design Principles • .NET Forms • Controls & Components • Menus • Validation • Give Away