lundi 28 mars 2016

How to makeStreamReader/Writer with check boxes to Save the Selected items and open the Selected items

My program is supposed to be able to allow the user to be able to select a flavor of icecreame and syrup using (comboboxes) and selecting the three check boxes if they want nuts, cherries or sprinkles. WHICH IS WORKING TO THE BEST OF MY KNOWLEGE

the Other part of the Program is supposed to allow the user to save there order and open it later using StreamReader/Writer (WHICH ISNT WORKING REALLY WELL I CANT GET IT TO WRITE DOWN WHAT IS SELECTED OUT OF BOTH COMBO BOXES AND CHECK BOXES DONT WRITE EITHER. SAMETHING WITH THE OPEN IT ONLY OPENS IF I CHANGE THE INDEX NUMBER AFTER THE EQUALS)

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();
        }
    }




Aucun commentaire:

Enregistrer un commentaire