Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 135,948 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,700 people online right now. Registration is fast and FREE... Join Now!




What loop function should I use here for labels.text?

 
Reply to this topicStart new topic

What loop function should I use here for labels.text?

papuccino1
15 May, 2008 - 05:37 PM
Post #1

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
Hello. I'm back after a long hiatus and I feel the rust creeping in. Never more shall I be without coding for so long.

Guys could you refresh my memory? How could I loop a random function to the .text of labels 1 - 10?

My code so far:

CODE
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random RandomNumber = new Random();
            int x = RandomNumber.Next(10);

            this.label2.Text = Convert.ToString(x);

            this.label3.Text = Convert.ToString(x);          

            
        }
    }
}


Good thing is, I know what has to be done, the logic behind it. I just need to know the proper format.

There are two lines that write to the .text of the labels, however they write the same random value, that's why I need the loop.

Is there a way I could check my previous posts, I believe I made a post about my Bingo game that used something similar, maybe that will help me out.

Thanks DIC. smile.gif



Edit: Guys I re-read my post and I think you'll have a hard time understanding it. I've drawn a lil pic for you. :3

IPB Image


Edit2: After toying around for a while, I've noticed that even I have to seperate random things, they still output the same "random" number.

CODE
private void button1_Click(object sender, EventArgs e)
        {
            Random RandomNumber = new Random();
            int x = RandomNumber.Next(10);

            Random lol = new Random();            
            int y = lol.Next(10);

            label2.Text = Convert.ToString(x);
            label3.Text = Convert.ToString(y);      
                        
        }


Any reason to this? Randomnumer and lol are two distinct things. Why do they produce the same number if they are allegedly random?

This post has been edited by papuccino1: 15 May, 2008 - 06:02 PM
User is offlineProfile CardPM
+Quote Post

papuccino1
RE: What Loop Function Should I Use Here For Labels.text?
15 May, 2008 - 06:39 PM
Post #2

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
Hehe. Ok figured it out but optimised = non existant. tongue.gif

Any thoughts on how to improve the code? I know with that for loop I'll make it way smaller, but any other thoughts?

CODE
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            label10.BackColor = Color.White;
            label11.BackColor = Color.White;
            label12.BackColor = Color.White;
            label13.BackColor = Color.White;

            Random rdm1 = new Random();
            int x = rdm1.Next(13);

            Random rdm2 = new Random();
            int y = rdm2.Next(12);

            Random rdm3 = new Random();
            int z = rdm3.Next(11);

            Random rdm4 = new Random();
            int a = rdm4.Next(10);

            Random rdm5 = new Random();
            int b = rdm5.Next(9);

            Random rdm6 = new Random();
            int c = rdm6.Next(8);

            Random rdm7 = new Random();
            int s = rdm7.Next(7);

            Random rdm8 = new Random();
            int t = rdm8.Next(6);

            label2.Text = Convert.ToString(x);
            label3.Text = Convert.ToString(y);
            label4.Text = Convert.ToString(z);
            label5.Text = Convert.ToString(a);
            label6.Text = Convert.ToString(b);
            label7.Text = Convert.ToString(c);
            label8.Text = Convert.ToString(s);
            label9.Text = Convert.ToString(t);
                                    
        }
              

        private void button2_Click(object sender, EventArgs e)
        {
            int sum1;
            int sum2;
            int sum3;
            int sum4;

            int val1 = int.Parse(label2.Text);
            int val2 = int.Parse(label3.Text);
            int val3 = int.Parse(label4.Text);
            int val4 = int.Parse(label5.Text);
            int val5 = int.Parse(label6.Text);
            int val6 = int.Parse(label7.Text);
            int val7 = int.Parse(label8.Text);
            int val8 = int.Parse(label9.Text);

            sum1 = val1 + val2;
            sum2 = val3 + val4;
            sum3 = val5 + val6;
            sum4 = val7 + val8;


            if (textBox1.Text == Convert.ToString(sum1))
            {
                label10.BackColor = Color.Green;
            }
            else
            {
                label10.BackColor = Color.Red;
            }





            if (textBox2.Text == Convert.ToString(sum2))
            {
                label11.BackColor = Color.Green;
            }
            else
            {
                label11.BackColor = Color.Red;
            }






            if (textBox3.Text == Convert.ToString(sum3))
            {
                label12.BackColor = Color.Green;
            }
            else
            {
                label12.BackColor = Color.Red;
            }





            if (textBox4.Text == Convert.ToString(sum4))
            {
                label13.BackColor = Color.Green;
            }
            else
            {
                label13.BackColor = Color.Red;
            }

        }
    }



