Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Count help

Count help

Count help

I am newish to programing and very much a C# newbie. I am trying to learn by doing as much as by reading from books and have set myself the challenge of creating a small program to select 6 numbers from 49 at random. Basically it is a lottery number selector program.

I have a liststring with values 1,2,3 etc to 49. These are then selected and configured as the text property of labels. As they are selected the Count method is used to track of the numbers selected. These numbers are then removed from the liststring.

My program works fine the first time my generate button is clicked. What I want to know is how to reset the program so that if the generate button is clicked again the full list of numbers is available within the liststring. Eg. Reset the count to null.

Any help would be appreciated.

Kind regards

RE: Count help

This is what I have at present;-

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;

namespace lottery_numbers
    public partial class Form1 : Form
        // Use this random object to generate numbers
        Random random1 = new Random();

        // each number is a number that can be selected at random"
        List<string> numberList = new List<string>()
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
                "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
                "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
                "43", "44", "45", "46", "47", "48", "49"
        /// <summary>
        /// Assign a number at random to each of the four label boxes
        /// </summary>
        private void AssignNumbers()
            foreach (Control control in tableLayoutPanel1.Controls)
                Label numLabel = control as Label;
                if (numLabel != null)
                    int randomNumber = random1.Next(numberList.Count);
                    numLabel.Text = numberList[randomNumber];
        public Form1()

        private void generateButton_Click(object sender, EventArgs e)

RE: Count help

there are a couple things i see.
to start you are storing strings instead of numbers. since you are working with numbers, why not store them as numbers?

second. indexes are zero base, not 1 base. so a collection of 3 values are indexed 0,1,2

third. while it's not a huge problem in this scenario, there isn't a need to load all 49 values in when the form first loads.

4th. iterating over the controls is making alot of assumuptions about the UI. it will never change, all controls are labels, etc. it would be more explicit to load the specific controls into a list and iterate over that list.

here is another approach to the scenario. note I'm using a Set<>. sets have a special contextual meaning that a value can appear exactly once in the collection.


var rand = new Random(DateTime.Now.Ticks);
var numbers = new Set<int>();

while(numbers.Count <= number of controls)
   var number = rand.Next(1, 49);
foreach(var number in numbers)
   var control = get next control
   control.Text = number.ToString();

Jason Meckley

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close