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 :
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.