如何使用复选框makeStreamReader / Writer来保存所选项目并打开所选项目

时间:2016-03-28 18:00:39

标签: c# checkbox combobox streamreader streamwriter

我的程序应该能够让用户能够使用(组合框)选择冰淇淋和糖浆的味道,并选择三个复选框,如果他们想要坚果,樱桃或洒水。这对我的知识来说是最好的

本程序的其他部分应该允许用户保存订单并稍后使用StreamReader / Writer打开它(因为工作真的很好我不能让它写下来从两个COMBO BOX中选出的东西和检查BOXES不写信。如果我改变平等后的索引号,那么打开它只会打开)

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
   //THIS IS MY SAVE BUTTON USING STREAMWRITER
//flavorBox is the Name of the comboBox that holds 3 flavors of iceCream
//syrupBox is the name of the comboBox that holds 3 syrupFlavors inside the combobox
// my check boxes for the toppings are the IF else if else statments
    {
        SaveFileDialog sfd = new SaveFileDialog();

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            StreamWriter sw = new StreamWriter(
                                            new FileStream(sfd.FileName,
                                                            FileMode.Create,
                                                            FileAccess.Write)
                                                            );
            if (!String.IsNullOrEmpty(syrupBox.Text))
            {
                sw.WriteLine(flavorBox.SelectedItem);
            }

           else if (!String.IsNullOrEmpty(syrupBox.Text))
            {
                sw.WriteLine(flavorBox.SelectedItem);
            }

           else if (Nuts.Checked)
            {
                this.Tag = "checked";
                sw.WriteLine(Nuts);


            }
            else if (Cherries.Checked)
            {
                this.Tag = "checked";
                sw.WriteLine(Cherries);

            }
           else if(Sprinkles.Checked)
            {
                this.Tag = "checked";
                sw.WriteLine(Sprinkles);

            }
            sw.Close();
        }


    }

private void openToolStripMenuItem_Click(object sender, EventArgs e)
//THIS IS MY OPEN METHOD WHERE IT IS SUPPOSED TO DISPLAY EVERYTHING THAT USE SAVED
    {
        OpenFileDialog ots = new OpenFileDialog();

        if (ots.ShowDialog() == DialogResult.OK)
        {
            StreamReader sr = new StreamReader(
                                            new FileStream(ots.FileName,
                                            FileMode.Open,
                                            FileAccess.ReadWrite)
                                            );
            String items;
// I tried coping my if else if statements for the save streamREader thinking that would work  it doesn't DUH. I'm out of IDEAS for this COULD USE SOME HELP WITH THIS
            while (!sr.EndOfStream)
            {
                items = sr.ReadLine();
                flavorBox.Items.Add(items);
                syrupBox.Items.Add(items);

                 if (Nuts.Checked)
                {
                    this.Tag = "checked";
              sw.WriteLine(Nuts);


                }
                else if (Cherries.Checked)
                {
                    this.Tag = "checked";
                 sw.WriteLine(Cherries);

                }
                else if (Sprinkles.Checked)
                {
                    this.Tag = "checked";


                }

            }
            flavorBox.SelectedIndex = 1;
            syrupBox.SelectedIndex = 1;

            sr.Close();
        }
    }

1 个答案:

答案 0 :(得分:0)

首先要做的事情是:如果我解释你的大便是正确的,那么我建议你冷静下来。如果您处于愤怒模式,那么发现错误绝非易事。

我想你问题的很大一部分可能是以下几行:

if (!String.IsNullOrEmpty(syrupBox.Text))
{
    sw.WriteLine(flavorBox.SelectedItem);
}
else if (!String.IsNullOrEmpty(syrupBox.Text))
{
    sw.WriteLine(flavorBox.SelectedItem);
}

您为syrupBox.Text检查了两次,并始终使用flavorBox.SelectedItem。我认为你把syrupBox和flavorBox搞混了。