Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,359 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,221 people online right now. Registration is fast and FREE... Join Now!




C# Event Handling

2 Pages V  1 2 >  
Reply to this topicStart new topic

C# Event Handling

webwired
post 1 Oct, 2008 - 05:54 AM
Post #1


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 129



Thanked 1 times

Dream Kudos: 100
My Contributions


One of the things that I'm having difficulty understanding, trying to learn C# is Event Handling in comparison to VB .NET... I get VB .NET's way

For example, here in this event I can simply change the .CellClick event to any number of events that I want to use, .DoubleClick, etc.
CODE
Private Sub CustomersDataGridView_CellSingleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CustomersDataGridView.CellClick


But, how do I do it in C# when it doesn't define the particular event?
CODE
private void CustomersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)


Please help, this has been one of my major problems trying to pick up on C#... I wrote a program in VB .NET and figured I would try to duplicate it in C# to help me learn.
User is offlineProfile CardPM

Go to the top of the page

modi123_1
post 1 Oct, 2008 - 06:04 AM
Post #2


D.I.C Addict

Group Icon
Joined: 12 Jun, 2008
Posts: 518



Thanked 10 times

Dream Kudos: 100
My Contributions


QUOTE(webwired @ 1 Oct, 2008 - 08:54 AM) *

One of the things that I'm having difficulty understanding, trying to learn C# is Event Handling in comparison to VB .NET... I get VB .NET's way

For example, here in this event I can simply change the .CellClick event to any number of events that I want to use, .DoubleClick, etc.
CODE
Private Sub CustomersDataGridView_CellSingleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CustomersDataGridView.CellClick


But, how do I do it in C# when it doesn't define the particular event?
CODE
private void CustomersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)


Please help, this has been one of my major problems trying to pick up on C#... I wrote a program in VB .NET and figured I would try to duplicate it in C# to help me learn.



It's all about event handlers! VB.net does event handlers, but they also have the nice short hand where you put the "handles <event name>" after the subroutine.

Check this out:
http://www.csharphelp.com/archives/archive253.html
User is offlineProfile CardPM

Go to the top of the page

webwired
post 1 Oct, 2008 - 06:17 AM
Post #3


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 129



Thanked 1 times

Dream Kudos: 100
My Contributions


QUOTE(modi123_1 @ 1 Oct, 2008 - 07:04 AM) *

It's all about event handlers! VB.net does event handlers, but they also have the nice short hand where you put the "handles <event name>" after the subroutine.

Check this out:
http://www.csharphelp.com/archives/archive253.html


Thank you for your reply, but from what I can tell, that is all using one particular Event Handler, what I'd like to know is how to change a particular Event for a control... Like you said, VB .NET you just put ""handles <event name>" after the subroutine."
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 1 Oct, 2008 - 07:06 AM
Post #4


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 502



Thanked 44 times

Dream Kudos: 25
My Contributions


I think you want something like this to tie the CellClick event of the CustomersDataGridView component to the event handler you've created:
csharp
CustomersDataGridView.CellClick += new EventHandler(this.CustomersDataGridView_CellContentClick);


This post has been edited by JackOfAllTrades: 1 Oct, 2008 - 07:07 AM
User is offlineProfile CardPM

Go to the top of the page

webwired
post 1 Oct, 2008 - 07:33 AM
Post #5


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 129



Thanked 1 times

Dream Kudos: 100
My Contributions


Oh, I think I see... You can define a different Event Handler by adding that step to the end of the sub, ie.
CODE
CustomersDataGridView.ANY_EVENT += new EventHandler(this.CustomersDataGridView_CellContentClick);


Whereas in VB .NET you just put it at the end sub...
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 1 Oct, 2008 - 07:42 AM
Post #6


D.I.C Regular

Group Icon
Joined: 21 Mar, 2008
Posts: 314



Thanked 17 times

Dream Kudos: 25
My Contributions


QUOTE(webwired @ 1 Oct, 2008 - 11:33 AM) *

Oh, I think I see... You can define a different Event Handler by adding that step to the end of the sub, ie.
CODE
CustomersDataGridView.ANY_EVENT += new EventHandler(this.CustomersDataGridView_CellContentClick);


Whereas in VB .NET you just put it at the end sub...


you would put that in the form load
User is offlineProfile CardPM

Go to the top of the page

modi123_1
post 1 Oct, 2008 - 07:43 AM
Post #7


D.I.C Addict

Group Icon
Joined: 12 Jun, 2008
Posts: 518



Thanked 10 times

Dream Kudos: 100
My Contributions


Yes... essentially when your form starts up - in your constructor you would tied all your controls' events to a specific function.

Now say you create a new c# windows form with a button on it. If you are in the designer and double click that button the windows generated code that ties the control's event to the windows generated sub routine "button1_Click" is in the designer file. You can easily just pull that out of the designer and put that into main constructor.
cpp

public partial class Form1 : Form
{
// form's main
public Form1()
{
//init
InitializeComponent();
//where i tie the button's click event to the subroutine I want the event to fire.
this.button1.Click += new System.EventHandler(this.button1_Click);
}

//sub routine that I want to access when the button is clicked
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("yo");
}
}


This post has been edited by modi123_1: 1 Oct, 2008 - 07:46 AM
User is offlineProfile CardPM

Go to the top of the page

webwired
post 1 Oct, 2008 - 08:56 AM
Post #8


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 129



Thanked 1 times

Dream Kudos: 100
My Contributions


Excellent, good stuff... I think I got it...
User is offlineProfile CardPM

Go to the top of the page

webwired
post 2 Oct, 2008 - 08:54 AM
Post #9


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 129



Thanked 1 times

Dream Kudos: 100
My Contributions


Well guys, I tried it out and I can't seem to make it work... here's my code...

CODE

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            customersDataGridView.CellContentDoubleClick += new EventHandler(customersDataGridView_CellContentClick);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            customersDataGridView.DataSource = DataAccess.GetAllCustomers();
        }

        private void customersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show("It worked!");
        }
    }


I get this error... I did some more researching on it, and from what I can tell, it should be working... any ideas?

QUOTE
No overload for 'customersDataGridView_CellContentClick' matches delegate 'System.EventHandler'
User is offlineProfile CardPM

Go to the top of the page

girasquid
post 2 Oct, 2008 - 08:58 AM
Post #10


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,255



Thanked 14 times

Dream Kudos: 650
My Contributions


Try changing your DataGridViewCellEventArgs to EventArgs, and seeing if that solves the problem - if it does, then you it's not a DataGridViewCellEventArgs variable you should be expecting.
User is offlineProfile CardPM

Go to the top of the page

webwired
post 2 Oct, 2008 - 09:03 AM
Post #11


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 129



Thanked 1 times

Dream Kudos: 100
My Contributions


QUOTE(girasquid @ 2 Oct, 2008 - 09:58 AM) *

Try changing your DataGridViewCellEventArgs to EventArgs, and seeing if that solves the problem - if it does, then you it's not a DataGridViewCellEventArgs variable you should be expecting.


Just tried it, came up with error, "Cannot implicitly convert type 'System.EventHandler' to 'System.Windows.Forms.DataGridViewCellEventHandler'"
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 2 Oct, 2008 - 09:57 AM
Post #12


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Your answer is right there.

Try this:
dataGridView1.<event> += new DataGridViewCellEventHandler(dataGridView1_CellClick);
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:33AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month