user3185653
user3185653

Reputation: 39

Form not being displayed in c#

I am having a program.cs that is something like this :

namespace SumSwamp
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

But when I run the program it executes without any errors But the form1 is not being displayed. Please tell me if am doing something wrong

Here is my form class.

Upvotes: 0

Views: 92

Answers (2)

Dion V.
Dion V.

Reputation: 2120

From your posted code, your error lies in this piece;

public static Boolean WaitForRoll = true;
public static int Turn = 0;

public Form1()
{
    InitializeComponent();
    this.Load += new EventHandler(this.Form1_Load);
    while(Turn == 0) //always true
    {
        if (WaitForRoll==false) //always false
        {
            //never reached code
            DieTotal=DieLargeNum;
            Random rnd1 = new Random();
            DieLargeNum = rnd1.Next(1, 7);
            if (DieTotal>DieLargeNum)
            {
                Turn = 1;
                labelStatus.Text = "Player 1's Turn";
                WaitForRoll=true;
            }
            else
            {
                Turn = 2;
                labelStatus.Text = "Player 2's Turn";
                WaitForRoll = false;
            }
        }
    }

    //...
}

Look at it closely and you will find that your code never leaves the first while loop, and thus the constructor never comes to an end resulting in the Form1 object never to be created.

Some tips;

  • Rethink your design. You should not place this code in your constructor, but in methods.
  • Read up on while loops. They are a pain if used incorrectly, which you did.

Upvotes: 2

SiD
SiD

Reputation: 521

Your Form1 is not visible because there is an infinite loop in your code. Please check the following code it goes infinite.

while ((CompSum < TotalSpaces) & (PlayerSum < TotalSpaces))
{
    ...
}

Upvotes: 1

Related Questions