|
-
Aug 1st, 2017, 10:28 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] View items and their events in .cs file ?
Hi,
I've just started a new C# project (first one ever). I created some forms and added controls on them, and now I want to start coding. But, I can't access controls ?
When I open .cs file, I can't choose neither form events neither controls or their events.
What is wrong ? I'm using Visual Studio Community 2015 - should I download something for C# too ?...Strange, but working on VB.NET projects I didn't have those problems.
As mentioned, my 1st C# project, so I really don't know much about C#.
Thanks for help in advance !
Last edited by LuckyLuke82; Aug 1st, 2017 at 10:32 AM.
-
Aug 1st, 2017, 06:02 PM
#2
Re: View items and their events in .cs file ?
If you mean that you cannot create event handlers using the drop-down lists at the top of the code window, you're absolutely right. You can do that in VB but you've never been able to in C#. In C#, there are three options for handling events:
1. You can double-click a control or component in the designer to generate a handler for the default event, just as you can in VB.
2. If it's a control or component that you added in the designer, you can create an event handler from the designer. Select the item(s) you want to handle an event for, open the Properties window, click the Events button and then double-click the event you want to handle. That will generate a method to handle the event of the item(s) or, if a method is already registered to handle the event, navigate to that method. You can also select an existing method from the drop-down. Note that this same mechanism for creating event handlers has been available in VB since about 2005 and is the best way to handle the same event with one method for multiple controls.
3. You can register an event handler in code for any object using the C# equivalent of an AddHandler statement. The C# IDE actually provides significant help to do this. For instance, I just added a Button named 'button1' to a form and then typed "button1.Click +=" into the form's Load event handler in VS 2017 and was prompted to press the Tab key to generate a handler for that event. VS automatically changed the code to this:
csharp Code:
private void Form1_Load(object sender, EventArgs e) { button1.Click += Button1_Click; } private void Button1_Click(object sender, EventArgs e) { throw new NotImplementedException(); }
Now, I use ReSharper so it may be that it helped here but I do know that previous versions of VS have done similar things, although you had to press Tab once to complete the line you were typing and then a second time to generate the method. Either way, not much - if any - more onerous than using drop-downs in VB.
It's worth noting that C# offers no equivalent of WithEvents and Handles in VB. The VB IDE will always generate a method with a Handles clause for a field declared WithEvents. You can write your own AddHandler statement but the only time VB will generate one is if you configure the Properties for a control or component to not generate a member. It's good practice to do that if you have no need to access the control in code. In C#, the '+=' syntax is the only way to register an event handler and is equivalent to AddHandler, with '-=' being the equivalent of RemoveHandler.
-
Aug 2nd, 2017, 05:27 AM
#3
Thread Starter
Fanatic Member
Re: View items and their events in .cs file ?
If you mean that you cannot create event handlers using the drop-down lists at the top of the code window, you're absolutely right.
Yes, exactly that. Thanks for all info, I thought I was missing some update ....I must say that VB.NET beats C# in Visual Studio in may other things too, like Intellisense. Brackets, "()", ";" have to be all written manually. I hope someday Microsoft will fix that too.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|