- Adding Controls to the Form. - To make the form useful, you need to add controls and write some code of your own. - In the next exercise, you will add controls to the form that allow a user to input member details. - You will use a variety of different controls, each suited to a particular type of data entry.. - You will use TextBox controls for entering the first name and last name of the member.. - The form also records whether the member is the tower. - only rings one bell at a time, a group of bell ringers under the direction of the tower captain can ring their bells in different sequences and generally play simple music. - In this application, you will use a small selection of methods that you will hard-wire into the form. - (You will see how to use databases in the next part of the book.) A good control for displaying this information and indicating whether a member can ring a method is the CheckedListBox.. - The user can click Clear to reset the controls on the form and cancel any data entered.. - Ensure that Form1 is displayed in the Designer View window. - (If the Toolbox is not displayed, click Toolbox from the View menu, or click the Toolbox tab in the left-hand border of Visual Studio.) 2. - In the Properties window, click the Location property, and then type 10,40 to set. - the Location property of the label.. - From the Toolbox, drag a TextBox control onto MemberForm, to the right of the label. - Do not worry about aligning the TextBox exactly because you will set the Location property for this and the following controls later.. - Add a second Label to the form. - Place it to the right of the TextBox.. - Add another TextBox to MemberForm and position it to the right of the second Label.. - From the Toolbox, drag a third Label onto the form. - From the Toolbox, drag a ComboBox control onto the form. - Place it on MemberForm under the first TextBox and to the right of the third Label.. - From the Toolbox, drag a CheckBox control onto the form and place it under the second TextBox.. - From the Toolbox, drag a DateTimePicker control and place it under the ComboBox.. - In the Toolbox, expand the Containers category. - From the Common Controls category in the Toolbox, drag the RadioButton control and place it inside the GroupBox control you just added.. - Add three more RadioButton controls, vertically aligned with each other, to the GroupBox. - From the Toolbox, drag a CheckedListBox control and place it under the second Label and to the right of the GroupBox control.. - Add another Button control to the bottom of the form, just to the right of the first.. - You now need to set the properties of the controls you just added to the form. - To change the value of a control's property, click the control on the form to select it, and then enter the correct value in the Properties window. - You will start with the basic properties. - The following table lists the properties and values you need to assign to each of the controls.. - DropDownList (This setting forces users to pick one of the items in the list. - MiddleLeft (This property specifies the location of the checkbox relative to the text in the control. - When you click the drop-down arrow for this property, an. - Click the left square in the middle row.). - You can click the drop-down arrow in this property to display a simple text editor that also allows you to enter multi-line text values). - Location 16, 32 (Note that this location is relative to the radio button's container, the experience GroupBox.) Text Up to 1 year. - If you want to learn more about the different properties available for each type of control, you can find a list of them in the MSDN Library for Visual Studio 2005 supplied with Visual Studio 2005.. - When the form runs, it would be useful to reset the value of each control to an initial default value. - To do this, you will need to write some code (at last). - In the following exercises, you will create a private method called Reset. - Later, you will invoke the Reset method when the form first starts, and when the user clicks the Clear button.. - Rather than coding the method from scratch, you will use the Class Diagram editor to generate the method. - Create the Reset method. - In the Solution Explorer, right-click Form1.cs. - Right-click the MemberForm class in the diagram, point to Add, and then click Method. - In the Class Details pane that appears underneath the class diagram, verify that the Type of the Reset method is void, and that the Modifier is public. - If they are wrong, you can click these fields in the Class Details pane and modify them.. - If the Class Details pane is not visible, click the Class Details tab below the Error List pane to display it.. - In the Class Details pane that appears underneath In the Class Diagram, right-click the Reset method and then click View Code.. - You are placed in the Code and Text Editor window displaying the MemberForm class. - The Reset method has been added with a default implementation that throws a NotImplementedException:. - In the Code And Text Editor window, replace the throw statement in the Reset method with the following lines of code:. - firstName.Text . - lastName.Text . - You now need to configure the properties of the remaining controls on the form. - You will do this programmatically.. - If you recall, the towerName ComboBox will contain a list of all the bell towers in the Middleshire district. - A ComboBox has a property called Items that contains a list of the data to be displayed.. - In the Reset method, after the code you have already written, add the following. - statements to clear this list (this is important because otherwise you would end up with many duplicate values in the list) and create four items in the ComboBox:. - towerNames.Items.Clear();. - towerNames.Items.Add("Great Shevington");. - towerNames.Items.Add("Little Mudford");. - towerNames.Items.Add("Upper Gumtree");. - towerNames.Items.Add("Downley Hatch");. - The next step is to initialize the memberSince DateTimePicker control to the current date.. - You can obtain the current date by using the static property Today of the DateTime class. - Add the following statement to the Reset method:. - The form contains four radio buttons that indicate the number of years of bell ringing experience the member has. - By default, none of the buttons will be selected. - You should rectify this by setting the Checked property of the novice radio button. - However, as before, you will supply some hard-coded values for now. - Complete the Reset method by adding the following code:. - methods.Items.Clear();. - methods.Items.Add("Canterbury Minimus");. - methods.Items.Add("Reverse St Nicholas");. - methods.Items.Add("Plain Bob Doubles");. - methods.Items.Add("Grandsire Doubles");. - methods.Items.Add("Cambridge Minor");. - methods.Items.Add("Old Oxford Delight Minor");. - methods.Items.Add("Kent Treble Bob Major");. - Call the Reset method. - You need to arrange for the Reset method to be called when the form is first displayed. - A good place to do this is in the MemberForm constructor. - In the Code And Text Editor window, scroll to the beginning of the MemberForm class in the file Form1.cs, and find the constructor (it is called MemberForm, just like the class). - Insert a call to the Reset method after the statement that calls the InitializeComponent method:. - It is a good practice to name the file containing a form after the form itself. - In the Solution Explorer, right-click Form1.cs, click Rename, and then type. - When the form runs, click the Tower ComboBox.. - You will see the list of bell towers, and you can select one of them.. - Click the drop-down arrow on the right side of the Member Since date/time picker.. - You will be presented with a calendar of dates. - You can also click the month name to display the months as a drop-down list, and click the year to allow you to select a year using a numeric up-down control.. - Click each of the radio buttons in the Experience group.. - In the Methods list box, click some of the methods and select the corresponding check box. - You will have to click once to select a method and a second time to select or clear the checkbox.. - Close the form and return to Visual Studio 2005.
Xem thử không khả dụng, vui lòng xem tại trang nguồn hoặc xem
Tóm tắt