The code I posted works 100% just like I wanted it to, so I guess that's great. biggrin.gif

However I'd like to start learning some good programming habits before I get stuck making these redundant lines of code in the future.

I need that loop!!!

Should I post the program itself for you guys to try it out? Maybe that makes the code easier to understand. Let me know.

This post has been edited by papuccino1: 15 May, 2008 - 06:47 PM
User is offlineProfile CardPM
+Quote Post

papuccino1
RE: What Loop Function Should I Use Here For Labels.text?
16 May, 2008 - 07:25 AM
Post #3

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
Is anyone going to help me?

I mean I posted almost all of my code, and I've seen people only asking for the whole program and they get help.

Do you help people who show no effort?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: What Loop Function Should I Use Here For Labels.text?
16 May, 2008 - 08:09 AM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,907



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Patience, young one. We have lives besides this forum and as a result it can take a day or two before you get an answer.

Here is one way you can optimize your code. You only need one Random object, you will just reuse it each time you need it. Currently, loops are not an option for your implementation.

CODE

        private void button1_Click(object sender, EventArgs e)
        {
            label10.BackColor = Color.White;
            label11.BackColor = Color.White;
            label12.BackColor = Color.White;
            label13.BackColor = Color.White;

            Random rdm = new Random();

            label2.Text = Convert.ToString(rdm.Next(13));
            label3.Text = Convert.ToString(rdm.Next(12));
            label4.Text = Convert.ToString(rdm.Next(11));
            label5.Text = Convert.ToString(rdm.Next(10));
            label6.Text = Convert.ToString(rdm.Next(9));
            label7.Text = Convert.ToString(rdm.Next(8));
            label8.Text = Convert.ToString(rdm.Next(7));
            label9.Text = Convert.ToString(rdm.Next(6));
                                    
        }

User is offlineProfile CardPM
+Quote Post

papuccino1
RE: What Loop Function Should I Use Here For Labels.text?
16 May, 2008 - 09:04 AM
Post #5

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
Ah, beautiful. Thank you for the tip.

However I know there is a loop function that cycles through the labels 1-10. I've used it before for my game of Bingo I made.

Any insight on that?

User is offlineProfile CardPM
+Quote Post

Jayman
RE: What Loop Function Should I Use Here For Labels.text?
16 May, 2008 - 10:45 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,907



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You would have to use a ForEach loop to iterate through all the controls on the page looking for the ones you want.
CODE

foreach(Control ctl in this.Controls)
{
   'look for each control by name or type here
}


If you have done it before, why didn't you just look at the Bingo project and see how you did it last time?

User is offlineProfile CardPM
+Quote Post

papuccino1
RE: What Loop Function Should I Use Here For Labels.text?
16 May, 2008 - 11:05 AM
Post #7

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
Well I've wiped my PC last month so no way I can look at the source code or anything now.

I'll look into that ForEach loop you posted. Thank you.
User is offlineProfile CardPM
+Quote Post

papuccino1
RE: What Loop Function Should I Use Here For Labels.text?
16 May, 2008 - 11:14 AM
Post #8

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
Is there a way i can see my previous posts? The answer lies there.

Edit: Nevermind found it. :3

http://www.dreamincode.net/forums/showtopic45008.htm


CODE
for (int i = 1; i <= 9; i++)
            {
                Label label = (Label)Controls["label" + i];
                label.Text = Convert.ToString(RandomClass.Next(1, 100));              
            }



Ah, it seems my past-me was more fluent than my "now" me.

This post has been edited by papuccino1: 16 May, 2008 - 11:17 AM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: What Loop Function Should I Use Here For Labels.text?
16 May, 2008 - 12:31 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,907



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Ah, yes that is another way of doing it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:56AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month