0
完全不熟悉這一點。根據我點擊的事件按鈕,我需要寫入適當的文本文件。但是,輸入數據寫入相同的文本文件。如何指定寫入並保存的適當文本文件。寫入文本文件
的編碼,從文本文件的名稱除了相同的,我目前使用的是:
private void btnItemAdd_Click(object sender, EventArgs e)
{
string sItem;
string sNumber;
if (rdoDrinks.Checked == false && rdoConfectionary.Checked == false)
{
//Message to remind user to select category
MessageBox.Show("Please select a category");
txtItem.Focus();
}
{
{
if ((rdoDrinks.Checked == true) && (txtItem.Text != "") && (txtItemNumber.Text == ""))
//Message to remind user to enter a number
MessageBox.Show("Please input a number");
txtItemNumber.Focus();
if ((txtItem.Text != "") && (txtItemNumber.Text != ""))
{
//add Item to end of list
lstItems.Items.Add(string.Format("{0, -15} {1, -20}", txtItem.Text, txtItemNumber.Text));
txtCount.Text = lstItems.Items.Count.ToString();
//set focus to the text box
txtItem.Focus();
StreamWriter sw = File.AppendText("Drinks.txt");
{
sItem = txtItem.Text;
sw.WriteLine(sItem);
sNumber = txtItemNumber.Text;
sw.WriteLine(sNumber);
}
MessageBox.Show("Details have been saved");
txtItem.Clear();
txtItemNumber.Clear();
sw.Close();
}
else if ((rdoConfectionary.Checked == true) && (txtItem.Text != "") && (txtItemNumber.Text == ""))
//Message to remind user to enter a number
MessageBox.Show("Please input a number");
txtItemNumber.Focus();
if ((txtItem.Text != "") && (txtItemNumber.Text != ""))
{
//add Item to end of list
lstItems.Items.Add(string.Format("{0, -15} {1, -20}", txtItem.Text, txtItemNumber.Text));
txtCount.Text = lstItems.Items.Count.ToString();
//set focus to the text box
txtItem.Focus();
StreamWriter sw = File.AppendText("Confectionary.txt");
{
sItem = txtItem.Text;
sw.WriteLine(sItem);
sNumber = txtItemNumber.Text;
sw.WriteLine(sNumber);
}
MessageBox.Show("Details have been saved");
txtItem.Clear();
txtItemNumber.Clear();
sw.Close();
}
}
}
}
謝謝 - 在您的建議後,我確實解決了這個問題。有一點,它總是寫入Drinks.txt文件而不是Confectionary.txt文件。但是這已經被排序。 – user3387822
@ user3387822:由於您在這裏是新手,請不要忘記將答案標記爲答案,如果答案足夠。請參閱[FAQ#why-vote] –