muhan
muhan

Reputation: 2557

C# combine 2 events in one method

I am relatively new to programming, as you will soon see...
I have 2 events, which execute the same code. I currently have the following pseudo code for a datagridview:

private void dgv_CellEnter(object sender, DataGridViewCellEventArgs e)  
{
   string abc = "abc";
}

private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)  
{
   string abc = "abc";
}

Is there a way to combine this into one event? Is there a better way to do this?

Upvotes: 5

Views: 8983

Answers (5)

YetAnotherDeveloper
YetAnotherDeveloper

Reputation: 846

Well the quick answer is yes. Put the guts of the methods in its own method and then just have the onclick event call that method. This will give you only one place to update code should it need to change.

There are a 100 different ways to do this, and this is prob the easiest.

So create something like this:

protected void MyNewMethod()
{
    string abc = "123";
}

and then your other methods will just call it like this:

private void dgv_CellEnter(object sender, DataGridViewCellEventArgs e)
{ 
    MyNewMethod(); 
}

private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{ 
    MyNewMethod(); 
}

Option 2

Just call the same method from the markup. You really only need one of those methods and the event in the markup can call the same one.

Upvotes: 6

Andrew Shepherd
Andrew Shepherd

Reputation: 45252

Why not just have one method and map it to two events?

private void dgv_CellEvent(object sender, DataGridViewCellEventArgs e)
{
    string abc = "123";
}

// In the event mapping

dgv.CellEnter += dgv_CellEvent;
dgv.CellClick += dgv_CellEvent;

Upvotes: 9

snarf
snarf

Reputation: 2852

In the property window (using C# Express) you can select event handlers in a drop-down, or manually type the name of a method. The signature just has to match. I'm assuming that this is the same in VS.

Upvotes: 2

mlevit
mlevit

Reputation: 2736

You could try the following:

private void dgv_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    string abc = "abc";
}

private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
    dgv_CellEnter(sender, e);
}

That way when you either Click or push Enter it will run the same method.

Thanks

Upvotes: 1

Scott Whitlock
Scott Whitlock

Reputation: 13839

Easiest way is to do this:

private void dgv_CellEnter(object sender, DataGridViewCellEventArgs e)
{ 
    dgv_CellClick(sender, e); 
}

private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{ 
    string abc = "abc"; 
}

There is code automatically generated by the IDE that wires up your events though, and you could modify this so they both hook up the same event handler, but I don't like messing with generated code.

In ASP.NET or WPF, your data grid or similar has a property that determines the name of the event handler, so you could just point them both to that.

If you were programming in VB.NET instead of C#, you could just write one method and use the Handles keyword to say that this one method handles both events.

Upvotes: 1

Related Questions