Click here to Skip to main content
15,908,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo, i have 2 textboxes and file test.txt it contains some words, each word is on single line and I want to randomly pick one word to textbox1, and to textbox2 i write some word (it not nessessary to write same word like in textbox1) and through button i save word in textbox2 to test.txt file. Thank you for answer.
Posted
Comments
Peter Leow 28-Feb-15 9:44am    
Do you mean:
1. Textbox1 to display a randomly picked word from text.txt
2. User enters some word in Textbox2 and click a button to save it to test.txt.
Member 10808387 28-Feb-15 9:53am    
Yes please

1 solution

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

But I'll give you a few clues:
Start by working out how you want to read the file: because words aren't all the same length, and text files don't really have lines as such (they have line termination characters, which isn't the same thing) that means that you can't just say "read line 14" or "read line 723" - the reader doesn't know where the lines start. The easiest way round this problem is to read the whole file into separate lines: the File.ReadAllLines method will help here.
Randomness is easy: Create a class level instance of the Random class, and use Random.Next to get a random number (it has useful overloads - see MSDN for details).
Adding data to the file is also pretty easy: the File.AppendAllText method will do it, provided you remember to include a '\n' character after each word you add as the line separator.

Give it a try - this isn't complicated, not really.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900