winforms - Getting same numbers within two event methods on windows form -


i have created takes user input , compares 100 randomly generated numbers. once click guess button result shown. aid user supposed give them hint when hover mouse on label, hint should either 3 higher or 3 lower actual number. cannot figure out how random number generated when user hits guess button equal number hover event. sorry code, appreciated.

here how creating random number:

   public partial class form1 : form    {     int[] rndarray = new int[100];      public void getnumbers()     {         random random = new random();         (int x = 0; x < rndarray.length; x++)         {             rndarray[x] = random.next(1, 100);          }     } 

here guss button event:

 private void guess_click(object sender, eventargs e)     {         getnumbers();         (int x = 0; x < rndarray.length; x++)         {                 if (convert.toint32(textbox1.text) == rndarray[x])                 {                      result.text = "you win!";                     correct.text = "correct: ";                 }                 else                 {                      result.text = "sorry - loose; number is: " + rndarray[x];                     incorrect.text = "incorrect: ";                 }         } 

and mouse hover event:

private void mousehere_mousehover(object sender, eventargs e)     {         getnumbers();         (int x = 0; x < rndarray.length; x++)             hint.text = "it's not " + (rndarray[x] +- 3);     } 

i see 3 possible issues.

first, @ beginning of mouse hover , guess click functions, call getnumbers, generates 100 numbers , assigns them array. should generate them once per game. recommend calling once @ beginning of each game (perhaps in formshown or formload event handlers), , not calling again until next game begins. otherwise, numbers keep changing.

second, inside mouse hover function, have loop assigns text "hint" 100 times. first 99 hints not accurate, end of mouse hover event display hint last number. need identify array element give hint for, , assign appropriate hint display.

third, +- operator not actual operator in c#. if compiling , running, it's interpreting in manner such hint.text = "it's not " + (rndarray[x] + (-3); recommend using random object generate number, using whether it's odd or determine whether add or subtract. make sure don't reassign array.

edit: regards figuring out whether add or subtract hint, make sure don't randomly generate hint each time hover; once number , store hint. otherwise, hovering on few times show both possible hints.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -