2016-02-26 22 views
4

問題1:無論用戶在文本框中輸入的內容顯示在列表框中,但其他文本先顯示出來,然後用戶輸入的內容顯示在最後。列表框中的StreamReader和Writer

問題2:我的StreamReader/StreamWriter我不斷收到1601錯誤代碼,以新的C#,所以我不知道所有的條款。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace foodOrderApp 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      //textDialog = new SaveFileDialog(); 
      //textDialog.Filter = "" 
     } 

     private void addToListButton_Click(object sender, EventArgs e) 
     { 
      if (!System.Text.RegularExpressions.Regex.IsMatch(foodText.Text, "^[a-zA-Z]")) 
      { 
       MessageBox.Show("This textbox only accepts alphebetical characters"); 
      } 
      else 
      { 
       displayFoodOrder.Items.Add(foodText.ToString()); 
      } 
     } 

     private void loadButton_Click(object sender, EventArgs e) 
     { 
      if (loadButton.ShowDialog() == DialogResult.OK) 
      { 
       StreamWriter sw = new StreamWriter(
            new FileStream(loadButton.FileName, 
             FileMode.Create, 
             FileAccess.ReadWrite) 
            ); 

       sw.WriteLine(displayFoodOrder.Text); 

       sw.Close(); 
      } 
     } 

     private void saveOrder_Click(object sender, EventArgs e) 
     { 
      if (saveOrder.ShowDialog() == DialogResult.OK) 
      { 
       StreamReader sr = new StreamReader(
           new FileStream(saveOrder.FileName, 
            FileMode.Open, 
            FileAccess.Read) 
            ); 

      }//end if 
     } 
    } 
} 

錯誤:

CS1061 'Button' does not contain a definition for 'FileName' and no extension method 'FileName' accepting a first argument of type 'Button' could be found (are you missing a using directive or an assembly reference?)
line 42

+0

能否請**至少**發佈完整和正確的錯誤信息 - 不僅僅是「類似1061」....和**其中**在你的代碼是否發生錯誤 - 在哪行代碼? –

+0

錯誤\t CS1061 \t'Button'不包含'FileName'的定義,並且沒有找到接受'Button'類型的第一個參數的擴展方法'FileName'(您是否缺少using指令或程序集引用?)\t LoadButton.Filename和saveOrder.filename以及saveOrderDialog方法中的showDialog()和Load Button方法 –

+1

請**不要**將代碼示例或示例數據放入註釋中 - 因爲您無法對其進行格式化,所以它* *非常困難**閱讀它....相反:**更新**您的問題通過編輯它提供額外的信息!謝謝。 –

回答

3

我真的不明白你的第一個問題,還有什麼其他的文本首先顯示了?

對於第二個問題,其實我還有其他問題。首先你使用:

if (loadButton.ShowDialog() == DialogResult.OK) 

if (saveOrder.ShowDialog() == DialogResult.OK) 

從我可以告訴,這些都是你點擊按鈕,這不會有ShowDialog方法。

你實際上是在尋找的錯誤是由於你想從我仍然懷疑是按鈕(以及由該錯誤消息備份 - 'Button' does not contain a definition for 'FileName')一FileName屬性:

loadButton.FileName 

saveOrder.FileName 

我懷疑你實際上應該是使用的OpenFileDialogSaveFileDialog控制,但you'v e實際上引用了您點擊的按鈕。

+0

好吧,所以我有3個按鈕和一個列表框和一個文本框1)按鈕添加到列表按鈕,它顯示了用戶在列表框中輸入的內容2)然後我有一個保存按鈕,應該保存列表中的內容(3)應該很好加載的加載按鈕我猜 –

+0

@TJ你是否從某處複製並粘貼了代碼?我很確定你想要一個'OpenFileDialog'和一個'SaveFileDialog',它們就是你應該在'ShowDialog()'和'.Filename'上執行的操作。 – Jonno

+0

我只能認爲TJ不知道他在做什麼。他真的有意在'Load'函數中擁有'WriteLine'嗎? – Sakura

1

我相信Jonno是正確的關於SaveFileDialogOpenFileDialog。點擊保存或載入按鈕時,應打開其中一個對話框並等待其結果。然後在它的好的,你可以做SomeFileDialog.FileName,而不是loadButton.FileName

你的第一個問題是(如果我解釋正確),你看到的東西+用戶輸入的內容。

這是因爲你在做

displayFoodOrder.Items.Add(foodText.ToString());

而是試圖

displayFoodOrder.Items.Add(foodText.Text);

您的ToString-ING整個控制,而不只是在其文本字段。

此外,作爲一個附註,你的正則表達式匹配的第一個字符,因爲^而不是所有字符。你可以使用.如果你想要任何字符[a-zA-Z]

+0

啊,是的,它會是一個'TextBox'。猜測的水平正在通過屋頂:) – Jonno

+0

虐待這個更好的轉貼,並描述我試圖做什麼 –

+0

changin,以.text給我一個字符串我的showdialog和.filenames是我的問題 –