Limited Offer Get 25% off — use code BESTW25
No AI No Plagiarism On-Time Delivery Free Revisions
Claim Now

Third GUI

After this lab, students will be able to:

  1. Write an application that takes user input from .NET controls
  2. Write code to convert user input into a valid SQL string
  3. Validate user input
  4. Use MessageBoxes to display strings

Follow these instructions carefully to complete your third GUI assignment. If you have questions at any time, don’t hesitate to raise your hand during lab or to stop by my office.

You’ll do this lab in a pair (or individually, if you don’t make it to lab). As with FirstGUI, you will both be at the same computer. One student will be the pilot, while the other will be the navigator. 

Forking the base repository

The steps to fork the repository are the same as earlier assignments, only with a different link: 

 You can find the instructions for forking the repository in FirstGUI. Make sure both people are in the team, if you’re working in a pair.

Take a few moments to look at the GUI in the designer and in the XAML file. Does everything make sense? You should be able to run the application, showing a visible, but not interactive user interface.

Creating a Button Event

In order to make the interface interactive, we need to add an event. Specifically, we are going to

Take a few moments to look at the GUI in the designer and in the XAML file. Does everything make sense? You should be able to run the application, showing a visible, but not interactive user interface.

Creating a Button Event

In order to make the interface interactive, we need to add an event. Specifically, we are going to add a Click event to a Button. To do this, double click on the Submit button in the GUI Designer. This will add some code to the XAML file and open the source code to a method that will be called when the button is clicked

Creating a MessageBox

The easiest way to create some initial interactivity is with a MessageBox. This is simply a popup window that appears on the screen. They are also sometimes called modal dialog boxes. These can be useful for displaying error messages or debugging your user interface. To create a popup window, use the following code in your button event code:

Add this code inside the Button_Click method and run your program to see the MessageBox before moving on.

Getting User Input

Next, we need to be able to get user input. You’ll notice that there are two different areas that you can get user input from in this interface: Textboxes and ComboBoxes.

In order to access the contents of these controls, you need to give them unique names.

You can do this either via the XAML – by adding an x:Name attribute:

Or in the Properties Designer in the lower right corner of the Visual Studio interface:

You should use names that describe what is inside the control, rather than just MyTextBox. Do this for all of the textboxes and comboboxes in the interface.

Getting Input from a TextBox

Now that the controls have names, those names can be used as variables in event handlers. To access the input that has been typed into a textbox, use the Text property. For example, this line of code will store the input typed into the TextBox called MyTextBox into a variable called textboxText:

This variable can now be used anywhere a string can be.

Getting Input from ComboBoxes and ListBoxes

To get input from a ComboBox or ListBox, you’ll need to determine which ComboBoxItem or ListBoxItem is selected. You can do this using the following code snippet:

A couple of notes about this snippet:

First, the second line of code performs a cast of MyComboBox.SelectedItem. This needs to be done, because a ComboBox can store any kind of object. This could also be performed using this line of code:

ComboxItem selectedItem = (ComboBoxItem)MyComboBox.SelectedItem;

However, this code (which performs an explicit cast) is slower at runtime and will cause exceptions when trying to perform a conversion that is not allowed.

Second, the fourth line of code is required because selectedItem.Content returns an object, and thus can not be converted into a string. The ToString method performs this conversion, instead of a cast.

Finally, you can use a similar method for accessing ListBox content by replacing all instances of ComboBox with ListBox and all instances of ComboBoxItem with ListBoxItem in the code snippet. You won’t use this in the current assignment, but may need it in future assignments.

New Database

For the GUI assignments, we’ll be using a new database called lahman. It stores historical baseball statistics. You can access lahman from ”lahman2016.sqlite I put it with stiff. Load this database into sqliteonline like you have for the music store database. Explore the database for a bit, then answer this question.

Writing the code to directly access this database through C# will be in a future assignment. For now, you’ll need to create the SQL string that would be required to search for the baseball players that match the search terms in the GUI. You may find it useful to write the query in sqliteonline, making sure it works before attempting to do it in C#.

Your Turn

After the user clicks the Submit button, the program should…

  • Get the input from the “strikeouts” text box
  • Get the input from the “walks” text box
  • Assume the user meant a 0, if they left a text box blank
  • Handle invalid input (ex: text and negative numbers). I’ve written some method headers that will be useful when performing this operation.
  • Create a string storing an SQL query that answers the following scenario: “Display the players who match the values entered by the user. The final query should only display the playerID, the total number of walks and the total number of strikeouts in the player’s career.”
  • Hint: You will need to use an aggregate, a GROUP BY, and a HAVING clause.
  • Display either an error message or the SQL string in a MessageBox 

Here are some sample outputs, given various valid and invalid inputs. Your SQL query will

This is not an exhaustive list of error states. Make sure you handle the other scenarios properly.

Posting to Blackboard

Commit and push your code to GitHub, then go into our Blackboard course under Assignments. Click the ThirdGUI link. Type in your GitHub usernames and a link to the repository in the “Comments” box. If you don’t remember how to push to GitHub, check the instructions in FirstGUI. No other files are required. Congratulations on creating an interactive WPF application!

References

  • TextBox tutorial – https://www.wpf-tutorial.com/basic-controls/the-textblock-control/
  • RadioButton tutorial – https://www.wpf-tutorial.com/basic-controls/the-radiobutton-control/
  • CheckBox tutorial – https://www.wpf-tutorial.com/basic-controls/the-checkbox-control/
  • ComboBox tutorial – https://www.wpf-tutorial.com/list-controls/combobox-control/
  • ListBox tutorial – https://www.wpf-tutorial.com/list-controls/listbox-control/
  • MessageBox tutorial (shows other ways of using MessageBox) – https://www.wpf-tutorial.com/dialogs/the-messagebox/

The post Third GUI appeared first on My Assignment Online.

Plagiarism Free Assignment Help

Expert Help With This Assignment — On Your Terms

Native UK, USA & Australia writers Deadline from 3 hours 100% Plagiarism-Free — Turnitin included Unlimited free revisions Free to submit — compare quotes
Scroll to Top