StreamReader and Writer from the list

Question 1: no matter what the user enters into the text field, it is displayed in the list, but first the first text is displayed, then what the user enters appears at the end.

Question 2: my StreamReader/ StreamWriterI keep getting error code 1601 for the new in C #, so I don’t know all the terms.

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
        }
    }
}

Error:

CS1061 'Button' does not contain a definition for “FileName”, and the “FileName” extension method cannot be found that accepts the first argument of type “Button” (do you miss the using directive or assembly references?)
Line 42

+4
source share
2 answers

, ?

, , , . -, :

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

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

, , , , ShowDialog.

, , , FileName , , ( - 'Button' does not contain a definition for 'FileName'):

loadButton.FileName

saveOrder.FileName

, OpenFileDialog SaveFileDialog, , .

+3

, Jonno SaveFileDialog OpenFileDialog. , . , SomeFileDialog.FileName loadButton.FileName

( ), + , .

,

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

displayFoodOrder.Items.Add(foodText.Text);

ToString , .

, , - ^ . ., , [a-zA-Z]

+1

All Articles