【问题标题】:StreamReader and Writer from a list box列表框中的 StreamReader 和 Writer
【发布时间】:2016-06-09 23:27:12
【问题描述】:

问题 1:用户在文本框中输入的任何内容都会显示在列表框中,但其他文本会先显示,然后用户输入的内容才会显示在最后。

问题 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“按钮”不包含“文件名”的定义,并且找不到接受“按钮”类型的第一个参数的扩展方法“文件名”(您是否缺少 using 指令或程序集引用?)
第 42 行

【问题讨论】:

  • 能否请您至少发布完整且正确的错误消息 - 而不仅仅是“类似于 1061”...以及 where 在您的该错误发生在代码的哪一行??
  • 错误 CS1061 'Button' 不包含 'FileName' 的定义,并且找不到接受“Button”类型的第一个参数的扩展方法 'FileName'(您是否缺少 using 指令或程序集参考?)LoadButton.Filename、saveOrder.filename以及saveOrderDialog方法和Load Button方法中的showDialog()
  • 不要将代码示例或示例数据放入 cmets - 因为您无法对其进行格式化,所以阅读它非常困难....而是:更新您的问题,编辑它以提供附加信息!谢谢。

标签: c# streamreader streamwriter


【解决方案1】:

我不太明白你的第一个问题,还有什么文字首先出现?

对于您的第二个问题,我认为您实际上还有其他问题。首先你正在使用:

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

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

据我所知,这些是您正在单击的按钮,它们没有 ShowDialog 方法。

您实际看到的错误是由于您试图从我仍然怀疑是按钮的地方获取 FileName 属性(并由错误消息支持 - 'Button' does not contain a definition for 'FileName'):

loadButton.FileName

saveOrder.FileName

我怀疑您实际上应该使用的是 OpenFileDialogSaveFileDialog 控件,但您实际上引用了您正在单击的按钮。

【讨论】:

  • 好的,所以我有 3 个按钮和一个列表框和一个文本框 1) 按钮添加到列表按钮,显示用户在列表框中输入的内容 2) 然后我有一个保存按钮应该将列表框中的内容保存到文件中。(3)我猜应该很好加载的加载按钮
  • @T.J.您是否从某处复制并粘贴了代码?我很确定你也想要OpenFileDialogSaveFileDialog,它们是你应该做的ShowDialog().Filename on。
  • 我只能认为TJ不知道他在做什么。他真的是想在Load 函数中有WriteLine 吗?
  • @Sakura 嗯,好点。看起来从头开始可能是最好的选择。
  • 不,我认为他在继续这个项目之前可能需要阅读一些关于 WinForm 的基本教程。
【解决方案2】:

我相信 Jonno 关于SaveFileDialogOpenFileDialog 的说法是正确的。单击保存或加载按钮时,您应该打开其中一个对话框并等待其结果。然后没关系,您可以使用SomeFileDialog.FileName 而不是loadButton.FileName

您的第一个问题是(如果我解释正确的话)您看到了内容 + 用户输入的内容。

这是因为你在做

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

不如试试

displayFoodOrder.Items.Add(foodText.Text);

您正在对整个控件进行 ToString,而不仅仅是其文本字段中的文本。

另外,作为旁注,您的正则表达式匹配第一个字符,因为 ^ 而不是所有字符。如果你想让任何字符成为[a-zA-Z],你可以使用.

【讨论】:

  • 啊,是的,它将是TextBox。猜测的程度正在飙升:)
  • 我会更好地转发它并描述我想要做什么
  • 更改为 .text 给了我一个字符串,我的 showdialog 和 .filenames 是我的问题
  • 查找 openfiledialog 和 savefiledialog 的示例。他们会准确地告诉你你需要什么。你会想把它放在按钮点击中。但是您不需要在按钮单击中引用按钮,这是您在这里做错的事情之一
猜你喜欢
  • 1970-01-01
  • 2011-08-24
  • 2017-06-29
  • 2014-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多