0

I am trying to create a lottery program that would generate random numbers between 1 - 35. These numbers need to be unique as well, they can not be the same. With C#, running on the .NET framework.

I have been trying to get it to work, but I keep getting errors and I don't understand why. I have been googling and looking at videos - still I don't get what I am doing wrong.

This is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace help
{
    public partial class Form1 : Form
    {
        // The array 
        TextBox[] TextBoxArray;
    
        // the counter on how many times u pressed btn 
        private int counter = 1;

        public Form1()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterScreen;
            // The text boxes name 1 - 7 
            //TextBoxArray = new TextBox[] { Nr1, Nr2, Nr3, Nr4, Nr5, Nr6, Nr7 };
        }

        private void button1_Click(object sender, EventArgs e)
        { 
            NrOfPull.Text = counter.ToString();
            counter++;

            // Keep getting an error 
            // Error CS0443   Syntax error; value  
            // they are talking about the Parse(TextBoxArray[].Text); 
            int number = int.Parse(TextBoxArray[].Text);

            // Loop through 
            Random rand = new Random();

            for (int i = 0; i < number; i++)
            {
                // Generate a alotter by turn 
                int storedNumber;
                int[] randomLottoRow = new int[7];

                for (int row = 0; row < 7; row++)
                {
                    do
                    {
                        storedNumber = rand.Next(1, 35);
                    }
                    while (randomLottoRow.Contains(storedNumber));

                    randomLottoRow[row] = storedNumber;
                }}

            /*
             *  This will only let me generate numbers.. but wanna use Parse...  
           
            Random generator = new Random();
            for (int x = 0; x < 7; x++)
            {
                TextBoxArray[x].Text = generator.Next(1, 35).ToString();
                Console.WriteLine(TextBoxArray[x].Text = generator.Next(1, 35).ToString());

            }
            */

        }
    }
}

Would love to get feedback on what I am doing wrong Thank you so much :)

6
  • 2
    TextBoxArray[].Text is not a valid expression. You need to specifiy which entry you want to use. Try TextBoxArray[counter - 1].Text (the -1 because you have incremented the counter already) Commented Feb 27, 2022 at 16:22
  • Do you want to generate ALL numbers when the button is clicked, or ONE number and display it in the next TextBox per click? Commented Feb 27, 2022 at 19:49
  • I want all the numbers to show up when the button is clicked. Commented Feb 27, 2022 at 19:59
  • @Mihye Hi, any update about this issue. Do you have any other Problems. If the answer can solve your problem, please consider accepting it as answer to change its status to Answered. Commented Feb 28, 2022 at 1:50
  • Yeah I got the Array to Work, thank you for the help :) Commented Mar 2, 2022 at 11:30

2 Answers 2

1

This is one way of generating random sequences of numbers:

using System.Linq;

public static class Ex
{
    static readonly Random rng = new Random();

    /// <summary>
    /// Randoms the sequence.
    /// </summary>
    /// <param name="maxValue">The maximum number in drawing.</param>
    /// <param name="count">The number of numbers drawn.</param>
    public static int[] Lottery(int maxValue, int count)
    {
        return Enumerable.Range(1, maxValue+1)
            .OrderBy((x)=>rng.NextDouble())
            .Take(count).ToArray();
    }
}
static class Program
{
    static void Main(string[] args)
    {
        var seq = Ex.Lottery(35, 8);
        // draw 8 numbers ranging between 1-35
        Console.WriteLine(string.Join(",", seq));
        // 14,24,1,3,25,5,31,30
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here you go:

private TextBox[] TextBoxArray;
private Random rand = new Random();

public Form1()
{
    InitializeComponent();
    TextBoxArray = new TextBox[] { Nr1, Nr2, Nr3, Nr4, Nr5, Nr6, Nr7 };
}        

private void button1_Click(object sender, EventArgs e)
{
    int[] picks = Enumerable.Range(1, 35).OrderBy(x => rand.NextDouble()).Take(7).ToArray();
    for(int i=0; i<picks.Count(); i++)
    {
        TextBoxArray[i].Text = picks[i].ToString();
    }
}

This uses the same approach as John Alexiou for generating the random lottery pick numbers, but then also shows you how to put those numbers into your TextBoxes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.