10 Replies - 1034 Views - Last Post: 23 March 2012 - 04:24 AM Rate Topic: -----

#1 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Hello, I am new to C#! Question:

Posted 21 March 2012 - 03:36 PM

Hello, all my name is Tailean. I was wondering if anyone could help me solve a problem I ran into while trying to code a Keno game. My game picks 20 random numbers out of 80 possible. These 80 numbers are represented by 80 buttons! (button1,button2 and so on...)

When the max bet button is pushed,it is supposed to turn any buttons colored orange to their original color transparent first.


The Problem: It does change the buttons back to their original color but it does it at the same time it is changing other random numbers orange. I need it to first change them all to Transparent before it starts to pick the new numbers by changing them orange. (I hope I got the problem across well)

Here is a little snip-it of my code (WARNING: Newbie code, try not to hate on it too much :P)

 public Form1()
        {
            InitializeComponent();
        }

            int returnValue;
            Random randObj = new Random(35);
            Random random = new Random(35);

            int tracker = 0;
            int tokenTracker = 200;

           
        private void button81_Click(object sender, EventArgs e)
        {
           
                button1.BackColor = Color.Transparent;
                button2.BackColor = Color.Transparent;
                button3.BackColor = Color.Transparent;
                button4.BackColor = Color.Transparent;
                button5.BackColor = Color.Transparent;
                button6.BackColor = Color.Transparent;
                button7.BackColor = Color.Transparent;
                button8.BackColor = Color.Transparent;
                button9.BackColor = Color.Transparent;
                button10.BackColor = Color.Transparent;
                button11.BackColor = Color.Transparent;
                button12.BackColor = Color.Transparent;
                button13.BackColor = Color.Transparent;
                button14.BackColor = Color.Transparent;
                button15.BackColor = Color.Transparent;
                button16.BackColor = Color.Transparent;
                button17.BackColor = Color.Transparent;
                button18.BackColor = Color.Transparent;
                button19.BackColor = Color.Transparent;
                button20.BackColor = Color.Transparent;
                button21.BackColor = Color.Transparent;
                button22.BackColor = Color.Transparent;
                button23.BackColor = Color.Transparent;
                button24.BackColor = Color.Transparent;
                button25.BackColor = Color.Transparent;
                button26.BackColor = Color.Transparent;
                button27.BackColor = Color.Transparent;
                button28.BackColor = Color.Transparent;
                button29.BackColor = Color.Transparent;
                button30.BackColor = Color.Transparent;
                button31.BackColor = Color.Transparent;
                button32.BackColor = Color.Transparent;
                button33.BackColor = Color.Transparent;
                button34.BackColor = Color.Transparent;
                button35.BackColor = Color.Transparent;
                button36.BackColor = Color.Transparent;
                button37.BackColor = Color.Transparent;
                button38.BackColor = Color.Transparent;
                button39.BackColor = Color.Transparent;
                button40.BackColor = Color.Transparent;
                button41.BackColor = Color.Transparent;
                button42.BackColor = Color.Transparent;
                button43.BackColor = Color.Transparent;
                button44.BackColor = Color.Transparent;
                button45.BackColor = Color.Transparent;
                button46.BackColor = Color.Transparent;
                button47.BackColor = Color.Transparent;
                button48.BackColor = Color.Transparent;
                button49.BackColor = Color.Transparent;
                button50.BackColor = Color.Transparent;
                button51.BackColor = Color.Transparent;
                button52.BackColor = Color.Transparent;
                button53.BackColor = Color.Transparent;
                button54.BackColor = Color.Transparent;
                button55.BackColor = Color.Transparent;
                button56.BackColor = Color.Transparent;
                button57.BackColor = Color.Transparent;
                button58.BackColor = Color.Transparent;
                button59.BackColor = Color.Transparent;
                button60.BackColor = Color.Transparent;
                button61.BackColor = Color.Transparent;
                button62.BackColor = Color.Transparent;
                button63.BackColor = Color.Transparent;
                button64.BackColor = Color.Transparent;
                button65.BackColor = Color.Transparent;
                button66.BackColor = Color.Transparent;
                button67.BackColor = Color.Transparent;
                button68.BackColor = Color.Transparent;
                button69.BackColor = Color.Transparent;
                button70.BackColor = Color.Transparent;
                button71.BackColor = Color.Transparent;
                button72.BackColor = Color.Transparent;
                button73.BackColor = Color.Transparent;
                button74.BackColor = Color.Transparent;
                button75.BackColor = Color.Transparent;
                button76.BackColor = Color.Transparent;
                button77.BackColor = Color.Transparent;
                button78.BackColor = Color.Transparent;
                button79.BackColor = Color.Transparent;
                button80.BackColor = Color.Transparent;
            
            Random randObj = new Random(35);
            Console.WriteLine(randObj.Next().ToString());
            Byte[] randArray = new Byte[25];

            randObj.NextBytes(randArray);
            --tokenTracker;
            tokenTextBox.Text = "" + tokenTracker;
            textBox1.Clear();
            timer1.Start();
            int returnValue = random.Next(1, 80);
            for (int i = 0; i < randArray.Length; i++)
            {
                returnValue = random.Next(1, 80);

                if (returnValue < 0)
                {
                    returnValue = returnValue * -1;
                }

                if (returnValue == 1)
                {
                    button1.BackColor = Color.Orange;

                    ++tracker;
                }

                if (returnValue == 2)
                {
                    button2.BackColor = Color.Orange;

                    ++tracker;
                }


Is This A Good Question/Topic? 0
  • +

Replies To: Hello, I am new to C#! Question:

#2 Tenderfoot  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 160
  • Joined: 21-March 12

Re: Hello, I am new to C#! Question:

Posted 21 March 2012 - 04:30 PM

It would save you a lot of work if you just used a button[] array to store all of those buttons as you could just loop through them with a for loop when you wanted to change their color.

As for the rest, I'm not sure if I completely understand but, I think, that what you were trying to say is that, you want the program to turn the colors to transparent and wait for a bit before you keep running it and they turn back orange?

If so, and I could very well be wrong, but I think you're using the timer wrong.

            public Form1()
        {
            InitializeComponent();
        }

            int returnValue;
            Random randObj = new Random(35);
            Random random = new Random(35);

            int tracker = 0;
            int tokenTracker = 200;

           
        private void button81_Click(object sender, EventArgs e)
        {
           
                button1.BackColor = Color.Transparent;
                button2.BackColor = Color.Transparent;
                button3.BackColor = Color.Transparent;
                button4.BackColor = Color.Transparent;
                button5.BackColor = Color.Transparent;
                button6.BackColor = Color.Transparent;
                button7.BackColor = Color.Transparent;
                button8.BackColor = Color.Transparent;
                button9.BackColor = Color.Transparent;
                button10.BackColor = Color.Transparent;
                button11.BackColor = Color.Transparent;
                button12.BackColor = Color.Transparent;
                button13.BackColor = Color.Transparent;
                button14.BackColor = Color.Transparent;
                button15.BackColor = Color.Transparent;
                button16.BackColor = Color.Transparent;
                button17.BackColor = Color.Transparent;
                button18.BackColor = Color.Transparent;
                button19.BackColor = Color.Transparent;
                button20.BackColor = Color.Transparent;
                button21.BackColor = Color.Transparent;
                button22.BackColor = Color.Transparent;
                button23.BackColor = Color.Transparent;
                button24.BackColor = Color.Transparent;
                button25.BackColor = Color.Transparent;
                button26.BackColor = Color.Transparent;
                button27.BackColor = Color.Transparent;
                button28.BackColor = Color.Transparent;
                button29.BackColor = Color.Transparent;
                button30.BackColor = Color.Transparent;
                button31.BackColor = Color.Transparent;
                button32.BackColor = Color.Transparent;
                button33.BackColor = Color.Transparent;
                button34.BackColor = Color.Transparent;
                button35.BackColor = Color.Transparent;
                button36.BackColor = Color.Transparent;
                button37.BackColor = Color.Transparent;
                button38.BackColor = Color.Transparent;
                button39.BackColor = Color.Transparent;
                button40.BackColor = Color.Transparent;
                button41.BackColor = Color.Transparent;
                button42.BackColor = Color.Transparent;
                button43.BackColor = Color.Transparent;
                button44.BackColor = Color.Transparent;
                button45.BackColor = Color.Transparent;
                button46.BackColor = Color.Transparent;
                button47.BackColor = Color.Transparent;
                button48.BackColor = Color.Transparent;
                button49.BackColor = Color.Transparent;
                button50.BackColor = Color.Transparent;
                button51.BackColor = Color.Transparent;
                button52.BackColor = Color.Transparent;
                button53.BackColor = Color.Transparent;
                button54.BackColor = Color.Transparent;
                button55.BackColor = Color.Transparent;
                button56.BackColor = Color.Transparent;
                button57.BackColor = Color.Transparent;
                button58.BackColor = Color.Transparent;
                button59.BackColor = Color.Transparent;
                button60.BackColor = Color.Transparent;
                button61.BackColor = Color.Transparent;
                button62.BackColor = Color.Transparent;
                button63.BackColor = Color.Transparent;
                button64.BackColor = Color.Transparent;
                button65.BackColor = Color.Transparent;
                button66.BackColor = Color.Transparent;
                button67.BackColor = Color.Transparent;
                button68.BackColor = Color.Transparent;
                button69.BackColor = Color.Transparent;
                button70.BackColor = Color.Transparent;
                button71.BackColor = Color.Transparent;
                button72.BackColor = Color.Transparent;
                button73.BackColor = Color.Transparent;
                button74.BackColor = Color.Transparent;
                button75.BackColor = Color.Transparent;
                button76.BackColor = Color.Transparent;
                button77.BackColor = Color.Transparent;
                button78.BackColor = Color.Transparent;
                button79.BackColor = Color.Transparent;
                button80.BackColor = Color.Transparent;
            }  // Was missing
            timer1.Interval = 5000; //Sets the interval to 5 seconds
            timer1.Enabled = true; 
            timer1.Tick += new EventHandler(timer1_Tick); 

            void timer1_Tick(object sender, EventArgs e)
            {
            Random randObj = new Random(35);
            Console.WriteLine(randObj.Next().ToString());
            Byte[] randArray = new Byte[25];

            randObj.NextBytes(randArray);
            --tokenTracker;
            tokenTextBox.Text = "" + tokenTracker;
            textBox1.Clear();
            int returnValue = random.Next(1, 80);
            for (int i = 0; i < randArray.Length; i++)
            {
                returnValue = random.Next(1, 80);

                if (returnValue < 0)
                {
                    returnValue = returnValue * -1;
                }

                if (returnValue == 1)
                {
                    button1.BackColor = Color.Orange;

                    ++tracker;
                }

                if (returnValue == 2)
                {
                    button2.BackColor = Color.Orange;

                    ++tracker;
                }

               timer1.Enabled = false; 
            }



Hope this helps at all, even if just to get the general idea of how this could work. I didn't write this/try to compile any of it so I'm not sure what will happen, but you could try running it. In my head it seemed like it might be set to wait for about 5 seconds before running the rest, and I hope that's the case but yeah, try it out, but don't hope for too much. ^^

This post has been edited by tlhIn`toq: 21 March 2012 - 04:58 PM

Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5465
  • View blog
  • Posts: 11,740
  • Joined: 02-June 10

Re: Hello, I am new to C#! Question:

Posted 21 March 2012 - 04:41 PM

Looping through a collection of buttons is a good idea.
In fact you already that that: The form.controls collection will include all the controls: Buttons and otherwise. So you could loop through that.

Personally I would have made the buttons programmatically instead of in designer, but it reduces all the manual labor.

List<Button> myKenoButtons = new List<Button>;
for(int Index = 0; Index < 79; Index++)
{
   button temp = new button();
   temp.Name = "KenoButton" + Index;// KenoButton1, KenoButton2 etc.
   myKenoButtons.Add(temp);
   // set the location and other properties
   this.Controls.Add(temp);
}


Then you access any button from the array. So if you randomly pick 52 that is myKenoButtons[52] you can then change its color and so on.

All of what you have so far desperatly needs to be broken down into individual methods. MakeButtons(), PickNumber(), ResetBoard() etc. Having one giant method like this is a maintenance nightmare. This article should help with that:
This is the 'Help me with my homework' article you wanted.
Was This Post Helpful? 0
  • +
  • -

#4 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: Hello, I am new to C#! Question:

Posted 21 March 2012 - 04:52 PM

Thanks for the post Tenderfoot! Yeah it pretty much worked except..it kept adding a bunch of new numbers every 5 seconds..and I only need it to do it once.....can you help me with that? Also could I get it to clear the numbers quicker? Thanks for you help ;)
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5465
  • View blog
  • Posts: 11,740
  • Joined: 02-June 10

Re: Hello, I am new to C#! Question:

Posted 21 March 2012 - 05:03 PM

it kept adding a bunch of new numbers every 5 seconds..and I only need it to do it once


Then in the Timer_Tick handler method stop the timer.

Timer1.Stop();


View Postsela007, on 21 March 2012 - 05:57 PM, said:

View PosttlhIn`toq, on 21 March 2012 - 04:41 PM, said:

Personally I would have made the buttons programmatically instead of in designer, but it reduces all the manual labor.
Then you access any button from the array. So if you randomly pick 52 that is myKenoButtons[52] you can then change its color and so on.

one question. Is this possible by adding existing buttons to list, I mean buttons created in designer?


Yes. Just add them to the List<Button> in the same way I did in my example

myKenoButtons.Add(Button1);


The best advice I can give you though is to stop. You're not ready to build a program yet.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.




Finding answers to specific problems:
Spoiler





If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler


See FAQ # 23. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Mar 2012
Spoiler



Was This Post Helpful? 1
  • +
  • -

#6 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Hello, I am new to C#! Question:

Posted 21 March 2012 - 05:06 PM

First off, 80 buttons? On one form? How in the world did you fit 80 buttons on one form? Anyway, you are changing them all back to Transparent first, it's just that it's happening too quickly for you to actually see it. When do you want it to change to orange?
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5465
  • View blog
  • Posts: 11,740
  • Joined: 02-June 10

Re: Hello, I am new to C#! Question:

Posted 21 March 2012 - 05:21 PM

That is the very first error message covered in this article:
What does this error message mean?
Was This Post Helpful? 0
  • +
  • -

#8 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: Hello, I am new to C#! Question:

Posted 21 March 2012 - 07:12 PM

Hi all, I am having some issues with the random number generator. I want it to only generate 20 random numbers and right now it is just generating as many as it wants and I am not sure why.

Here is my code:
 public Form1()
        {
            InitializeComponent();
        }

            int returnValue;
            Random randObj = new Random(20);
            Random random = new Random(20);

            int tracker = 0;
            int tokenTracker = 100;


            private void button81_Click(object sender, EventArgs e)
            {

                button1.BackColor = Color.Transparent;
                button2.BackColor = Color.Transparent;
                button3.BackColor = Color.Transparent;
                button4.BackColor = Color.Transparent;
                button5.BackColor = Color.Transparent;
                button6.BackColor = Color.Transparent;
                button7.BackColor = Color.Transparent;
                button8.BackColor = Color.Transparent;
                button9.BackColor = Color.Transparent;
                button10.BackColor = Color.Transparent;
                button11.BackColor = Color.Transparent;
                button12.BackColor = Color.Transparent;
                button13.BackColor = Color.Transparent;
                button14.BackColor = Color.Transparent;
                button15.BackColor = Color.Transparent;
                button16.BackColor = Color.Transparent;
                button17.BackColor = Color.Transparent;
                button18.BackColor = Color.Transparent;
                button19.BackColor = Color.Transparent;
                button20.BackColor = Color.Transparent;
                button21.BackColor = Color.Transparent;
                button22.BackColor = Color.Transparent;
                button23.BackColor = Color.Transparent;
                button24.BackColor = Color.Transparent;
                button25.BackColor = Color.Transparent;
                button26.BackColor = Color.Transparent;
                button27.BackColor = Color.Transparent;
                button28.BackColor = Color.Transparent;
                button29.BackColor = Color.Transparent;
                button30.BackColor = Color.Transparent;
                button31.BackColor = Color.Transparent;
                button32.BackColor = Color.Transparent;
                button33.BackColor = Color.Transparent;
                button34.BackColor = Color.Transparent;
                button35.BackColor = Color.Transparent;
                button36.BackColor = Color.Transparent;
                button37.BackColor = Color.Transparent;
                button38.BackColor = Color.Transparent;
                button39.BackColor = Color.Transparent;
                button40.BackColor = Color.Transparent;
                button41.BackColor = Color.Transparent;
                button42.BackColor = Color.Transparent;
                button43.BackColor = Color.Transparent;
                button44.BackColor = Color.Transparent;
                button45.BackColor = Color.Transparent;
                button46.BackColor = Color.Transparent;
                button47.BackColor = Color.Transparent;
                button48.BackColor = Color.Transparent;
                button49.BackColor = Color.Transparent;
                button50.BackColor = Color.Transparent;
                button51.BackColor = Color.Transparent;
                button52.BackColor = Color.Transparent;
                button53.BackColor = Color.Transparent;
                button54.BackColor = Color.Transparent;
                button55.BackColor = Color.Transparent;
                button56.BackColor = Color.Transparent;
                button57.BackColor = Color.Transparent;
                button58.BackColor = Color.Transparent;
                button59.BackColor = Color.Transparent;
                button60.BackColor = Color.Transparent;
                button61.BackColor = Color.Transparent;
                button62.BackColor = Color.Transparent;
                button63.BackColor = Color.Transparent;
                button64.BackColor = Color.Transparent;
                button65.BackColor = Color.Transparent;
                button66.BackColor = Color.Transparent;
                button67.BackColor = Color.Transparent;
                button68.BackColor = Color.Transparent;
                button69.BackColor = Color.Transparent;
                button70.BackColor = Color.Transparent;
                button71.BackColor = Color.Transparent;
                button72.BackColor = Color.Transparent;
                button73.BackColor = Color.Transparent;
                button74.BackColor = Color.Transparent;
                button75.BackColor = Color.Transparent;
                button76.BackColor = Color.Transparent;
                button77.BackColor = Color.Transparent;
                button78.BackColor = Color.Transparent;
                button79.BackColor = Color.Transparent;
                button80.BackColor = Color.Transparent;

                timer1.Interval = 1; //Sets the interval to 5 seconds
                timer1.Enabled = true;
                timer1.Tick += new EventHandler(timer2_Tick);
            }
    void timer2_Tick(object sender, EventArgs e)

    {
            Random randObj = new Random(10);
            Console.WriteLine(randObj.Next().ToString());
            Byte[] randArray = new Byte[10];
    
            randObj.NextBytes(randArray);
            --tokenTracker;
            tokenTextBox.Text = "" + tokenTracker;
            textBox1.Clear();
            timer1.Start();

            int returnValue = random.Next(1, 80);
            for (int i = 0; i < randArray.Length; i++)
            {
                returnValue = random.Next(1, 80);

                if (returnValue < 0)
                {
                    returnValue = returnValue * -1;
                }

                if (returnValue == 1)
                {
                    button1.BackColor = Color.Orange;

                    ++tracker;
                }

                if (returnValue == 2)
                {
                    button2.BackColor = Color.Orange;

                    ++tracker;
                }

                if (returnValue == 3)
                {
                    button3.BackColor = Color.Orange;

                    ++tracker;
                }

                if (returnValue == 4)
                {
                    button4.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 5)
                {
                    button5.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 6)
                {
                    button6.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 7)
                {
                    button7.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 8)
                {
                    button8.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 9)
                {
                    button9.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 10)
                {
                    button10.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 11)
                {
                    button11.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 12)
                {
                    button12.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 13)
                {
                    button13.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 14)
                {
                    button14.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 15)
                {
                    button15.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 16)
                {
                    button16.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 17)
                {
                    button17.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 18)
                {
                    button18.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 19)
                {
                    button19.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 20)
                {
                    button20.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 21)
                {
                    button21.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 22)
                {
                    button22.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 23)
                {
                    button23.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 24)
                {
                    button24.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 25)
                {
                    button25.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 26)
                {
                    button26.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 27)
                {
                    button27.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 28)
                {
                    button28.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 29)
                {
                    button29.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 30)
                {
                    button30.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 31)
                {
                    button31.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 32)
                {
                    button32.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 33)
                {
                    button33.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 34)
                {
                    button34.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 35)
                {
                    button35.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 36)
                {
                    button36.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 37)
                {
                    button37.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 38)
                {
                    button38.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 39)
                {
                    button39.BackColor = Color.Orange;
     
                    ++tracker;

                }

                if (returnValue == 40)
                {
                    button40.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 41)
                {
                    button41.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 42)
                {
                    button42.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 43)
                {
                    button43.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 44)
                {
                    button44.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 45)
                {
                    button45.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 46)
                {
                    button46.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 47)
                {
                    button47.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 48)
                {
                    button48.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 49)
                {
                    button49.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 50)
                {
                    button50.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 51)
                {
                    button51.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 52)
                {
                    button52.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 53)
                {
                    button53.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 54)
                {
                    button54.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 55)
                {
                    button55.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 56)
                {
                    button56.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 57)
                {
                    button57.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 58)
                {
                    button58.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 59)
                {
                    button59.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 60)
                {
                    button60.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 61)
                {
                    button61.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 62)
                {
                    button62.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 63)
                {
                    button63.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 64)
                {
                    button64.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 65)
                {
                    button65.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 66)
                {
                    button66.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 67)
                {
                    button67.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 68)
                {
                    button68.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 69)
                {
                    button69.BackColor = Color.Orange;

                    ++tracker;

                }

                if (returnValue == 70)
                {
                    button70.BackColor = Color.Orange;
               
                    ++tracker;

                }

                if (returnValue == 71)
                {
                    button71.BackColor = Color.Orange;
                   
                    ++tracker;

                }

                if (returnValue == 72)
                {
                    button72.BackColor = Color.Orange;
                 
                    ++tracker;

                }

                if (returnValue == 73)
                {
                    button73.BackColor = Color.Orange;
                   
                    ++tracker;

                }

                if (returnValue == 74)
                {
                    button74.BackColor = Color.Orange;
            
                    ++tracker;

                }

                if (returnValue == 75)
                {
                    button75.BackColor = Color.Orange;
                
                    ++tracker;

                }

                if (returnValue == 76)
                {

                    button76.BackColor = Color.Orange;
                 
                    ++tracker;

                }

                if (returnValue == 77)
                {
                    button77.BackColor = Color.Orange;
                 
                    ++tracker;

                }

                if (returnValue == 78)
                {
                    button78.BackColor = Color.Orange;
               
                    ++tracker;

                }

                if (returnValue == 79)
                {
                    button79.BackColor = Color.Orange;
                
                    ++tracker;

                }

                if (returnValue == 80)
                {
                    button80.BackColor = Color.Orange;
                 
                    ++tracker;
                
                }
                

            }
            if (returnValue >= 10)
            {
                timer1.Stop();
            }
            timer1.Stop();
            returnValue = 0; 
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            ++returnValue;
           
            if(returnValue >= 10)
            {
                timer1.Stop();
            }
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
    }
}



Thanks in advance for any help.
Was This Post Helpful? 0
  • +
  • -

#9 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 271
  • View blog
  • Posts: 550
  • Joined: 07-July 10

Re: Hello, I am new to C#! Question:

Posted 22 March 2012 - 05:45 AM

View Postsuperkb10, on 21 March 2012 - 07:06 PM, said:

First off, 80 buttons? On one form? How in the world did you fit 80 buttons on one form?

Small buttons to simulate this.

Posted Image


@ Tailean: The number in the Random constructor is a seed number. Setting the seed number like that will mean the same random numbers will come up every time you run your program. Random will always give you an infinite count of numbers. You need to manually track how many buttons have turned colors and turn off the timer once you've hit 20. You also have to be aware of a trap. What happens if it randomly gives you the number 14 twenty times in a row? Are there twenty numbers lit? How will you handle repeat numbers like that? You should also redactor the code to take advantage of what others have suggested. It will make much cleaner code.

This post has been edited by Robin19: 22 March 2012 - 05:46 AM

Was This Post Helpful? 0
  • +
  • -

#10 Tailean  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 21-March 12

Re: Hello, I am new to C#! Question:

Posted 22 March 2012 - 11:35 AM

Thank you robin19 for your posts. When you was talking about changing the consutor do you mean this line?:
 Byte[] randArray = new Byte[20];

Thanks again
Was This Post Helpful? 0
  • +
  • -

#11 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 271
  • View blog
  • Posts: 550
  • Joined: 07-July 10

Re: Hello, I am new to C#! Question:

Posted 23 March 2012 - 04:24 AM

Sorry, no. I was talking about the Random constructors.
            Random randObj = new Random(20);
            Random random = new Random(20);


What you have written will create two Random objects that will return the same numbers in the same order every time. We can test this in a console. If you run the following code, you can see that both randoms will return the same numbers. You will also see that they will return the same numbers, no matter how many times you run the code. This is because they are really pseudo random. Look it up online if you want more details about why this happens.
        static void Main(string[] args)
        {
            Random randObj = new Random(20);
            Random random = new Random(20);

            for (int i = 0; i < 30; i++)
            {
                var first = randObj.Next(100);
                var second = random.Next(100);
                Console.WriteLine("{0:00} {1:00} {2:00}", 
                    i, first, second);
            }
        }


Instead, make a class level Random variable. Use this variable every time you need something randomized. And create it using a parameterless constructor Random random = new Random() if you don't want the same numbers to come up every time you start the project.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1