在BtnNext_Click
方法,它只是從文本文件閱讀,就像是一個不同的文本文件,而不是從已經被打開了一個同一個文本文件。它不會逐行進行。 我需要幫助閱讀不同方法
下面是代碼:
public void ScrubData()
{
string FileName1;
string FilePath1;
// Display an OpenFile Dialog box for user
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt Files|*.txt";
openFileDialog1.Title = "Select a txt File";
// Show the Dialog. If user clicked OK in the 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 it 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 one 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
我需要重複使用的變量,如在我的下一個方法「strFileName」。
我通過在文本文件中的每一行創建一個以前&下一個按鈕循環:
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
如果你看到什麼即時消息說:該設計是:用戶打開文件,從文件中的文本的第一行顯示在窗體上,然後我有一個'上一個'和'下一個'按鈕,我想通過同一個文件中的文本行來循環。
@Tim是啊我想我知道你在說什麼在這裏看看這個:
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 are you sure you want to reset the data
DialogResult dialogResult1 = MessageBox.Show("Are you want to reset the data?", "ALERT", MessageBoxButtons.YesNo);
if (dialogResult1 == DialogResult.Yes)
{
// Resets all the 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)
{
// Do 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 = "";
for (int i = 0; i < lastNameLetters; i++)
{ // Replace Last Name
randomString += lettersTwo
[randLetters.Next(0, 25)].ToString();
}
Txt11ScrubLastNam.Text = randomString; //
#endregion
#region
var newAccountNum = ""; //
int numOfCharacters = 4; // # to leave behind
for (var i = 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)
{ // Get 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 from the Data section");
}
}
public void ScrubData()
{ // Begin Scrub Method
我真的不知道問題是什麼,但是整個'XXX'/'XXX結尾'確實會降低您的代碼可讀性。 –
根據你發佈的內容,很難說出你有什麼問題(如果有的話)。什麼是預期的行爲與發生了什麼?你是什麼意思,它看起來像在閱讀,就像它是一個不同的文件?您需要使用類字段(或屬性)來存儲文件路徑,以便您可以在不同的方法中重用它。 – JNYRanger
我想要一個動態文件名不只是像「bob.txt」那樣的靜態 – Colin