Download
graphical user interface gui n.
Skip this Video
Loading SlideShow in 5 Seconds..
Graphical User Interface (GUI) PowerPoint Presentation
Download Presentation
Graphical User Interface (GUI)

Graphical User Interface (GUI)

8 Vues Download Presentation
Télécharger la présentation

Graphical User Interface (GUI)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Graphical User Interface(GUI) Ch 14, 15

  2. Payroll Example

  3. Creating a GUI Application • Create a new project • Choose “Windows Application” template • A blank Form will be created • Design the form using the GUI designer • Add other necessary classes • Add event-handling code • Compile (Build), Run, and Debug

  4. Windows Form • Used to create GUIs for programs • Container for controls and components • Form base class • Defined in System.Windows.Forms namespace • Text property • Text in the title bar • Close() method • Closes a Form

  5. Using the GUI Designer • Drag-and-drop GUI controls to a Form • Edit the GUI controls • Change the properties of the GUI controls • in the "Properties" window • Visual Studio generates much of the GUI-related code

  6. Event Handling • GUI applications are event driven • User interaction with a GUI component triggers an event • A method that performs a task in response to an event is called an event handler

  7. Event Handler Header private void btnAdd_Click( object sender, EventArgs e) • Responds to the Click event on the Add button.

  8. Creating Event Handlers • Double-clicking a GUI control creates a handler for the default event • Other handlers need to be manually created and registered • Write a handler method • Setup the handler to respond to the appropriate event

  9. Events in the Properties Window

  10. GUI Controls • Display information on a Form or enable users to interact with application • All derived from the Control class • Share many common properties, methods, and events • Each has some special features

  11. Control Class Properties, Methods

  12. Referring to GUI Controls • (Name) in the "properties" window • Name of GUI control reference • Refer to the control in source code • Use meaningful names

  13. Label • Displays text

  14. TextBox • Enables the user to enter data • The ReadOnly property • To get input ((HourlyEmployee)employee).Hours = decimal.Parse(txtHoursWorked.Text); • To display txtHoursWorked.Text = ((HourlyEmployee)employee).Hours.ToString();

  15. Button • Triggers an event when clicked • Provide an event handler method

  16. CheckBox • Specifies a Boolean option • The Checked property • To get input employee.Married = chkMarried.Checked; • To display chkMarried.Checked = employee.Married;

  17. RadioButton • Specifies one of several mutually exclusive options • The Checked property • To get input if (radSalaried.Checked) employee = newSalariedEmployee(); • To display if (employee isSalariedEmployee) radSalaried.Checked = true;

  18. GroupBox • Arrange other controls on a GUI • Group similar functionality that are related • Example: a group of radio buttons

  19. DateTimePicker • Facilitates entering a date (and time) • The Format property • Example: mm/dd/yyyy for a date • The Value property • To get input employee.BirthDate = dtpBirthDate.Value; • To display dtpBirthDate.Text = employee.BirthDate.ToShortDateString();

  20. ListBox • Provides a list of items • The SelectionMode property • The Items property • Add(), Remove(), Clear() methods • The SelectedItem property • Type casting may be needed

  21. ComboBox • Combination of TextBox and ListBox • The Items property • Should be populated initially (e.g., in the constructor) • To get input employee.HomeAddress = newAddress(txtStreet.Text, txtCity.Text, cmbState.Text, txtZip.Text); • To display cmbState.Text = employee.HomeAddress.State;

  22. Menu • Groups of related commands • Insert separator between sub groups • Provide event handlers for menu items

  23. Payroll Example (Partial Code) publicpartialclassPayrollSystemForm : Form { public PayrollSystemForm() { InitializeComponent(); // populate the state combobox foreach (string state inAddress.StateNames) cmbState.Items.Add(state); // clear all fields clear(); }

  24. privatevoid btnAdd_Click(object sender, EventArgs e) { add(); }

  25. privatevoid btnDelete_Click(object sender, EventArgs e) { if (lstEmployees.SelectedItems.Count == 0) { return; } if (MessageBox.Show("Are you sure to delete " + (Employee)lstEmployees.SelectedItem, "Payroll System", MessageBoxButtons.YesNo, ) == DialogResult.Yes) lstEmployees.Items.Remove( lstEmployees.SelectedItem); }

  26. privatevoid btnUpdate_Click(object sender, EventArgs e) { if (lstEmployees.SelectedItems.Count == 0) { return; } if(add()) lstEmployees.Items.Remove( lstEmployees.SelectedItem); }

  27. privatevoid lstEmployees_SelectedIndexChanged( object sender, EventArgs e) { if (lstEmployees.SelectedItems.Count == 0) { return; } Employee employee = (Employee)lstEmployees.SelectedItem; clear(); displayEmployee(employee); }

  28. privatevoid clear() { txtFirstName.Text = ""; txtLastName.Text = ""; radMale.Checked = true; chkMarried.Checked = false; dtpBirthDate.Text = DateTime.Now.ToShortDateString(); cmbState.Text = "WISCONSIN"; }

  29. privatebool add() { Employee employee; if (radSalaried.Checked) employee = newSalariedEmployee(); else employee = newHourlyEmployee(); if (getInputs(employee) == true) { lstEmployees.Items.Add(employee); displayEmployee(employee); returntrue; } returnfalse; }

  30. privatebool getInputs(Employee employee) { try { employee.FirstName = txtFirstName.Text; } catch (Exception ex) { MessageBox.Show(ex.Message,Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); txtFirstName.Focus(); returnfalse; }

  31. if (employee isSalariedEmployee) { try { ((SalariedEmployee)employee).WeeklySalary = decimal.Parse(txtWeeklySalary.Text); } catch (Exception ex) { MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); txtWeeklySalary.Focus(); returnfalse; } }

  32. privatevoid displayEmployee(Employee employee) { txtFirstName.Text = employee.FirstName; txtHomePhone.Text = employee.HomePhone.ToString(); chkMarried.Checked = employee.Married; radMale.Checked = employee.IsMale; dtpBirthDate.Text = employee.BirthDate.ToShortDateString(); cmbState.Text = employee.HomeAddress.State;

  33. Summary • Form contains controls and components • Using the GUI Designer • Event handlers respond to events