【问题标题】:ComboBox & ListBox are not displaying contents of text file组合框和列表框不显示文本文件的内容
【发布时间】:2021-12-25 06:34:19
【问题描述】:

我有一项任务是为一家冰淇淋店创建一个订单表格,为了将配料和口味放入列表和组合框中,我需要从一个文本文件中读取它。我按照说明和非常糟糕的视频教程进行操作,并且我的 bin/debug 文件夹中有我的文本文件,文件中包含了各自的风格和配料。它运行良好,除了下拉列表是空白的,列表框也是如此。这是 C#,我正在使用 Windows 窗体应用程序。这是我的代码

namespace Lab5_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            saveButton.Text = "&Save";
            exitButton.Text = "&Exit";

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PopulateBoxes();

            flavorsComboBox.SelectedItem = "Vanilla";
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                // writes order information to output file
                StreamWriter outputFile;
                outputFile = File.AppendText("Orders.txt");

                outputFile.WriteLine(DateTime.Now.ToString("mm/dd/yyyy"));

                if (sugarConeRadioButton.Checked)
                {
                    outputFile.WriteLine("Sugar Cone");
                }
                else
                {
                    outputFile.WriteLine("Waffle Cone");
                }

                outputFile.WriteLine(flavorsComboBox.SelectedItem.ToString());

                for (int count = 0; count < toppingsListBox.Items.Count; count++)
                {
                    if (toppingsListBox.GetSelected(count))
                    {
                        outputFile.WriteLine(toppingsListBox.Items[count]);
                    }
                }

                outputFile.WriteLine();
                outputFile.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            sugarConeRadioButton.Checked = true;
            flavorsComboBox.SelectedItem = "Vanilla";
            toppingsListBox.ClearSelected();
            sugarConeRadioButton.Focus();

        }

        private void PopulateBoxes()
        {
            try
            {
                StreamReader inputFile;

                inputFile = File.OpenText("Flavors.txt");
                while (inputFile.EndOfStream)
                {
                    flavorsComboBox.Items.Add(inputFile.ReadLine());
                }
                inputFile.Close();

                inputFile = File.OpenText("Toppings.txt");
                while (inputFile.EndOfStream)
                {
                    toppingsListBox.Items.Add(inputFile.ReadLine());
                }
                inputFile.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.Close();
            }
        }

    }
}

【问题讨论】:

  • 您需要debug your codewhile (inputFile.EndOfStream) {} 可能不会像你认为的那样做。
  • PopulateBoxes 中的两个 while 循环都有同样的问题。使用调试器单步调试代码,看看是否能找出原因。 (如果你只是非常仔细地阅读代码并思考它在做什么,你可能甚至不需要调试器就可以弄清楚。)
  • 把你的两个文本文件放在你的项目源文件夹中。这样您就可以像编辑任何其他文件一样编辑它们。右键单击每个文件并选择属性。 (从内存中,在手机上) 将文件类型属性设置为 Content,将复制属性设置为 始终复制。现在,当您编译代码时,文件将被复制到项目的输出文件夹(EXE 所在的位置)
  • 我投票结束这个问题,因为要求其他人调试您的代码不是问题。

标签: c# windows


【解决方案1】:

如果您希望逐行查看整个文件内容,如果您使用 System.IO.File.ReadAllLines 帮助程序,文件 IO 生活会更轻松

例如

    private void PopulateBoxes()
    {
        try
        {
            flavorsComboBox.DataSource = File.ReadAllLines("Flavors.txt").ToList();

            toppingsListBox.DataSource = File.ReadAllLines("Toppings.txt").ToList();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

或者

    private void PopulateBoxes()
    {
        try
        {
            flavorsComboBox.Items.AddRange(File.ReadAllLines("Flavors.txt"));

            toppingsListBox.Items.AddRange(File.ReadAllLines("Toppings.txt"));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2021-11-19
    • 2016-06-01
    相关资源
    最近更新 更多