?? mainform.cs
字號:
string message = "You haven't satisfied all " +
"of the prerequisites for this course.";
MessageBox.Show(message, "Request Denied",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else {
if (status == Section.PREVIOUSLY_ENROLLED) {
string message = "You are enrolled in or have successfully " +
"completed a section of this course.";
MessageBox.Show(message, "Request Denied",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else { // success!
string message = "Seat confirmed in " +
selected.RepresentedCourse.CourseNo + ".";
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();
// Clear the selection in the schedule of classes list.
scheduleListBox.SelectedItem = null;
}
}
}
// Check states of the various buttons.
ResetButtons();
}
// event handling method for "Log Off" Button
public void LogOffButtonClicked(object source, EventArgs e) {
ClearFields();
ssnTextBox.Text = "";
currentUser = null;
// Clear the selection in the
// schedule of classes list.
scheduleListBox.SelectedItem = null;
// Check states of the various buttons.
ResetButtons();
}
// event handling method for the "Schedule of Classes" ListBox
public void ScheduleSelectionChanged(object source, EventArgs e) {
// When an item is selected in this list,
// we clear the selection in the other list.
if (scheduleListBox.SelectedItem != null) {
registeredListBox.SelectedItem = null;
}
// reset the enabled state of the buttons
ResetButtons();
}
// event handling method for the "Registered For:" ListBox
public void RegisteredSelectionChanged(object source, EventArgs e) {
// When an item is selected in this list,
// we clear the selection in the other list.
if (registeredListBox.SelectedItem != null) {
scheduleListBox.SelectedItem = null;
}
// reset the enabled state of the buttons
ResetButtons();
}
// event handling method for the ssn TextBox
public void SsnTextBoxKeyUp(object source, KeyEventArgs e) {
// We only want to act if the Enter key is pressed
if ( e.KeyCode == Keys.Enter ) {
// First, clear the fields reflecting the
// previous student's information.
ClearFields();
// We'll try to construct a Student based on
// the ssn we read, and if a file containing
// Student's information cannot be found,
// we have a problem.
currentUser = new Student(ssnTextBox.Text);
// Test to see if the Student fields were properly
// initialized. If not, reset currentUser to null
// and display a message box
if (!currentUser.StudentSuccessfullyInitialized()) {
// Drat! The ID was invalid.
currentUser = null;
// Let the user know that login failed,
string message = "Invalid student ID; please try again.";
MessageBox.Show(message, "Invalid Student ID",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else {
// Hooray! We found one! Now, we need
// to request and validate the password.
passwordDialog = new PasswordForm();
passwordDialog.ShowDialog(this);
string password = passwordDialog.Password;
passwordDialog.Dispose();
if (currentUser.ValidatePassword(password)) {
// Let the user know that the
// login succeeded.
string message =
"Log in succeeded for " + currentUser.Name + ".";
MessageBox.Show(message, "Log In Succeeded",
MessageBoxButtons.OK, MessageBoxIcon.Information);
// Load the data for the current user into the TextBox and
// ListBox components.
SetFields(currentUser);
}
else {
// The ssn was okay, but the password validation failed;
// notify the user of this.
string message = "Invalid password; please try again.";
MessageBox.Show(message, "Invalid Password",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
// Check states of the various buttons.
ResetButtons();
}
}
// These are private housekeeping methods
// Because there are so many different situations in which one or
// more buttons need to be (de)activated, and because the logic is
// so complex, we centralize it here and then just call this method
// whenever we need to check the state of one or more of the buttons.
// It is a tradeoff of code elegance for execution efficiency:
// we are doing a bit more work each time (because we don't need to
// reset all four buttons every time), but since the execution time
// is minimal, this seems like a reasonable tradeoff.
private void ResetButtons() {
// There are four conditions which collectively govern the
// state of each button:
//
// o Whether a user is logged on or not.
bool isLoggedOn;
if (currentUser != null) {
isLoggedOn = true;
}
else {
isLoggedOn = false;
}
// o Whether the user is registered for at least one course.
bool atLeastOne;
if (currentUser != null && currentUser.GetCourseTotal() > 0) {
atLeastOne = true;
}
else {
atLeastOne = false;
}
// o Whether a registered course has been selected.
bool courseSelected;
if (registeredListBox.SelectedItem == null) {
courseSelected = false;
}
else {
courseSelected = true;
}
// o Whether an item is selected in the Schedule of Classes.
bool catalogSelected;
if (scheduleListBox.SelectedItem == null) {
catalogSelected = false;
}
else {
catalogSelected = true;
}
// Now, verify the conditions on a button-by-button basis.
// Drop button:
if (isLoggedOn && atLeastOne && courseSelected) {
dropButton.Enabled = true;
}
else {
dropButton.Enabled = false;
}
// Add button:
if (isLoggedOn && catalogSelected) {
addButton.Enabled = true;
}
else {
addButton.Enabled = false;
}
// Save My Schedule button:
if (isLoggedOn) {
saveButton.Enabled = true;
}
else {
saveButton.Enabled = false;
}
// Log Off button:
if (isLoggedOn) {
logOffButton.Enabled = true;
}
else {
logOffButton.Enabled = false;
}
}
// Called whenever a user is logged off.
private void ClearFields() {
nameTextBox.Text = "";
totalTextBox.Text = "";
registeredListBox.Items.Clear();
}
// Set the various fields, lists, etc. to reflect the information
// associated with a particular student. (Used when logging in.)
private void SetFields(Student theStudent) {
nameTextBox.Text = theStudent.Name;
int total = theStudent.GetCourseTotal();
totalTextBox.Text = ""+total;
// If the student is registered for any courses, list these, too.
if (total > 0) {
// Use the GetEnrolledSections() method to obtain a list
// of the sections that the student is registered for and
// add the sections to the registered ListBox
IEnumerator e = theStudent.GetEnrolledSections();
while ( e.MoveNext() ) {
registeredListBox.Items.Add((Section)e.Current);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -