forms - load from a txt to a list box in C# -


i have saving part down , know works, when click load button, not display have saved text boxes go saying.txt file

using system; using system.io; using system.windows.forms;  namespace windowsformsapplication2 { public partial class grades : form {      private streamwriter fil;     public grades()     {         initializecomponent();     }      private void form1_load(object sender, eventargs e)     {         try         {             fil = new streamwriter("saying.txt"); //this txt file         }         catch (directorynotfoundexception exc)         {             lstbxdisplay.text = "nothing " +                 exc.message;         }         catch (system.io.ioexception exc)         {             lstbxdisplay.text = exc.message;         }     }            // saving files saying.txt      private void btnsaveas_click(object sender, eventargs e)     {         try         {             fil.writeline(txtbxlastname.text);             txtbxlastname.text = "";             txtbxlastname.focus();         }         catch (system.io.ioexception exc)         {             lstbxdisplay.text = exc.message;         }     }      // next load button load files list/display box     private void btnload_click(object sender, eventargs e)     {           string invalue;          try         {             using (streamreader infil =                 new streamreader("saying.txt"))             {                 invalue = infil.readline();                 while (invalue != null)                 {                     invalue = infil.readline();                     if (invalue != null)                         this.lstbxdisplay.items.add(invalue);                 } // end of while             } // end of using         }         catch (system.io.ioexception exc)         {             lstbxdisplay.text = exc.message;         }     }      private void grades_formclosing(object sender, formclosingeventargs e)     {         try         {             fil.close();         }         catch         {          }     }   } } 

any reason why not loading list box? have tried both label , text box display message , neither of them work. debugged program , executing fine

you have multiple issues here point out 2 main issues code working.

  1. you not closing stream when try save. if stream stays open, never able read values when try "load" file. need call fil.close(); @ end of btnsaveas_click method.

this how save should read.

    fil.writeline(txtbxlastname.text);     txtbxlastname.text = "";     txtbxlastname.focus();      fil.close(); 
  1. you're skipping first line of file in "load" method. call infil.readline(); in loop, call again before add listbox. need move second readline(). if ever writing single line file, existing code skip first line , try read again null (no second line). so, never add listbox.

this how reads should ordered.

    using (streamreader infil = new streamreader("saying.txt"))     {         invalue = infil.readline();          while (invalue != null)         {             this.lstbxdisplay.items.add(invalue);              invalue = infil.readline();          } // end of while     } // end of using 

those changes working. point out, going reading , writing file wrong. should not opening stream in form load , waiting button clicks writing/reading stream. unless have reason doing, should opening stream, performing read/write operation, , closing stream right away. simple file io, suggest using different mechanism. @ msdn system.io.file class easier methods read lines or write lines file.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -