简体   繁体   中英

what can i do to make my code to work

i have 4 errors and im working on my save button if i could get these fixed it will only save the selected items that the user wants

THIS IS NOT all the code just the code that im having problems with. this program is for and icecream application with 2 combo boxes and 3 check boxes

I PUTT COMMENT LINES WHERE I HAVE THE ERRORS ARE AT

  private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            StreamWriter sw = new StreamWriter(
                                            new FileStream(sfd.FileName,
                                                            FileMode.Create,
                                                            FileAccess.Write)
                                                            );
            if(flavorBox) // i have an error right here (Cannot implicitly convert type 'System.Windows.Forms.ComboBox' to 'boolean)

            {
                sw.WriteLine(flavorBox.SelectedItem);

            }
            else(syrupBox) //syays i need semecolons right here for some reason
            {
                sw.WriteLine(syrupBox.SelectedItem);


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

            }
            else(Cherries.Checked) //says i need semicolons here to i dont know why
            {
                this.Tag = "checked";
                sw.WriteLine(Cherries);

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


    }

THIS IS MY 4TH ERROR

       private void closeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        DialogResult result = MessageBox.Show("Are you sure you want to send the data back?",
             "Data Sender",
             MessageBoxButtons.YesNo,
             MessageBoxIcon.Warning);



        if (result == DialogResult.No)
        {
            e.Cancel() = true;  //ITS ASKED ME AM I MISSING A DIRECTIVE OR ASSEMBLY REFRENCE (FOR CANCEL)
        }

In an if-else only the if should have a condition, and the else should not. Use an else if statement to explicitly define a condition.

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

Flavorbox is a textbox, so by doing if(flavorbox) you are checking if flavorbox is equal to true or false. It's a textbox so this isn't possible. You'll probably have to just change the flavorbox. Try the following:

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

1. about

if(flavorBox)

what are you checking?

about the:

else(syrupBox) and else(Cherries.Checked)

you cannot do else(something) . you can do else if(something) because else is all the rest of the options. so change them to: else if(syrupBox) and else if(Cherries.Checked)

2. about the cancel: what are you trying to do?

when you click no in the dialog, what are you trying to accomplish with the e.cancel ?

For the fourth error, note that EventArgs.Cancel is a property, not a method. Remove the brackets:

e.Cancel = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM