Question
C#: I am working on this application and keep running into this error. Please help me fix it. Err...
C#: I am working on this application and keep running into this error. Please help me fix it.
Error:
Design:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SinghAjayProgram08
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double totalInserted = 0;
double totalWon = 0;
private void spinButton_Click(object sender, EventArgs e)
{
Random rand = new Random();
int index = rand.Next(fruitsImageList.Images.Count);
firstPictureBox.Image = fruitsImageList.Images[index];
secondPictureBox.Image = fruitsImageList.Images[index];
thirdPictureBox.Image = fruitsImageList.Images[index];
int pictureOne = rand.Next(0, 10);
int pictureTwo = rand.Next(0, 10);
int pictureThree = rand.Next(0, 10);
double amount = double.Parse(amountTextBox.Text);
totalInserted += amount;
// Picture one
if (pictureOne == 0)
{
firstPictureBox.Image = Image.FromFile("Apple.bmp");
}
else if (pictureOne == 1)
{
firstPictureBox.Image = Image.FromFile("Banana.bmp");
}
else if (pictureOne == 2)
{
firstPictureBox.Image = Image.FromFile("Cherries.bmp");
}
else if (pictureOne == 3)
{
firstPictureBox.Image = Image.FromFile("Grapes.bmp");
}
else if (pictureOne == 4)
{
firstPictureBox.Image = Image.FromFile("Lemon.bmp");
}
else if (pictureOne == 5)
{
firstPictureBox.Image = Image.FromFile("Lime.bmp");
}
else if (pictureOne == 6)
{
firstPictureBox.Image = Image.FromFile("Orange.bmp");
}
else if (pictureOne == 7)
{
firstPictureBox.Image = Image.FromFile("Pear.bmp");
}
else if (pictureOne == 8)
{
firstPictureBox.Image = Image.FromFile("Strawberry.bmp");
}
else if (pictureOne == 9)
{
firstPictureBox.Image = Image.FromFile("Watermelon.bmp");
}
// Picture two
if (pictureTwo == 0)
{
secondPictureBox.Image = Image.FromFile("Apple.bmp");
}
else if (pictureTwo == 1)
{
secondPictureBox.Image = Image.FromFile("Banana.bmp");
}
else if (pictureTwo == 2)
{
secondPictureBox.Image = Image.FromFile("Cherries.bmp");
}
else if (pictureTwo == 3)
{
secondPictureBox.Image = Image.FromFile("Grapes.bmp");
}
else if (pictureTwo == 4)
{
secondPictureBox.Image = Image.FromFile("Lemon.bmp");
}
else if (pictureTwo == 5)
{
secondPictureBox.Image = Image.FromFile("Lime.bmp");
}
else if (pictureTwo == 6)
{
secondPictureBox.Image = Image.FromFile("Orange.bmp");
}
else if (pictureTwo == 7)
{
secondPictureBox.Image = Image.FromFile("Pear.bmp");
}
else if (pictureTwo == 8)
{
secondPictureBox.Image = Image.FromFile("Strawberry.bmp");
}
else if (pictureTwo == 9)
{
secondPictureBox.Image = Image.FromFile("Watermelon.bmp");
}
// Picture three
if (pictureThree == 0)
{
thirdPictureBox.Image = Image.FromFile("Apple.bmp");
}
else if (pictureThree == 1)
{
thirdPictureBox.Image = Image.FromFile("Banana.bmp");
}
else if (pictureThree == 2)
{
thirdPictureBox.Image = Image.FromFile("Cherries.bmp");
}
else if (pictureThree == 3)
{
thirdPictureBox.Image = Image.FromFile("Grapes.bmp");
}
else if (pictureThree == 4)
{
thirdPictureBox.Image = Image.FromFile("Lemon.bmp");
}
else if (pictureThree == 5)
{
thirdPictureBox.Image = Image.FromFile("Lime.bmp");
}
else if (pictureThree == 6)
{
thirdPictureBox.Image = Image.FromFile("Orange.bmp");
}
else if (pictureThree == 7)
{
thirdPictureBox.Image = Image.FromFile("Pear.bmp");
}
else if (pictureThree == 8)
{
thirdPictureBox.Image = Image.FromFile("Strawberry.bmp");
}
else if (pictureThree == 9)
{
thirdPictureBox.Image = Image.FromFile("Watermelon.bmp");
}
if (pictureOne == pictureTwo && pictureOne == pictureThree)
{
totalWon += amount * 3;
MessageBox.Show("You Won" + (amount * 3).ToString("c"));
}
else if (pictureOne == pictureTwo || pictureOne == pictureThree
|| pictureTwo == pictureThree)
{
totalWon += amount * 2;
MessageBox.Show("You Won" + (amount * 2).ToString("c"));
}
else
{
MessageBox.Show("You Won" + 0.ToString("c"));
}
}
private void exitButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Total Inserted" + " amount is" + totalInserted.ToString("c")
+ "\nTotal Winning amount is" + totalWon.ToString("c"));
this.Close();
}
}
}
Assignment:
Form 1.cs Form1.cs [Design] x Slot Machine Amount Inserted: Spin Exit 囤fruitsimageList
A slot machine is a gambling device into which the user inserts money and then pulls a lever (or presses a button). The slot machine then displays a set of random images. If two or more of the images match, the user wins an amount of money that the slot machine dispenses back to the user Create an application that simulates a slot machine. Figure shows an example of how the form should look. The application should let the user enter into a TextBox the amount of money he or she is inserting into the machine. When the user clicks the Spin button, the application should display three randomly selected symbols. (Slot machines traditionally display fruit symbols. You will find a set of fruit symbols in the ImageslFruit Symbols folder of the Student Sample Programs.) Figure Slot Machine application cation should let he user enter into a TexnBo the amount of money he or Slot Machine Amourt Ineerted S Spin If none of the randomly displayed images match, the program should inform the user that he or she has won SO. If two of the images match, the program should inform the user that he or she has won two times the amount entered. If three of the images match, the program should inform the user that he or she has won three times the amount entered. When the user clicks the Exit button to exit the application, the program should display the total amount of money entered into the slot machine and the total amount won.
Answers
You are getting this error because you are not catching an unchecked expression in your code. The type of Exception is FileNotFoundException, this type of exception happens when the file you are trying to open is not found.
I would first suggest you to first make sure the file you are trying to exits actually exist.
And, moreover you would also need to catch the required exception as shown below.
try {
firstPictureBox.Image = Image.FromFile("Banana.bmp");
} catch(FileNotFoundException e) {
Console.WriteLine("Exception caught: {0}", e);}