Sunday 15 April 2012

winforms - Display random line from .txt file - c# -


I'm trying to do something in C # (winforms), but I'm stuck on a small problem. I have tried all the code related to this problem, but without success please read the problem before answering.

I have 2 functions. I want to make 1 function which will get a random line from the specific .txt file and put it in another.

Here is an example:

  // This is a context menu item, a right click menu item that needs to be loaded function 1 (PDK object from private zero, object sender e) {function1 ();} Private Zero function 1 () {// code to show the random row from the .txt file and } To reflect   

is far I have tried many codes which were posted on here before stackoverflow.com and I tried several combinations with them. I will paste some of these:


  random rand = new random (); IEnumerable & lt; String & gt; Line = file. Readline (@ "D: \ FirstName. Test"); Var lineterread = round. August (1, lines. Count ()); Var line = lines Skip (line tier read -1) first ();   
  int counter = 0; String line; // Read the file and display it by line. System.IO.StreamReader File = New System IO.StreamReader (@ "D: \ FirstNames.txt"); While ((line = file.readline ()) = null ({) System.Console.WriteLine (line); Counter ++;} file. Stop it (); Sys Tem.Console.WriteLine ("{0} lines.", Counter); // Screen Suspend System.Console.ReadLine ();   

// This works only for the first line, can not make any combination with it (to make it random by other tasks)

 Use of  (Stream Reader Reader = File. OpenText (@ "D: \ FirstName. Txt") {textbox1.Text = reader.ReadLine ();}   
  Line line = file. Redlines (@ "D: \ FirstName. Test"); var R = new random (); var random line = = r. Next (0, lines length = 1); var line = lines [Random line number]; < / Code>  
  string [] rows = file. ReadAlline (@ "D: \ FirstName. Txt"); Random Rand = New Random (); Return Lines [Rand Function To count the file, select a random line and return it. The menu from the ContexMenuStrip function which calls the function is used on TEXTBOX.  

In general, let me type a text box in the textbox Right-click on the text box to show inside and To select the item to load my function, you need a random name from the .txt file. There is a small picture w ith simple explanation

Enter image details here As Fabian Said or randomly declared inside the square bracket and call the method directly

  string [] lines = file. Read offline (@ "C: \ ... \ .... \ YourFile.txt"); Textbox1.Text = Line [New Random (). Next (lines. Lang)];    

No comments:

Post a Comment