public PersonEditForm(Person person, Form parentForm) { InitializeComponent(); foreach (Control control in this.Controls) { control.Tag = false; } if (parentForm != null) { this.Owner = parentForm; this.CenterToParent(); } this.formPerson = person; this.okButton.Enabled = false; this.nameText.Validating += new CancelEventHandler(this.TxtBoxEmpty__Validating); this.emailText.Validating += new CancelEventHandler(this.TxtBoxEmpty__Validating); this.ageText.Validating += new CancelEventHandler(this.TxtBoxEmpty__Validating); this.licText.Validating += new CancelEventHandler(this.TxtBoxEmpty__Validating);; this.gpaText.Validating += new CancelEventHandler(this.TxtBoxEmpty__Validating);; this.specText.Validating += new CancelEventHandler(this.TxtBoxEmpty__Validating); this.nameText.TextChanged += new EventHandler(this.TxtBoxEmpty__TextChanged); this.emailText.TextChanged += new EventHandler(this.TxtBoxEmpty__TextChanged); this.ageText.TextChanged += new EventHandler(this.TxtBoxEmpty__TextChanged); this.licText.TextChanged += new EventHandler(this.TxtBoxEmpty__TextChanged);; this.gpaText.TextChanged += new EventHandler(this.TxtBoxEmpty__TextChanged);; this.specText.TextChanged += new EventHandler(this.TxtBoxEmpty__TextChanged); /* * KeyPress Event for TextBox fields * Occurs when the user presses a key sequence which generates a character(shift + A for example) within the control * Accepts the KeyPressEventHandler() delegate, whose method must have the following signature: * private void ObjectName__KeyPress(object sender, KeyPressEventArgs e) * * Example for adding the delegate method: * this.objectName.KeyPress += new KeyPressEventHandler(this.ObjectName__KeyPress); * * Important Fields in sender: * TextBox tb = (TextBox)sender; * tb.Text: the current text in the TextBox * * Important Fields in KeyPressEventArgs * e.KeyChar: gets or sets the character just pressed allowing you to change, suppress or pass - through each character * e.Handled: a boolean to indicate whether the delegate's method "handled" the keypress. If it is set to true, then .NET will not process the keypress (ie. the keyboard buffer will be cleared). */ // finish coding the TxtNum__KeyPress function below, which enforces digits-only in the Age, License and GPA fields, // and allows 1 decimal point in the GPA field. // using the example code on line #54, add the KeyPressEventHandler delegate function TxtNum__KeyPress for the following 3 numeric fields: this.ageText.KeyPress += new KeyPressEventHandler(this.TxtNum__KeyPress); this.licText.KeyPress += new KeyPressEventHandler(this.TxtNum__KeyPress); this.gpaText.KeyPress += new KeyPressEventHandler(this.TxtNum__KeyPress); /* * SelectedIndexChanged Event for ComboBox Controls * Occurs when the user changes the ComboBox value * Accepts the empty EventHandler() delegate because the event is limited to only the current control. * * Example for adding the delegate method: * this.objectName.SelectedIndexChanged += new EventHandler(this.ObjectName__SelectedIndexChanged); * * The EventHandler delegate method must have the following signature: * private void ObjectName__SelectedIndexChanged(object sender, EventArgs e) * * Important Fields in sender * ComboBox cb = (ComboBox) sender; * cb.SelectedIndex: the 0-based index of the selected item * cb.SelectedItem: the string of the display value of the selected item * * Important Fields in EventArgs * None. */ // finish coding the TypeComboBox__SelectedIndexChanged delegate function below based on the comments // using line #81 add the Event Handler to the typeComboBox control this.typeComboBox.SelectedIndexChanged += new EventHandler(this.TypeComboBox__SelectedIndexChanged); /* Increase the size of the form by changing: * MaximumSize: 842,478 * Size: 842,478 * * Place 7 radio buttons on the form to the right of the Combo Box, Name and Email fields. * They should be defined as: * (Name): himRadioButton herRadioButton themRadioButton * Text: Him Her Them * * (Name): froshRadioButton sophRadioButton juniorRadioButton seniorRadioButton * Text: Freshman Sophomore Junior Senior * * Run the application, and notice how you can only select 1 Radio Button at a time. * We want to be able to select 1 Gender Radio Button, and 1 Class Year Radio Button. * * We can do this by putting the Radio Buttons in Group Boxes. * * Add a GroupBox control to the form: * (Name): genderGroupBox * Text: Gender * * Drag the 3 Gender Radio Buttons onto the Group Box. * * Run the application again and notice how you can now select one Gender and one Class Radio Button. * * Your next homework will be to add Radio Buttons to the form. * * The CheckedChanged Event for Radio Buttons * Occurs when the Checked status changes. * Note that this will be called when the currently selected RadioButton unchecks and the new RadioButton becomes checked. * * Accepts the empty EventHandler() delegate because the event is limited to only the current control. * * Example for adding the delegate method: * this.objectName.CheckedChanged += new EventHandler(this.ObjectName__CheckedChanged); * * The EventHandler delegate method must have the following signature: * private void ObjectName__CheckedChanged(object sender, EventArgs e) * * Important Fields in sender * RadioButton rb = (RadioButton)sender; * rb.Checked: the current checked state of the RadioButton control. * * Important Fields in EventArgs * None. */ // finish the GenderRadioButton__CheckedChanged delegate function below // and using the code example on line #130, add the EventHandler to the Radio Buttons GroupBox genderGroupBox = new GroupBox(); GroupBox classGroupBox = new GroupBox(); //size, location, text and tab for gender genderGroupBox.Size(90, 90); genderGroupBox.Location(454, 53); genderGroupBox.Text = "Gender"; genderGroupBox.TabIndex = 6; //size, location, text and tab for class classGroupBox.Size(155, 136); classGroupBox.Location(454, 53); classGroupBox.Text = "Class"; classGroupBox.TabIndex = 7; //radio buttons for gender RadioButton himRadioButton = new RadioButton(); RadioButton herRadioButton = new RadioButton(); RadioButton themRadioButton = new RadioButton(); //text for the radio buttons himRadioButton.Text = "Him"; herRadioButton.Text = "Her"; themRadioButton.Text = "Them"; //adding them to the gender box genderGroupBox.Controls.Add(himRadioButton); genderGroupBox.Controls.Add(herRadioButton); genderGroupBox.Controls.Add(themRadioButton); RadioButton froshRadioButton = new RadioButton(); RadioButton sophRadioButton = new RadioButton(); RadioButton juniorRadioButton = new RadioButton(); RadioButton seniorRadioButton = new RadioButton(); froshRadioButton.Text = "Freshman"; sophRadioButton.Text = "Sophomore"; juniorRadioButton.Text = "Junior"; seniorRadioButton.Text = "Senior"; genderR classGroupBox.Controls.Add(froshRadioButton); classGroupBox.Controls.Add(sophRadioButton); classGroupBox.Controls.Add(juniorRadioButton); classGroupBox.Controls.Add(seniorRadioButton); Label classOfLabel = new Label(); classOfLabel.Size(143, 23); classOfLabel.Text = "Class of NNNN"; classOfLabel.TextAlign = CenterToScreen; this.himRadioButton.CheckedChanged += new EventHandler(this.GenderRadioButton__CheckedChanged); this.herRadioButton.CheckedChanged += new EventHandler(this.GenderRadioButton__CheckedChanged); this.themRadioButton.CheckedChanged += new EventHandler(this.GenderRadioButton__CheckedChanged); this.froshRadioButton.CheckedChanged += new EventHandler(this.ClassRadioButton__CheckedChanged); this.sophRadioButton.CheckedChanged += new EventHandler(this.ClassRadioButton__CheckedChanged); this.juniorRadioButton.CheckedChanged += new EventHandler(this.ClassRadioButton__CheckedChanged); this.seniorRadioButton.CheckedChanged += new EventHandler(this.ClassRadioButton__CheckedChanged); this.okButton.Click += new EventHandler(this.OkButton__Click); this.cancelButton.Click += new EventHandler(this.CancelButton__Click); this.nameText.Text = person.name; this.emailText.Text = person.email; this.ageText.Text = person.age.ToString(); this.licText.Text = person.LicenseId.ToString(); if (person.GetType() == typeof(Student)) { // initialize the typeComboBox SelectedIndex to 0 (Student) this.typeComboBox.SelectedIndex = 0; Student student = (Student)person; this.gpaText.Text = student.gpa.ToString(); } else { this.typeComboBox.SelectedIndex = 1; Teacher teacher = (Teacher)person; this.specText.Text = teacher.specialty; } this.Show(); }