Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to display all the numbers of my random c# form

I want to generate 500 random number between 1 to 200 and display all this number, but i have just one on the screen.

        private void cmdGenererNombres_Click(object sender, EventArgs e)
    {
        Random rand = new Random(500);
        int number = rand.Next(1, 201);

        FileStream fs = new FileStream("Binaire.bin", FileMode.Create, FileAccess.Write, FileShare.None);
        BinaryWriter bw = new BinaryWriter(fs);
        bw.Write(number);

        bw.Close();
        fs.Close();
    }

    private void cmdAfficherNombres_Click(object sender, EventArgs e)
    {
        FileStream fsSource = new FileStream("Binaire.bin", FileMode.Open, FileAccess.Read, FileShare.None);
        BinaryReader br = new BinaryReader(fsSource);

        MessageBox.Show(br.ReadByte().ToString());

        br.Close();
        fsSource.Close();
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could use a combination of Chunk and string.join to combine all the numbers into a single, large string.

var rand = new Random(42);
var rows = Enumerable.Range(0, 500)
    .Select(i => rand.Next(0, 201))
    .Chunk(50) // number of values per row
    .Select(r => string.Join(", ", r.ToString("000")); // Combine the values in each row. Use fixed width formatting

var block = string.Join(Environment.NewLine, rows); // combine rows into a block

MessageBox.Show(block);

Formatting the numbers as a block should help in keeping the text within the width and height of the screen.

An alternative would be to create your own UI with a scrollable textbox or something similar to show all the numbers.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading