In this lab you will create a program to create a character for a Role Playing Game (RPG). This will be a Windows Form application in which a user may create a character for their favorite game.
Create a new Windows Form project to hold your code.
For the main window do the following.
Acceptance Criteria
1.Solution opens properly in Visual Studio and loads all projects.
2.Project is properly named in repository.
3.Main window displays properly.
4.Main window cannot be resized smaller than the given value.
Implement a command to exit the program.
Create a new menu item for FileExit with appropriate accelerator keys. The command should be assigned the shortcut key of Alt+F4. When the command is executed close the main window and terminate the application.
Acceptance Criteria
1.Menu option is available to exit program.
2.Selecting option closes program.
Implement the command to show help information.
Create a new menu item for HelpAbout with appropriate accelerator keys. The command should be assigned the shortcut key of F.
When the command is executed show an About form centered in the parent. The form should contain your name, course and name of lab (e.g. Character Creator). The form should provide a button to close it.
Acceptance Criteria
1.Menu option is available to show help.
2.Selecting option shows About form.
3.Form shows correct data.
4.Form can be closed using button.
Add support for collecting character information.
Create the new project.
This project will be the business layer for the application. It will house any logic that is not tied to a UI. The UI project will rely on this.
This project will be the business layer for the application. It will house any logic that is not tied to a UI. The UI project will rely on this.
Note: Do not add any UI elements to this project.
Rename the auto-generated class to Character or delete it and create a new class file. The type will contain the data needed to "define" a character. The following attributes define what a character is.
Ensure that the class is properly documented and follows the general guidelines outlined in class.
Note: If you already have a favorite RPG then you may optionally choose to use the appropriate profession/race/attributes/range from the RPG. However you must provide multiple options for profession and race, you must have 5 attributes and they must define a lower and upper bound. Any variant in ranges should be documented in the UI.
Allow the user to create a new character.
Create a new menu item for CharacterNew with appropriate accelerator keys. The command should be assigned the shortcut key of Ctrl+N.
When the command is executed show a form to collect the character information. The form will have the following attributes. - The title will be Create New Character. - The form will be centered on the parent. - The form will not be resizable and will appropriately fit the contents. - The form will not have an icon, minimize or maximize buttons. - When validating the user should be able to navigate to other controls but an error should be shown next to the invalid fields. - Ensure all controls and labels are lined up properly.
The form will display the fields from the Character class defined earlier. For each field.
Heroes are not average people so set each attribute to an initial value of 50. Note: If you are using your own RPG then you may use substitute profession/race values.
The combo boxes should not allow freeform text. Only the pre-defined options should be available.
The form will have a Save button that will save the character. If there is any validation issues then prevent the form from closing.
The form will return the newly created character. Create an instance of the Character class to store the character information and provide it back to the main form.
The form will have a Cancel button that will cancel the creation. No validation is done and no changes should be made.
Acceptance Criteria
1.When selected the form is shown.
2.As validation errors occur they are shown to the user.
3.User cannot save if there are validation errors.
4.Saving saves the character.
5.Cancel closes the form without validation and without making any changes.
Display the roster of characters that have been defined in the main window.
Allow the creation of a roster of characters by using an array to store the characters in the main form. Each time a new character is created it should be added to the array. Set the array size to a value of 100 so it is unlikely the array will fill.
Note: You do not have to code for the scenario where the array is full.
Display the roster in the main form. Use a ListBox that displays the character's name. Ensure that the items in the list box are associated with the character data so it can be used in a later story.
The control should resize as the form is resized to allow for a user to see the entire roster, if necessary.
Acceptance Criteria
1.List of characters are shown in the main form.
2.If a character is added then it appears in the main form.
3.The list resizes as the main form is resized.
Allow the user to edit an existing character.
Create a new menu item for CharacterEdit with appropriate accelerator keys. The command should be assigned the shortcut key of Ctrl+O.
When the command is selected get the currently selected character from the roster. If no character is selected then do nothing.
If a character is selected then show show the character form again with the selected character information already populated. The form will behave the same as when creating a new character with the following exceptions.
The roster should be refreshed after saving to reflect any changes in the name.
Acceptance Criteria
1.Selecting a character in the roster and then excuting the command properly displays the character in the form.
2.Saving the character updates the existing character.
3.Roster is refreshed after save.
4.Cancelling the edit does not change data.
Allow the user to delete an existing character.
Create a new menu item for CharacterDelete with appropriate accelerator keys. The command should be assigned the shortcut key of Del.
When the command is selected get the currently selected character from the roster. If no character is selected then do nothing.
If a character is selected then display a confirmation message that asks if the user wants to delete the given character. Include the character name in the message.
The roster should be refreshed after saving to reflect any changes in the name.
Acceptance Criteria
1.Selecting a character in the roster and then excuting the command prompts for confirmation with the character's name.
2.Confirming the message deletes the character and refreshes the list.
3.Cancelling the confirmation does not change anything.
Naming Conventions
Working with ComboBox
The Windows Forms ComboBox can be used to give the user a drop-down list of items to choose from. By default the user can either select an item from the list or enter a value. To only allow the user to select from a list use the DropDownStyleproperty and set it to DropDownStyle.DropDownList.
All the things you see in the ComboBox are stored in the Items property. This is a collection of Object instances. Thus it can store anything you want. For each item it will call the ToString method on the object to get the text to display. You may "bind" data to it a couple of different ways.
The first approach is to use strong types (e.g. your business objects). Store your data in a array as you might normally do. Assign the array to the control so it will render the elements using the Items property. The AddRange method accepts an array and assigns each of the elements to the control. Alternatively you could use a foreach to iterate through the array and add each item to the list. With the data assigned it will then render by calling the ToString method. Use the DisplayMemberproperty to specify the name of the property that contains the value you want to use as the text instead.
class MyItem
{
public int Id { get; set; }
public string Name { get; set; }
}
//Later in the UI
private void LoadItems ( MyItem[] items )
{
combobox1.DisplayMember = "Name";
combobox1.Items.AddRange(items);
}
The other approach is to use the designer. In the designer you can use the smart tag on the control (or use the Propertieswindow and go to the Items property). In either case you get a textbox that allows you to enter text. Each line becomes a value in the control. This approach is good when you just want to display text but it cannot be used for more complex scenarios.
To get or set the selected value in the control you have a series of properties that you can use.
If you are using the strongly typed approach then use SelectedItem and cast to the appropriate type.
var item = combobox1.SelectedItem as MyItem;
if (item != null)
...
If you are using the string approach then use the Text property that is already available on all controls.
Working with ListBox
A ListBox is useful for working with lists of items. While you can use simple strings it is really designed for objects. Like the ComboBox it sees everything as Object. Use the DisplayMember property to determine which property on your class to display.
The SelectedItem property can be used to get the selected item. Remember to typecast it as needed.
The ListBox can support multiple selection as well. This is controlled by the SelectionMode property. If using multi-select then use the SelectedItems property instead of SelectedItem to get the list of items.