c# - Read Same Text File in Different Methods -
in btnnext_click
method it's reading text file different text file, , not 1 opened. not go line line. need
here code:
public void scrubdata() { string filename1; string filepath1; // display openfile dialog box user openfiledialog openfiledialog1 = new openfiledialog(); openfiledialog1.filter = "txt files|*.txt"; openfiledialog1.title = "select txt file"; // show dialog. if user clicked ok in dialog if (openfiledialog1.showdialog() == system.windows.forms.dialogresult.ok) { try { string strfilename = openfiledialog1.filename; string strfilepath = system.io.path.getdirectoryname(strfilename); string filename = system.io.path.getfilenamewithoutextension(strfilename); string strfilenameandpathnew = strfilepath + openfiledialog1.initialdirectory + "\\" + filename + "_scrubbed.txt"; // if scrubbed file exists, delete first if (system.io.file.exists(strfilenameandpathnew)) { system.io.file.delete(strfilenameandpathnew); } // end if if (system.io.file.exists(strfilename)) { int linecount = system.io.file.readalllines(strfilename).length; system.io.streamreader file = new system.io.streamreader(strfilename); // status label lblstatus.text = "file loaded successfully"; lblstatus.visible = true; string line; while ((line = file.readline()) != null) { const char delim = '|'; // messagebox.show(line); string[] word = line.split(delim); txt2normaccnum.text = word[3]; //string accscrubbed = replacedata(word[0],"ssn"); txt3normamnt.text = word[4]; txt4normfirstnam.text = word[1]; txt5normlastnam.text = word[2]; txt6normss.text = word[0]; txt7normitem.text = word[5]; } // end while } // end if else { // status label lblstatus.text = "file load failed!"; lblstatus.visible = true; } // end else // text box 1 code: filename1 = openfiledialog1.filename; txt1.text = filename1; // } // end try catch (exception e1) { if (e1.source != null) { console.writeline("ioexception source: {0}", e1.source); throw; } // end if } // end catch } // end if } // end scrub method
i need reuse variables such "strfilename" in next method.
i creating previous & next buttons cycle through each line in text file:
public void btnnext_click(object sender, eventargs e) { streamreader myreader2 = new streamreader("colin.txt"); string line2 = ""; while (line2 != null) { line2 = myreader2.readline(); if (line2 != null) { const char delim = '|'; // messagebox.show(line); string[] word = line2.split(delim); txt2normaccnum.text = word[3]; txt3normamnt.text = word[4]; txt4normfirstnam.text = word[1]; txt5normlastnam.text = word[2]; txt6normss.text = word[0]; txt7normitem.text = word[5]; //txt12scrubss.text; //txt10scrubfirstnam.text; //txt11scrublastnam.text; //txt8scrubacctnum.text; //txt9scrubamt.text; //txt13scrubitem.text; } } myreader2.close(); } // end method
if see im saying: design is: user opens file, first line of text file displayed on form, have 'previous" , 'next' button want cycle through lines of text same file.
@tim yeah think know saying here @ this:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.io; namespace project2_datascrubber { public partial class form1 : form { // begin class #1 public form1() { // begin main method initializecomponent(); // messagebox.show(getrandomnumbers().tostring()); } // end main method private void btn4_click(object sender, eventargs e) { // btn4 click method // closes form 1 this.close(); } // end method private void btn3_click(object sender, eventargs e) { // btn3 click method // display alert message box of sure want reset data dialogresult dialogresult1 = messagebox.show("are want reset data?", "alert", messageboxbuttons.yesno); if (dialogresult1 == dialogresult.yes) { // resets data, textboxes, ect txt1.text = "no file loaded."; txt2normaccnum.clear(); txt3normamnt.clear(); txt4normfirstnam.clear(); txt5normlastnam.clear(); txt6normss.clear(); txt7normitem.clear(); txt8scrubacctnum.clear(); txt9scrubamt.clear(); txt10scrubfirstnam.clear(); txt11scrublastnam.clear(); txt12scrubss.clear(); txt13scrubitem.clear(); txt14scrubyesno.clear(); lblstatus.visible = false; } else if (dialogresult1 == dialogresult.no) { // nothing } } // end method public void btn1_click(object sender, eventargs e) { // btn1 click method scrubdata(); } // end method public void btn2_click (object sender, eventargs e) { if (txt2normaccnum.text != "" || txt3normamnt.text != "" || txt4normfirstnam.text != "" || txt5normlastnam.text != "" || txt6normss.text != "" || txt7normitem.text != "") { // txt12scrubss.text = getrandomnumbers().tostring(); // replace ss textbox // #region int lastnameletters = txt5normlastnam.text.length; // string letterstwo = "abcdefghijklmnopqrstuvwxyz"; random randletters = new random(); string randomstring = ""; (int = 0; < lastnameletters; i++) { // replace last name randomstring += letterstwo [randletters.next(0, 25)].tostring(); } txt11scrublastnam.text = randomstring; // #endregion #region var newaccountnum = ""; // int numofcharacters = 4; // # leave behind (var = 0; i<txt2normaccnum.text.length - numofcharacters; i++) { newaccountnum += "x"; // replace account number } newaccountnum += txt2normaccnum.text.substring (txt2normaccnum.text.length - numofcharacters); txt8scrubacctnum.text = newaccountnum; // #endregion #region double moneyamountdoub = 0; // string moneyamountstr = ""; moneyamountstr = txt3normamnt.text; moneyamountdoub = convert.todouble(moneyamountstr); if (moneyamountdoub > 100.00) { // yes or no answer txt14scrubyesno.text = "yes"; } else { txt14scrubyesno.text = "no"; } // #endregion txt10scrubfirstnam.text = txt4normfirstnam.text; txt13scrubitem.text = txt7normitem.text; txt13scrubitem.text = txt7normitem.text; txt9scrubamt.text = txt3normamnt.text; } else { messagebox.show("error: information missing data section"); } } public void scrubdata() { // begin scrub method
there couple of things need do. first, need create class level variables (more referred fields) hold information needs accessed different methods.
secondly, need keep track of (what line) in file, because everytime create streamreader
, position reader @ first line. corollary, roguebukkitdev said, should read file 1 time , dump list<string>
or array (string[]
). increase or decrease current position in collection based on user's direction - forward or back.
it this:
public partial class form1 : form { // class level fields private string filename = string.empty; private string[] filelines; private int currentline = 0; const char delim = '|'; public void scrubdata() { // display openfile dialog box user openfiledialog openfiledialog1 = new openfiledialog(); openfiledialog1.filter = "txt files|*.txt"; openfiledialog1.title = "select txt file"; // show dialog. if user clicked ok in dialog if (openfiledialog1.showdialog() == system.windows.forms.dialogresult.ok) { try { filename = openfiledialog1.filename; newfilename = string.format(@"{0}\{1}_scrubed.txt", openfiledialog1.initialdirectory, path.getfilenamewithoutextension(filename)); filelines = file.readalllines(filename); currentline = 0; } } } public void btnnext_click(object sender, eventargs e) { if (currentline <= filelines.length) { string line2 = filelines[currentline]; string[] word = line2.split(delim); txt2normaccnum.text = word[3]; txt3normamnt.text = word[4]; txt4normfirstnam.text = word[1]; txt5normlastnam.text = word[2]; txt6normss.text = word[0]; txt7normitem.text = word[5]; currentline = currentline + 1; } } }
lots of changes here, , ommitted code wasn't relevant example.
first, declare 4 fields - 1 filename, 1 scrubbed filename, 1 current line of file, , 1 constant delimiter.
next, in scrubdata
method file selected user, calls file.readalllines()
, return array of strings (one array element per line), , stores in class level field filelines
. sets currentline
0, in case user choosing different file before exiting program.
in btnnext_click
event handler, reads current line array of lines (filelines
) - after making sure won't have index out bounds error, splits them on delimiter , populates text boxes. increases currentline
one.
to go back, same logic, except decrease currentline
1 (and check make sure don't go past 0).
this basic outline of we've been trying explain. may need modify suit actual needs, principle remains same.
Comments
Post a Comment