Reputation: 99
I'm trying to make a small program that will select a random line form a richtextbox and print it out on a label.
I have also tried to make a timer that will display random lines on the label before it stops.
but when I run the program it gives me this error message:
Random min 'minValue' can not be greater then 'maxValue'
This is my code:
public partial class Form1 : Form
{
private int ran;
private int ranP;
private int max;
Random rnd = new Random();
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
int max = richTextBox1.Lines.Count();
int ranP = rnd.Next(1, max);
int ran = 0;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = "The Winner is: " + richTextBox1.Lines[ranP];
ran = rnd.Next(1, 11);
ranP = rnd.Next(1, max);
if(ran == 11){
timer1.Enabled = false;
}
}
}
The error come on this line in my timer:
ranP = rnd.Next(1, max);
Does anyone know why this happens?
Upvotes: 1
Views: 2835
Reputation: 10958
In your button1_Click_1
you declare a local variable max
which shadows your instance variable this.max
. When you use max
in the timer1_Tick
method it refers to that instance variable which is never assigned to.
Changing the assignment in the button1_Click_1
to
max = richTextBox1.Lines.Count();
should help.
Upvotes: 2