-1

I'm sorry if the question is vague but my form won't run. There's no errors or anything it just wont even load. Is their something wrong with my code or computer? All of the files are located here. I've never gotten anything like this. like I said I'm not getting any error messages it just wont run.

/* Program      :   Ch12Ex12
 * Programmer   :   Chase Mitchell
 * Date         :   4/21/2016
 * Description  :   Lets Make A Zonk
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ch12Ex13
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        Random rnd = new Random();



            string[] prises=new string[3]{"trip to japan!","Zonk","Cruise"};

            int x = 0; 
            int pr1=0, pr2=0, pr3=0,val1=0,val2=0,val3=0;


            int Cur1 = rnd.Next(1, 3);
            int Cur2 = rnd.Next(1, 3);
            int Cur3 = rnd.Next(1, 3);

            do
            {
                if (Cur1 == Cur2)
                {
                    Cur2 = rnd.Next(1, 3);
                }
                else if (Cur3 == Cur1 || Cur3 == Cur2)
                    Cur3 = rnd.Next(1, 3);
                else
                    x = 2;

            } while (x == 0);


            if (Cur1 == 1)
            {
                picP1.Location = new Point(26, 35);
                pr1 = 1;
                val1 = 3;
            }
            else if (Cur2 == 1)
            {
                picP2.Location = new Point(26, 35);
                pr2 = 1;
                val1 = 1;
            }
            else if (Cur3 == 1)
            {
                picP3.Location = new Point(26, 35);
                pr3 = 1;
                val1 = 2;
            }


            if (Cur1 == 1)
            {
                picP1.Location = new Point(275, 35);
                pr1 = 2;
                val2 = 3;
            }
            else if (Cur2 == 1)
            {
                picP2.Location = new Point(275, 35);
                pr2 = 2;
                val2 = 1;
            }
            else if (Cur3 == 1)
            {
                picP3.Location = new Point(275, 35);
                pr3 = 3;
                val2 = 2;
            }
            if (Cur1 == 1)
            {
                picP1.Location = new Point(543, 35);
                pr1 = 3;
                val3 = 3;
            }
            else if (Cur2 == 1)
            {
                picP2.Location = new Point(543, 35);
                pr2 = 3;
                val3 = 1;
            }
            else if (Cur3 == 1)
            {
                picP3.Location = new Point(543, 35);
                pr3 = 3;
                val3 = 2;
            }

            lblP1.Text = prises[pr1];
            lblP2.Text = prises[pr2];
            lblP3.Text = prises[pr3];

            lblV1.Text = Convert.ToString(val1);
            lblV2.Text = Convert.ToString(val2);
            lblV3.Text = Convert.ToString(val3);

        }

        private void btnP1_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lblTest) == 1)
            {
                if (Convert.ToInt32(lblV2.Text) < Convert.ToInt32(lblV3.Text))
                {
                    btnP2.Enabled = false;
                    picC2.Visible = false;
                }
                else if (Convert.ToInt32(lblV2.Text) > Convert.ToInt32(lblV3))
                {
                    btnP3.Enabled = false;
                    picC3.Visible = false;
                }

                lblOutput.Text = ("You missed out on a "+lblP1.Text);
                lblMore.Text = ("Would you like to change your curtain? (click the same curtain again if you want to keep it)");

            }
            else
            {


            }

        }




}
}

3 Answers3

4

The values of pr1, pr2 and pr3 variables used as indexes for the prises array cannot go from 1 to 3 but from 0 to 2 because you have only three strings in the array at position 0, 1 and 2

Using index 3 triggers an Index Out Of Range exception during the Form_Load event and this is a well known situation where a program compiled in 64 bit mode and in debug mode fails to start.
If you try to execute the program from the BIN\DEBUG folder outside Visual Studio you should be able to see the exception.

A very detailed answer and links to other documentation about the problem could be found at this QA

VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows

Of course, I assume that you have not changed the default behavior of Visual Studio when building a WinForm project. This project's template creates a Program class in the Program.cs file where a Main method starts the first form of your application using a line like this

 `Application.Run(new Form1());`

If this line is missing and in the project properties you don't have set the Form1 as the startup form then no form will be called and displayed.

NOTE: Looking at your code it seems that you have copy/pasted the checks on Cur1, Cur2 and Cur3. The code checks always for the value ==1, I think you have forgot to change it to ==2 and ==3

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
1

You can replace your code from this:

 rnd.Next(1, 3);

to

 rnd.Next(0, 2);

also need to update all the hard code values from 1-3 to 0-2 to avoid out of range exception.

sumngh
  • 566
  • 2
  • 10
  • No this will not avoid the out of range exception. It is the settings of the variables `prX` to 3 that triggers the exception – Steve Apr 28 '16 at 12:12
0

There is nothing wrong with your code as far as the problem you mentioned is concerned. I suggest you check if a Main method exists in your program.cs and it should call Application.Run(new Form1).

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
Mukesh Adhvaryu
  • 642
  • 5
  • 16
  • This should have been a comment. – Mixxiphoid Apr 28 '16 at 12:52
  • 2
    In the future please remember that answers are mean to answer the question. Meta commentary about voting is irrelevant and distracts readers from your actual answer. I've edited your answer to make your solution more obvious. – ryanyuyu Apr 28 '16 at 13:05