360 likes | 537 Views
Learn how to create GUI applications step-by-step using event handling, control classes, and GUI controls. Explore event handlers, properties, and useful GUI elements like TextBox, Button, and ComboBox. Practice with Payroll Example code snippets.
E N D
Graphical User Interface(GUI) Ch 14, 15
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
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
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
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
Event Handler Header private void btnAdd_Click( object sender, EventArgs e) • Responds to the Click event on the Add button.
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
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
Referring to GUI Controls • (Name) in the "properties" window • Name of GUI control reference • Refer to the control in source code • Use meaningful names
Label • Displays text
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();
Button • Triggers an event when clicked • Provide an event handler method
CheckBox • Specifies a Boolean option • The Checked property • To get input employee.Married = chkMarried.Checked; • To display chkMarried.Checked = employee.Married;
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;
GroupBox • Arrange other controls on a GUI • Group similar functionality that are related • Example: a group of radio buttons
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();
ListBox • Provides a list of items • The SelectionMode property • The Items property • Add(), Remove(), Clear() methods • The SelectedItem property • Type casting may be needed
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;
Menu • Groups of related commands • Insert separator between sub groups • Provide event handlers for menu items
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(); }
privatevoid btnAdd_Click(object sender, EventArgs e) { add(); }
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); }
privatevoid btnUpdate_Click(object sender, EventArgs e) { if (lstEmployees.SelectedItems.Count == 0) { return; } if(add()) lstEmployees.Items.Remove( lstEmployees.SelectedItem); }
privatevoid lstEmployees_SelectedIndexChanged( object sender, EventArgs e) { if (lstEmployees.SelectedItems.Count == 0) { return; } Employee employee = (Employee)lstEmployees.SelectedItem; clear(); displayEmployee(employee); }
privatevoid clear() { txtFirstName.Text = ""; txtLastName.Text = ""; radMale.Checked = true; chkMarried.Checked = false; dtpBirthDate.Text = DateTime.Now.ToShortDateString(); cmbState.Text = "WISCONSIN"; }
privatebool add() { Employee employee; if (radSalaried.Checked) employee = newSalariedEmployee(); else employee = newHourlyEmployee(); if (getInputs(employee) == true) { lstEmployees.Items.Add(employee); displayEmployee(employee); returntrue; } returnfalse; }
privatebool getInputs(Employee employee) { try { employee.FirstName = txtFirstName.Text; } catch (Exception ex) { MessageBox.Show(ex.Message,Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); txtFirstName.Focus(); returnfalse; }
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; } }
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;
Summary • Form contains controls and components • Using the GUI Designer • Event handlers respond to events