James
James

Reputation: 131

Grouping two methods together in C#

I currently have two different event handlers in C#, which perform two different functions. Although how could I combine the two methods together, so only 1 button could perform both actions? (Taking into account that button1_Click event must be performed first.)

    private void button2_Click(object sender, EventArgs e)
    {
        var file = File.AppendText(@"c:\output2.txt");

        foreach (string tmpLine in File.ReadAllLines(@"c:\output.txt"))
        {
            if (File.Exists(tmpLine))
            {
                file.WriteLine(tmpLine);
            }
        }

        file.Close();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (StreamWriter sw = File.AppendText(@"c:\output.txt"))
        {
            StreamReader sr = new StreamReader(@"c:\filename.txt");

            string myString = "";
            while (!sr.EndOfStream)
            {

                myString = sr.ReadLine();
                int index = myString.LastIndexOf(":");
                if (index > 0)
                    myString = myString.Substring(0, index);

                sw.WriteLine(myString);
            }
            button2_Click(sender, e);

        }

    }

Upvotes: 1

Views: 1250

Answers (5)

IAbstract
IAbstract

Reputation: 19881

// given these two methods extracted from your events
void DoBar(object sender, EventArgs e)
{

    var file = File.AppendText(@"c:\output.txt");

    foreach (string tmpLine in File.ReadAllLines(@"c:\filename.txt"))
    {
        if (File.Exists(tmpLine))
        {
            file.WriteLine(tmpLine);
        }
    }

    file.Close();

}

void DoFoo(object sender, EventArgs e)
{
        using (StreamWriter sw = File.AppendText(@"c:\output.txt"))
        {
            StreamReader sr = new StreamReader(@"c:\filename.txt");

            string myString = "";
            while (!sr.EndOfStream)
            {

                myString = sr.ReadLine();
                int index = myString.LastIndexOf(":");
                if (index > 0)
                    myString = myString.Substring(0, index);

                sw.WriteLine(myString);
            }
        }

}

    // you can subscribe like this
button1.Click += DoFoo;
button1.Click += DoBar;
button2.Click += DoBar;  

EDIT forgot my sender and eventargs

Upvotes: 0

jumpdart
jumpdart

Reputation: 1712

I try to keep most logic out of event handlers and create functions with logical names that i call from the event handlers. Then they can be called from wherever.

Upvotes: 2

Rob
Rob

Reputation: 45771

You could, if I'm understanding you correctly, have one event handler call another. An event handler is "just" a method after all, so:

private void button1_Click(object sender, EventArgs e)
{
    // All the code that's currently there
    button2_Click(sender, e);
}

Or, you could extract the code from the event handlers into separate methods:

private void button1_Click(object sender, EventArgs e)
{
    WriteToOutputDotTxt();
    OtherMethodThatWritesToOutputDotTxt();
}
private void button2_Click(object sender, EventArgs e)
{
    OtherMethodThatWritesToOutputDotTxt();
}

private void WriteToOutputDotTxt()
{
    // Code that's currently in button1_Click
}

private void OtherMethodThatWritesToOutputDotTxt()
{
    // Code that's currently in button2_Click
}

The code doesn't have to be contained within the event handlers, in fact you'll find it easier (if you're interested!) to test your code if you can separate it away from your UI. You could, for example, have a class called ProcessOutputFile and move the WriteToOutputDotTxt and OtherMethodThatWritesToOutputDotTxt methods onto that class. It's then a lot easier to write tests for that code as it's not "tied into" the UI code.

Upvotes: 2

cichy
cichy

Reputation: 10644

Just attach two event handlers to some button:

somebutton.Click += new EventHandler(button1_Click);
somebutton.Click += new EventHandler(button2_Click);

Upvotes: 1

Chris Walsh
Chris Walsh

Reputation: 1903

Instead of writing the code in the event handler, bring them out into two functions and then call those functions whichever way you want from the event handler.

Upvotes: 11

Related Questions