Domanda

private void save(object sender, EventArgs e)
{
    if (File.Exists(fileLabel.Text))
    {
        // this will save in the debug folder unfortunately
        FileStream outputFileStream = new FileStream(fileLabel.Text, FileMode.Create, FileAccess.Write);
        StreamWriter writer = new StreamWriter(outputFileStream);

        // writing block (too long code)


        writer.Close();
        outputFileStream.Close();
    }

    else
    {
        saveAs(); // no overload
    }
}

So what I'm trying to do is if a user presses Save they will save the file without the dialog. This code checks if the file exists to save. If it doesn't exist it will redirect to the saveAsDialog method.

private void saveAs(object sender, EventArgs e)
{
    // code is similar (it works fine if user clicks the menu strip)
}

However, when I call the saveAs() method it will not overload. Now I've never called event handlers in my Form1 class so I wouldn't know how to use it. All the handlers are from double-clicking the form design.

So what parameters do I have to put on the saveAs() method call if I want it to do the same thing as if the user selected it from the menu strip?

È stato utile?

Soluzione

Try this to trigger your event:

saveAs(this, EventArgs.Empty)

Altri suggerimenti

Pass on the save parameters:

saveAs(sender, e);

Using a method in one place suggests refactoring code into a common method that can be used anywhere.

write a Method using the code of your save event

void saveOrSaveAs()
{
  if (File.Exists(fileLabel.Text))
  {
    // this will save in the debug folder unfortunately
    FileStream outputFileStream = new FileStream(fileLabel.Text, FileMode.Create, FileAccess.Write);
    StreamWriter writer = new StreamWriter(outputFileStream);

    // writing block (too long code)

    writer.Close();
    outputFileStream.Close();
  }
  else
  {
    saveAs(); //If You have already written code for saveAs() method.
  }
}

And Call this method in both events as

private void save(object sender, EventArgs e)
{
  saveOrSaveAs();
}

Again call this method in your saveAs Event.

private void saveAs(object sender, EventArgs e)
{
  saveOrSaveAs();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top