c# - How to get values from a text file and set them in a Textbox? -


this question has answer here:

here problem: i've got 2 classes. form 1 creates .txt-file , sets 2 values (strings) in it. want these 2 strings pressing button(bdirekt), , set each string in textbox in form 2.

form 1 (should correct far know, please tell me if i'm wrong):

    public void txfw()     {         string txbetrag = gbetrag.text;         string txmonate = gmonate.text;          string[] vars = new string[] { txbetrag, txmonate };         using (streamwriter sw = new streamwriter(@"c:\users\p2\desktop\variablen.txt"))         {              foreach (string s in vars)             {                 sw.writeline(s);             }         }     } 

form 2 (got no idea how go ahead):

    private void bdirekt_click(object sender, routedeventargs e)     {         using (streamreader sr = new streamreader("variables.txt")) ;          string line = "";         while ((line = sr.readline()) != null)         {             monate2.text =          }      } 

i appreciate help.

try this

stringbuilder sb = new stringbuilder();     using (streamreader sr = new streamreader(@"c:\users\p2\desktop\variablen.txt"))    {                    string line;                     // read , display lines file until                     // end of file reached.                     while ((line = sr.readline()) != null)                    {                       sb.append((line);                    }     }     monate2.text = sb.tostring(); 

update: separate line 1 rest of text, can try this. there better ways achieve this.

stringbuilder sb = new stringbuilder();     string headerline = string.empty;     int currentline = 0;         using (streamreader sr = new streamreader(@"c:\users\p2\desktop\variablen.txt"))        {                        string line;                         // read , display lines file until                         // end of file reached.                         while ((line = sr.readline()) != null)                        {                           currentline++; //increment keep track of current line number.                           if(currentline == 1)                           {                             headerline = line;                             continue; //skips rest of processing , goes next line in while loop                           }                           sb.append((line);                         }         }         header.text = headerline;         monate2.text = sb.tostring(); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -