Home > database >  Is it possible to use the same method on two different events on a WPF application?
Is it possible to use the same method on two different events on a WPF application?

Time:10-01

I am wondering if it is possible to use the same event on a different method. My example is I'm trying to create my own version of note pad and two the same buttons technically almost do the exact same thing. New file and open file.

Here is my code for creating a new file

private void NewSubMenuOption_Click(object sender, RoutedEventArgs e)
        {
            if (Editor.Text.Length == 0)
            {
                return; 
            }
            else
            {

                if(MessageBox.Show("Do you want to save changes to Untitled", "NotePad", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    SaveFileDialog save = new SaveFileDialog()
                    {
                        Title = "Save your file",
                        Filter = "Text Document (*.txt) | *.txt",
                        FileName = " "
                    };

                    if (save.ShowDialog() == true)
                    {

                        StreamWriter sw = new StreamWriter(File.Create(save.FileName));
                        sw.Write(Editor.Text); 
                        sw.Dispose();
                        Editor.Clear(); 
                    }
                    
                }
               
            }
           


} 

I just wanna know if there is away that I can use the same code up there again in like a function so I don't have to rewrite the code and repeat it again.

CodePudding user response:

Take the code that you want to reuse and put it into a more generic function. Then in your NewSubMenuOption_Click click handler, and the other click handler, call the more generic code.

CodePudding user response:

You needn't necessarily worry about function parameters, but use the same code more like this:

void askForSave(string filename){
     if (Editor.Text.Length == 0)
        {
            return; 
        }
        else
        {

            if(MessageBox.Show("Do you want to save changes to " filename, "NotePad", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                SaveFileDialog save = new SaveFileDialog()
                {
                    Title = "Save your file",
                    Filter = "Text Document (*.txt) | *.txt",
                    FileName = " "
                };

                if (save.ShowDialog() == true)
                {

                    StreamWriter sw = new StreamWriter(File.Create(save.FileName));
                    sw.Write(Editor.Text); 
                    sw.Dispose();
                    Editor.Clear(); 
                }
                
            }
           
        }
}

And then use this function whenever you want to ask the user to save their file, like this:

      private void NewSubMenuOption_Click(object sender, RoutedEventArgs e)
        {
            //check if the file is not saved, and then call askForSave()
            askForSave(filename)  //Initialize filename to Untitled first
            //rest of your code, depending on New option, or Open option clicked
        }

In this way, you can use this function whenever you want the user to save their file, either in the New menu bar option or the Open option

  • Related