Help with Windows fForm (.net)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help with Windows fForm (.net)

  1. #1
    Join Date
    Oct 2010
    Posts
    2

    Help with Windows fForm (.net)

    Hello,

    I am new to windows forms so please bear with me!

    Ok, I am trying to make a program for my friends and I who play a card game.
    The basic idea of the program will be to randomize teams and decks to show who is on what team, and what deck they will use.

    So far, this is what I have:

    Code:
    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;
    using System.Collections;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            ArrayList players = new ArrayList();
            ArrayList decks = new ArrayList();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            /***************************************************
             * Add Players
             * ************************************************/
            private void button1_Click(object sender, EventArgs e)
            {
                //Add player Rob to array.
                players.Add("Rob");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //Add player Bryan to array.
                players.Add("Bryan");
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                //Add player Caryn to array.
                players.Add("Caryn");
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                //Add player Matt to array.
                players.Add("Matt");
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                //Add player Chris to array.
                players.Add("Chris");
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                //Add player Wisto to array.
                players.Add("Wisto");
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                //Add player Sean to array.
                players.Add("Sean");
            }
    
            /***************************************************
             * Add Decks
             * ************************************************/
            private void button8_Click(object sender, EventArgs e)
            {
                //Add deck Blue/Red to array.
                decks.Add("Blue/Red");
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
                //Add deck Green/Black INFECT to array.
                decks.Add("Green/Black INFECT");
            }
    
            private void button10_Click(object sender, EventArgs e)
            {
                //Add deck Blue to array.
                decks.Add("Blue");
            }
    
            private void button11_Click(object sender, EventArgs e)
            {
                //Add deck Blue/Black to array.
                decks.Add("Blue/Black");
            }
    
            private void button12_Click(object sender, EventArgs e)
            {
                //Add deck Green/Red to array.
                decks.Add("Green/Red");
            }
    
            private void button13_Click(object sender, EventArgs e)
            {
                //Add deck Green/Black to array.
                decks.Add("Green/Black");
            }
    
            private void button14_Click(object sender, EventArgs e)
            {
                //Add deck White to array.
                decks.Add("White");
            }
    
            private void button15_Click(object sender, EventArgs e)
            {
                //Add deck White MYR to array.
                decks.Add("White MYR");
            }
    
            private void button16_Click(object sender, EventArgs e)
            {
                //Add deck ELF to array.
                decks.Add("ELF");
            }
    
            private void button17_Click(object sender, EventArgs e)
            {
                //Add deck Red/Black OLD to array.
                decks.Add("OLD Red/Black");
            }
    
            private void button18_Click(object sender, EventArgs e)
            {
                //Add deck Black OLD to array.
                decks.Add("OLD Black");
            }
    
            private void button19_Click(object sender, EventArgs e)
            {
                //Add deck Blue OLD to array.
                decks.Add("OLD Blue");
            }
    
            private void button20_Click(object sender, EventArgs e)
            {
                //Add deck White OLD to array.
                decks.Add("OLD White");
            }
    
            private void button21_Click(object sender, EventArgs e)
            {
                //Add deck Green/Red OLD to array.
                decks.Add("OLD Green/Red");
            }
    
            private void button22_Click(object sender, EventArgs e)
            {
                //Add deck Green OLD to array.
                decks.Add("OLD Green");
            }
    
            /********************************************************
             * Randomize 
             * for now, just print lists
             * *****************************************************/
    
            private void button25_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < players.Count; i++)
                {
                    string value = players[i] as string;
                    textBox4.Text = value + "\r";
                }
            }
    
            private void button26_Click(object sender, EventArgs e)
            {
                for (int j = 0; j < decks.Count; j++)
                {
                    string value1 = decks[j] as string;
                    textBox3.Text = value1 + "\r";
                }
            }
            
            private void textBox4_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void textBox3_TextChanged(object sender, EventArgs e)
            {
    
            }
        }
    }


    Basically, since I am new, I am just trying to add "players" and "decks" to the "players" and "decks" ArrayLists whenever a button is pushed. Than, when I press the "Randomize Names" or "Randomize Decks" buttons, I want to print the ArrayLists into the textBoxes.

    So far, I cannot get anything to print into the Teams textBox and the only thing that prints in the Decks textBox is the last deck button I pushed!

    Any advice or tips would be much appreciated. If you have any questions let me know and I would be happy to send my source code.

    This is just something I'm doing for fun but I am thankful for any help.

    Thanks in advance.
    Last edited by HanneSThEGreaT; May 4th, 2011 at 11:17 AM.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,099

    Re: Help with Windows fForm (.net)

    OUCH, my eyes!!

    Please use [ Code] tags when posting code. I have edited your post

  3. #3
    Join Date
    Oct 2010
    Posts
    2

    Re: Help with Windows fForm (.net)

    Oh nice, thanks Hannes. I wasn't sure how to do that.

  4. #4
    Join Date
    May 2011
    Posts
    4

    Re: Help with Windows fForm (.net)

    When you set the text of your text box, I think you want to append the text instead of replacing it. See your line of code that matches the following:

    Code:
    textBox4.Text = value + "\r";
    If you use the addition assignment operator (i.e. "+=" instead of "="), you will will concatenate (append) the new value to what already exists in the text box. Your code should look like the following:

    Code:
    textBox4.Text += value + "\r";
    There was one other spot in your code that has a similar problem.

  5. #5
    Join Date
    Oct 2010
    Posts
    2

    Re: Help with Windows fForm (.net)

    Great advice Josh, thanks.
    It worked like a charm.

    I am still having trouble with getting the players box to print. I really am not sure what is going on there. I can print the decks into the decks textBox, but the players will not print into the players textBox. The code seems to be identical...

    Please take a look and let me know if something look to be the problem.

    Code:
    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;
    using System.Collections;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            ArrayList players = new ArrayList();
            ArrayList decks = new ArrayList();
            //string addDeck = "";
            //string addPlayer = "";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            /***************************************************
             * Add Players
             * ************************************************/
            private void button1_Click(object sender, EventArgs e)
            {
                //Add player Rob to array.
                players.Add("Rob");
                //textBox4.Text = ("Rob\r\n");
                button1.Visible = false;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //Add player Bryan to array.
                players.Add("Bryan");
                //textBox4.Text = ("Bryan\r\n");
                button2.Visible = false;
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                //Add player Caryn to array.
                players.Add("Caryn");
                //textBox4.Text += ("Caryn\r\n");
                button4.Visible = false;
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                //Add player Matt to array.
                players.Add("Matt");
                //textBox4.Text += ("Matt\r\n");
                button3.Visible = false;
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                //Add player Chris to array.
                players.Add("Chris");
                //textBox4.Text += ("Chris\r\n");
                button5.Visible = false;
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                //Add player Wisto to array.
                players.Add("Wisto");
                //textBox4.Text += ("Wisto\r\n");
                button6.Visible = false;
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                //Add player Sean to array.
                players.Add("Sean");
                //textBox4.Text += ("Sean\r\n");
                button7.Visible = false;
            }
    
            /***************************************************
             * Add Decks
             * ************************************************/
            private void button8_Click(object sender, EventArgs e)
            {
                //Add deck Blue/Red to array.
                decks.Add("Blue/Red");
                //textBox3.Text += ("Blue/Red\r\n");
                button8.Visible = false;
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
                //Add deck Green/Black INFECT to array.
                decks.Add("Green/Black INFECT");
                //textBox3.Text += ("Green/Black INFECT\r\n");
                button9.Visible = false;
            }
    
            private void button10_Click(object sender, EventArgs e)
            {
                //Add deck Blue to array.
                decks.Add("Blue");
                //textBox3.Text += ("Blue\r\n");
                button10.Visible = false;
            }
    
            private void button11_Click(object sender, EventArgs e)
            {
                //Add deck Blue/Black to array.
                decks.Add("Blue/Black");
                //textBox3.Text += ("Blue/Black\r\n");
                button11.Visible = false;
            }
    
            private void button12_Click(object sender, EventArgs e)
            {
                //Add deck Green/Red to array.
                decks.Add("Green/Red");
                //textBox3.Text += ("Green/Red\r\n");
                button12.Visible = false;
            }
    
            private void button13_Click(object sender, EventArgs e)
            {
                //Add deck Green/Black to array.
                decks.Add("Green/Black");
                //textBox3.Text += ("Green/Black\r\n");
                button13.Visible = false;
            }
    
            private void button14_Click(object sender, EventArgs e)
            {
                //Add deck White to array.
                decks.Add("White");
                //textBox3.Text += ("White\r\n");
                button14.Visible = false;
            }
    
            private void button15_Click(object sender, EventArgs e)
            {
                //Add deck White MYR to array.
                decks.Add("White MYR");
                //textBox3.Text += ("White MYR\r\n");
                button15.Visible = false;
            }
    
            private void button16_Click(object sender, EventArgs e)
            {
                //Add deck ELF to array.
                decks.Add("ELF");
                //textBox3.Text += ("ELF\r\n");
                button16.Visible = false;
            }
    
            private void button17_Click(object sender, EventArgs e)
            {
                //Add deck Red/Black OLD to array.
                decks.Add("OLD Red/Black");
                //textBox3.Text += ("OLD Red/Black\r\n");
                button17.Visible = false;
            }
    
            private void button18_Click(object sender, EventArgs e)
            {
                //Add deck Black OLD to array.
                decks.Add("OLD Black");
                //textBox3.Text += ("OLD Black\r\n");
                button18.Visible = false;
            }
    
            private void button19_Click(object sender, EventArgs e)
            {
                //Add deck Blue OLD to array.
                decks.Add("OLD Blue");
                //textBox3.Text += ("OLD Blue\r\n");
                button19.Visible = false;
            }
    
            private void button20_Click(object sender, EventArgs e)
            {
                //Add deck White OLD to array.
                decks.Add("OLD White");
                //textBox3.Text += ("OLD White\r\n");
                button20.Visible = false;
            }
    
            private void button21_Click(object sender, EventArgs e)
            {
                //Add deck Green/Red OLD to array.
                decks.Add("OLD Green/Red");
                //textBox3.Text += ("OLD Green/Red\r\n");
                button21.Visible = false;
            }
    
            private void button22_Click(object sender, EventArgs e)
            {
                //Add deck Green OLD to array.
                decks.Add("OLD Green");
                //textBox3.Text += ("OLD Green\r\n");
                button22.Visible = false;
            }
    
            /********************************************************
             * Randomize 
             * for now, just print lists
             * *****************************************************/
    
            private void button25_Click(object sender, EventArgs e)
            {
                //players does not print into textbox4...not sure why....
                for (int i = 0; i < players.Count; i++)
                {
                    string value = players[i] as string;
                    textBox4.Text += value + ("\r\n");
                }
            }
    
            private void button26_Click(object sender, EventArgs e)
            {
                for (int j = 0; j < decks.Count; j++)
                {
                    string value1 = decks[j] as string;
                    textBox3.Text += (value1 + "\r\n");
                }
            }
            
            private void textBox4_TextChanged(object sender, EventArgs e)
            {
                //Player-Teams Textbox
            }
    
            private void textBox3_TextChanged(object sender, EventArgs e)
            {
                //Decks Textbox
            }
    
            private void button24_Click(object sender, EventArgs e)
            {
                //this is the manual add player button
                string addPlayer = textBox1.ToString();
                //string addPlayer = Console.ReadLine();
                players.Add(addPlayer);
            }
    
            private void button27_Click(object sender, EventArgs e)
            {
                //Clear Deck List
                decks.RemoveRange(0, decks.Count);
                textBox3.Text = "";
                button8.Visible = true;
                button9.Visible = true;
                button10.Visible = true;
                button11.Visible = true;
                button12.Visible = true;
                button13.Visible = true;
                button14.Visible = true;
                button15.Visible = true;
                button16.Visible = true;
                button17.Visible = true;
                button18.Visible = true;
                button19.Visible = true;
                button20.Visible = true;
                button21.Visible = true;
                button22.Visible = true;
    
            }
    
            private void button28_Click(object sender, EventArgs e)
            {
                //Clear Team List
                players.RemoveRange(0, decks.Count);
                textBox4.Text = "";
                button1.Visible = true;
                button2.Visible = true;
                button3.Visible = true;
                button4.Visible = true;
                button5.Visible = true;
                button6.Visible = true;
                button7.Visible = true;
            }
    
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
                //user input for manually added decks.
                //string addDeck = textBox2.;
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                //user input for manually added players.
            }
    
            private void button23_Click(object sender, EventArgs e)
            {
                //Manually add decks button.
                //decks.Add(addDeck);
            }
    
            private void label6_Click(object sender, EventArgs e)
            {
                //"Blue" new deck label.
            }
        }
    }
    Thanks again,
    Matt

  6. #6
    Join Date
    Oct 2006
    Posts
    82

    Re: Help with Windows fForm (.net)

    Google C# Arraylist. There is an excellent example there. You can get rid of the "as string" snippets.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center