Thread: C# beginner windows form app problem

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    3

    C# beginner windows form app problem

    Intro.

    Hi, I am new to programming. In school we now learn C#. We got an assignment that says make the game Seabattle. Ofcourse i am not going to ask here what the code is for such program. That would be silly and i would learn nothing from that.. However i am stuck with some parts. Maybe you guys could point out some mistakes i make or help me over something.

    In advance my appologies for the bad English, I do my best.

    Problem:

    Using the windows form i am trying to make a playfield. This playfield needs to grow or shrink depending on the level the player choses. I let the player choose the level or (niveau in my code) in a menu toolstrip.

    the problem is the folowing: when i select a larger level ex. low=> medium it works, my playfield grows. But when i go from high to lower my playfield does not shrink. I have been staring and trying but i cant seem to find the solution. Can anyone take a look and give me some hints on how to solve this problem?

    Thx in advance.

    the code :
    Code:
    {    public partial class Form1 : Form
        {
            private string level="Low";
            private Label[,] labelArr;
            private int nrRows;
            private int nrColoms;
            private int Guesses=0;
            //constructor
            public Form1()
            {
                InitializeComponent();
                createPlayfield();
            }
    
    
            //properties
            //methods
            private void determineSizePlayfield()
            {
                if (level == "Low")
                {
                    nrRows = 5;
                    nrColoms = 7;
                }
                if (level == "Medium")
                {
                    nrRows = 7;
                    nrColoms = 9;
                }
                if (level == "High")
                {
                    nrRows = 10;
                    nrColoms = 9;
                }
            }
            private void createPlayfield()
            {
                
                determineSizePlayfield();
                
                labelArr = new Label[nrRows, nrColoms];
                int xpositie = 1;
                int ypositie = 1;
    
    
                for (int r = 0; r < nrRows; r++)
                {
                    for (int k = 0; k < nrColoms; k++)
                    {
                        labelArr[r, k] = new Label();
                        labelArr[r, k].Left = xpositie;
                        labelArr[r, k].Top = ypositie;
                        labelArr[r, k].Width = 30;
                        labelArr[r, k].Height = 30;
                        labelArr[r, k].Tag = (char)('A' + r) + " " + k; 
                        labelArr[r, k].BackColor = Color.SkyBlue;
                        labelArr[r, k].BorderStyle = BorderStyle.FixedSingle;
                        labelArr[r, k].MouseClick += labelArr_Click;
                        panel_Speelveld.Controls.Add(labelArr[r, k]);
                        xpositie += labelArr[r, k].Width;
                    }
                    xpositie = 1;
                    ypositie += labelArr[r, nrColoms - 1].Height;
    
    
                }
            }
    
    
            //click events
            private void labelArr_Click(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    MessageBox.Show("LeftmouseButton");
                    Guesses++;
                    textBox_Guesses.Text = Guesses.ToString();
                }
                if (e.Button == MouseButtons.Right)
                {
                    MessageBox.Show("RightMouseButton");
                }
    
    
    
    
            }
    
    
            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Close();
            }
    
    
            private void rulesOfTheGameToolStripMenuItem_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Rules of the game");
            }
    
    
            private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
            {
                MessageBox.Show("About");
            }
    
    
            private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Guesses = 0;
                MessageBox.Show("New game");
            }
    
    
            private void lowToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (lowToolStripMenuItem.Checked == false)
                {
                    lowToolStripMenuItem.Checked = true;
                    mediumToolStripMenuItem.Checked = false;
                    highToolStripMenuItem.Checked = false;
                }
                level = "Low";
    
    
                createPlayfield();
                
            }
    
    
            private void mediumToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (mediumToolStripMenuItem.Checked == false)
                {
                    lowToolStripMenuItem.Checked = false;
                    mediumToolStripMenuItem.Checked = true;
                    highToolStripMenuItem.Checked = false;
                }
                level = "Medium";
                
                createPlayfield();
                
            }
    
    
            private void highToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (highToolStripMenuItem.Checked == false)
                {
                    lowToolStripMenuItem.Checked = false;
                    mediumToolStripMenuItem.Checked = false;
                    highToolStripMenuItem.Checked = true;
                }
                level = "High";
                createPlayfield();
                
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    What i believe that might be a solution is update the Panel or labelArr somehow? but how could i do that?

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You need to clear out the controls on the panel when creating a new playfield
    Code:
    panel_Speelveld.Controls.Clear()
    Woop?

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    that indead solves the problem. thx alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ 2010 Express Windows Form problem
    By Huzapro0 in forum C++ Programming
    Replies: 4
    Last Post: 08-10-2011, 05:24 AM
  2. Windows form C++ - referencing the form
    By rocketman50 in forum Windows Programming
    Replies: 2
    Last Post: 05-22-2010, 11:58 AM
  3. Windows Form Application ?
    By messi10 in forum C# Programming
    Replies: 3
    Last Post: 04-13-2009, 08:07 AM
  4. Windows Form?
    By MK4554 in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 09:43 AM
  5. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

Tags for this Thread