views:

89

answers:

2
Random r = new Random();
int sayi = r.Next(1, 49);

textBox1.Text = sayi.ToString();

This code shows the first random number in textbox1, but I also need to get the output for the other textboxes as well:

(first click)--->Textbox1: 3
(second click)--->textbox2: 24
(third click)---> textbox3: 32
A: 

If you just have text boxes on your form (so that they are effectively fields in your Form class), you can add an int counter field to your class, initialize it to 0, and have (note: untested code):

Random r = new Random();
int sayi = r.Next(1, 49);
string textBoxName = "textBox"+counter;

FieldInfo fi = GetType().GetField(textBoxName);
TextBox currentTextBox = (TextBox)fi.GetValue();
currentTextBox.Text = sayi.ToString();
couter++;

plese note that this approach is rather ugly. It would be nicer to have these text boxes in a list or array and find them using the index instead of using the name of the field.

Grzenio
+1  A: 
// Declare Click Counter Global

private int clickCounter = 0;

// On Button Click Event
Random r = new Random();

if(clickCounter == 0)
{
textBox1.Text = Convert.ToInt32(r.Next(1,49));
clickCounter++;
}

if(clickCounter == 1)
{
textBox2.Text = Convert.ToInt32(r.Next(1,49));
clickCounter++;
}

if(clickCounter == 2)
{
textBox3.Text = Convert.ToInt32(r.Next(1,49));
clickCounter++;
}

This code block gives you what you want.

Serkan Hekimoglu
yeah that was what i am looking, tesekkurler serkan bey. gereksiz degil tıklamayı sayacak bir sey ariyodum bu yardimci olucak.
karatekid
So you need to ask How can I track click count? :) Anyway, Im glad your problem solved. Good Luck. Iyi calismalar.
Serkan Hekimoglu
Thanks for editing :) (I wrote this on Notepad)
Serkan Hekimoglu
textBox1.Text = (r.Next(1,49).ToString()); is the right way. there is no need to convert integer..
karatekid