c# - Generation, adding, editing and deleting by the same button in the same textbox -


i have code winform button, either generates random line text file or lets me add new sentence file. (depending on radiobutton active).

private void button1_click(object sender, eventargs e)        {             string filepath = @"c:\file.txt";  if (radiobuttonnew.checked)       {            string[] lines = file.readalllines(filepath);            random rand = new random();            string sentnew = lines[rand.next(0, lines.length)];            textbox.text = sentnew;       }       else       {         file.appendalltext(filepath, textbox.text + environment.newline);         messagebox.show("value added");        } 

but example don't 1 of random results , want not add one.. but rather correct generated result, press button again , change line.

or want delete generated line file @ all. can done within same button , same textbox adding 2 more radio buttons?

i not sure how it, can help? goal have random generation, adding own (these have), editing , deleting of generated lines. problem - don't quite know how tell program edit or delete random line generated textbox.

quick (untested) example of described in comments above:

    private random rand = new random();      private int index = -1;     private list<string> lines = new list<string>();      private void button1_click(object sender, eventargs e)     {         string filepath = @"c:\file.txt";          if (radiobuttonnew.checked)         {             lines = new list<string>(file.readalllines(filepath));             index = rand.next(0, lines.count);             label1.text = "index: " + index.tostring();             textbox.text = lines[index];         }         else if (radiobuttonappend.checked)         {             file.appendalltext(filepath, textbox.text + environment.newline);             lines = new list<string>(file.readalllines(filepath));             index = lines.count - 1;             label1.text = "index: " + index.tostring();             messagebox.show("line added");         }         else if (radiobuttonmodify.checked)         {             if (index >= 0 && index < lines.count)             {                 lines[index] = textbox.text;                 file.writealllines(filepath, lines);                 messagebox.show("line modified");             }             else             {                 messagebox.show("no line selected");             }         }         else if (radiobuttondelete.checked)         {             if (index >= 0 && index < lines.count)             {                 lines.removeat(index);                 file.writealllines(filepath, lines);                 index = -1;                 label1.text = "index: " + index.tostring();                 messagebox.show("line deleted");             }             else             {                 messagebox.show("no line selected");             }         }     } 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -