4

Question 1: whatever the user enters in the text box displays in the listbox but other text is showing up first then what the user enters shows up at the end.

Question 2: my StreamReader / StreamWriter I keep getting 1601 error code to new to 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 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

NoName
  • 7,940
  • 13
  • 56
  • 108
T.J.
  • 43
  • 4
  • Could you please **at least** post the complete and correct error message - not just "something like 1061" .... And **where** in your code does that error occur - on which line of code?? – marc_s Feb 26 '16 at 16:43
  • Error 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?) LoadButton.Filename, and the saveOrder.filename as well as the showDialog () in the saveOrderDialog method and the Load Button method – T.J. Feb 26 '16 at 16:45
  • 1
    Please **do not** put code samples or sample data into comments - since you cannot format it, it's **extremely hard** to read it.... Instead: **update** your question by editing it to provide that additional information! Thank you. – marc_s Feb 26 '16 at 16:46

2 Answers2

3

I don't really understand your first question, what other text shows up first?

For your second question, there are actually other problems I think you have. Firstly you are using:

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

and

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

From what I can tell, these are buttons you are clicking, which won't have a ShowDialog method.

The error you are actually looking at is due to you trying to get a FileName property from what I still suspect are buttons (and backed up by the error message - 'Button' does not contain a definition for 'FileName'):

loadButton.FileName

and

saveOrder.FileName

I'm suspicious what you are actually supposed to be using are OpenFileDialog and SaveFileDialog controls, but you've actually referenced the buttons you're clicking on instead.

Jonno
  • 306
  • 3
  • 19
  • okay so i have 3 buttons and a list box and a text box 1) button is add to list button which displays what the user inputs in the list box 2) then i have a save button that is supposed save what is in the list box to a file.(3) the load button which is supposed to well load i guess – T.J. Feb 26 '16 at 17:15
  • @T.J. Did you copy and paste the code from somewhere? I'm pretty sure you want an `OpenFileDialog` and a `SaveFileDialog` too, they are what you should be doing `ShowDialog()` and `.Filename` on. – Jonno Feb 26 '16 at 17:17
  • I only can think TJ don't know what is he doing. Does he really mean to have `WriteLine` in the `Load` function? – NoName Feb 26 '16 at 18:13
  • @Sakura Mmm good point. Looks like starting from scratch might be the best option. – Jonno Feb 26 '16 at 18:14
  • 1
    No, I think he may need to read some basic toturial about WinForm before he continue this project. – NoName Feb 26 '16 at 18:19
  • how do i get it to work i dont know how to fix it or how to go about fixing – T.J. Feb 26 '16 at 19:31
1

I believe Jonno is correct with the about the SaveFileDialog and OpenFileDialog. When the save or load button are clicked you should open one of those dialogs and wait for its result. Then on its okay you can do SomeFileDialog.FileName instead of loadButton.FileName

Your first question is (if I interpreted it correctly) that you see the stuff + what the user entered.

This is because you are doing

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

instead try

displayFoodOrder.Items.Add(foodText.Text);

You are ToString-ing the whole control and not just the text in its text field.

Also, as a side note, your regex is matching the first character because of the ^ and not all characters. You can use . if you want any character to be [a-zA-Z]

Krill
  • 293
  • 1
  • 4
  • 17
  • Ahh yes, it'll be a `TextBox`. The level of guesswork is going through the roof :) – Jonno Feb 26 '16 at 17:36
  • ill repost it this better and describe what im trying to do – T.J. Feb 26 '16 at 18:43
  • changin that to a .text gives me a string my showdialog and the .filenames are my problems – T.J. Feb 26 '16 at 19:28
  • look up examples for openfiledialog and savefiledialog. They will show you exactly what you need. You would want to place that in the button clicks. But you shouldn't need to reference the button inside the buttons click, which is one of the things you are doing wrong here – Krill Feb 26 '16 at 20:43