?? mainform.cs
字號:
// MainForm.cs - Chapter 16 version.
// Copyright 2004 by Jacquie Barker and Grant Palmer - all rights reserved.
// A GUI class.
using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
public class MainForm : Form {
private Button dropButton, saveButton;
private Button addButton, logOffButton;
private TextBox ssnTextBox, nameTextBox, totalTextBox;
private ListBox scheduleListBox, registeredListBox;
private Label classScheduleLabel, ssnLabel;
private Label nameLabel, totalCourseLabel;
private Label registeredLabel;
private PasswordForm passwordDialog;
// Maintain a handle on the Student who is logged in.
// (Whenever this is set to null, nobody is officially logged on.)
private Student currentUser;
public MainForm() {
currentUser = null;
// Create left-hand side labels
int labelVertSpace = 40;
int labelLeft = 5;
ssnLabel = new Label();
ssnLabel.Text = "SSN:";
ssnLabel.Font = new Font(ssnLabel.Font, FontStyle.Bold);
ssnLabel.AutoSize = true;
ssnLabel.Top = 5;
ssnLabel.Left = labelLeft;
nameLabel = new Label();
nameLabel.Text = "Name:";
nameLabel.Font = new Font(nameLabel.Font, FontStyle.Bold);
nameLabel.AutoSize = true;
nameLabel.Top = ssnLabel.Top + labelVertSpace;
nameLabel.Left = labelLeft;
totalCourseLabel = new Label();
totalCourseLabel.Text = "Total Courses:";
totalCourseLabel.Font = new Font(totalCourseLabel.Font, FontStyle.Bold);
totalCourseLabel.AutoSize = true;
totalCourseLabel.Top = nameLabel.Top + labelVertSpace;
totalCourseLabel.Left = labelLeft;
registeredLabel = new Label();
registeredLabel.Text = "Registered For:";
registeredLabel.Font = new Font(registeredLabel.Font, FontStyle.Bold);
registeredLabel.AutoSize = true;
registeredLabel.Top = totalCourseLabel.Top + labelVertSpace;
registeredLabel.Left = labelLeft;
// Create TextBox components
ssnTextBox = new TextBox();
ssnTextBox.Width = 140;
ssnTextBox.AutoSize = true;
ssnTextBox.Top = ssnLabel.Top;
ssnTextBox.Left = totalCourseLabel.Right + 15;
// Assign an event handler to the SSN TextBox
ssnTextBox.KeyUp += new KeyEventHandler(SsnTextBoxKeyUp);
nameTextBox = new TextBox();
nameTextBox.Width = 140;
nameTextBox.AutoSize = true;
nameTextBox.Top = nameLabel.Top;
nameTextBox.Left = totalCourseLabel.Right + 15;
nameTextBox.ReadOnly = true;
totalTextBox = new TextBox();
totalTextBox.Width = 20;
totalTextBox.AutoSize = true;
totalTextBox.Top = totalCourseLabel.Top;
totalTextBox.Left = totalCourseLabel.Right + 15;
totalTextBox.ReadOnly = true;
// Create right-hand side labels
classScheduleLabel = new Label();
classScheduleLabel.Text = "--- Schedule of Classes ---";
classScheduleLabel.Font = new Font(classScheduleLabel.Font, FontStyle.Bold);
classScheduleLabel.AutoSize = true;
classScheduleLabel.Top = 5;
classScheduleLabel.Left = ssnTextBox.Right + 55;
// Create "Schedule of Classes" ListBox Component
scheduleListBox = new ListBox();
scheduleListBox.Width = 210;
scheduleListBox.Height = 225;
scheduleListBox.Top = classScheduleLabel.Bottom + 5;
scheduleListBox.Left = ssnTextBox.Right + 30;
// Display an alphabetically sorted course catalog list
// in the scheduleListBox component.
ArrayList sortedSections = SRS.scheduleOfClasses.GetSortedSections();
for(int i = 0; i < sortedSections.Count; ++i) {
scheduleListBox.Items.Add(sortedSections[i]);
}
// Create "Registered For" ListBox Component
registeredListBox = new ListBox();
registeredListBox.Width = 210;
registeredListBox.Top = registeredLabel.Bottom + 5;
registeredListBox.Height = scheduleListBox.Bottom - registeredListBox.Top + 3;
registeredListBox.Left = labelLeft;
// Add event handlers to the ListBox components
scheduleListBox.SelectedIndexChanged +=
new EventHandler(ScheduleSelectionChanged);
registeredListBox.SelectedIndexChanged +=
new EventHandler(RegisteredSelectionChanged);
// Create buttons
int buttonHeight = 50;
int buttonWidth = 80;
int buttonTop = 260;
dropButton = new Button();
dropButton.Text = "Drop";
dropButton.Height = buttonHeight;
dropButton.Width = buttonWidth;
dropButton.Top = buttonTop;
dropButton.Left = 10;
saveButton = new Button();
saveButton.Text = "Save My Schedule";
saveButton.Height = buttonHeight;
saveButton.Width = buttonWidth;
saveButton.Top = buttonTop;
saveButton.Left = dropButton.Right + 5;
addButton = new Button();
addButton.Text = "Add";
addButton.Height = buttonHeight;
addButton.Width = buttonWidth;
addButton.Top = buttonTop;
addButton.Left = saveButton.Right + 100;
logOffButton = new Button();
logOffButton.Text = "Log Off";
logOffButton.Height = buttonHeight;
logOffButton.Width = buttonWidth;
logOffButton.Top = buttonTop;
logOffButton.Left = addButton.Right + 5;
// Assign event handlers to the Buttons
addButton.Click += new EventHandler(AddButtonClicked);
dropButton.Click += new EventHandler(DropButtonClicked);
saveButton.Click += new EventHandler(SaveButtonClicked);
logOffButton.Click += new EventHandler(LogOffButtonClicked);
// Initialize the buttons to their proper enabled/disabled
// state.
ResetButtons();
// Add the GUI components to the form
this.Controls.Add(dropButton);
this.Controls.Add(saveButton);
this.Controls.Add(addButton);
this.Controls.Add(logOffButton);
this.Controls.Add(classScheduleLabel);
this.Controls.Add(ssnLabel);
this.Controls.Add(ssnTextBox);
this.Controls.Add(nameLabel);
this.Controls.Add(nameTextBox);
this.Controls.Add(totalCourseLabel);
this.Controls.Add(totalTextBox);
this.Controls.Add(registeredLabel);
this.Controls.Add(scheduleListBox);
this.Controls.Add(registeredListBox);
// Set some appearance properties for the Form
this.Text = "Student Registration System";
this.Height = 350;
this.Width = 500;
this.MinimumSize = this.Size;
this.StartPosition = FormStartPosition.CenterScreen;
this.Visible = true;
}
// event handling method for the "Drop" Button
public void DropButtonClicked(object source, EventArgs e) {
// Determine which section is selected (note that we must
// cast it, as it is returned as an Object reference).
Section selected = (Section)registeredListBox.SelectedItem;
// Drop the course.
selected.Drop(currentUser);
// Display a confirmation message.
string message = "Course " +
selected.RepresentedCourse.CourseNo + " dropped.";
MessageBox.Show(message, "Request Successful",
MessageBoxButtons.OK, MessageBoxIcon.Information);
// Update the list of sections that
// this student is registered for.
registeredListBox.Items.Clear();
IEnumerator ie = currentUser.GetEnrolledSections();
while ( ie.MoveNext() ) {
registeredListBox.Items.Add((Section)ie.Current);
}
// Update the field representing student's course total.
totalTextBox.Text = "" + currentUser.GetCourseTotal();
// Check states of the various buttons.
ResetButtons();
}
// event handling method for the "Save" Button
public void SaveButtonClicked(object source, EventArgs e) {
bool success = currentUser.Persist();
if (success) {
// Let the user know that his/her
// schedule was successfully saved.
MessageBox.Show("Schedule saved", "Schedule Saved",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else {
// Let the user know that there was a problem.
string message = "Problem saving your " +
"schedule; please contact " +
"SRS Support Staff for assistance.";
MessageBox.Show(message, "Problem Saving Schedule",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
// event handling method for the "Add" Button
public void AddButtonClicked(object source, EventArgs e) {
// Determine which section is selected (note that we must
// cast it, as it is returned as an Object reference).
Section selected = (Section)scheduleListBox.SelectedItem;
// Attempt to enroll the student in the section, noting
// the status code that is returned.
int status = selected.Enroll(currentUser);
// Report the status to the user.
if (status == Section.SECTION_FULL) {
MessageBox.Show("Sorry - that section is full.", "Request Denied",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else {
if (status == Section.PREREQ_NOT_SATISFIED) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -