Pasting control event code in C# - c#

This may seem like a funny question to some but I am comming from VB so it seems a little strange. Why is it when you copy controls from another form and then copy the code into the code behind in c# it doesn't automatically bind to that control even though it has the controls name and event in Visual Studio?
I know that VB has extra code to show that it handles a controls event so it makes it a little different, but it makes it difficult to paste large amount of code from other places.. So I tend to wonder if I am missing something.
The other thing I have noticed is using VB.net in Visual Studio the code editor has a event drop down that displays all of the controls events and in C# it only shows the ones that have code in them. The only way to see all events in VS is the events column of the property explorer.
Again I am sorry for my ignorance but I would like to make sure I am being as efficient as possible and would like to make sure I don't have some options turned off or something.

It's a common complaint for people from VB. VB.Net uses the "WithEvents/Handles" combination to allow code to handle events, which is why you can copy paste the code to a different form. C# has no such equivalent. You can do things in C#, however, that VB.Net cannot. For instance, you can wire up an event in code and the IDE will automatically insert a method stub with the correct signature for you. To do this in VB requires a separate WithEvents declaration at class level, and then you select the control/event from the dropdowns across the IDE. Neither is better, just different!

Related

Delegates in C# and how do I attach them to controls in visual studio?

I'm new to C# and delegates, however, from what I've gathered, they say if this event happened run this method. But in Visual Studio (community 2017), I can't figure out where/how I am meant to go about defining/attaching them.
I'd appreciate an answer as I'm trying to go about switching a program I've previously written in Python over to C#.
I've looked through documentation, I've looked over questions already asked here, and none of them seem to give me the information I'm after.
To add delegates (in this case they're typically called "event handlers") to UI controls in WinForms, you should:
Select the control in the visual designer
Look at the Properties windows
Click the lightning bolt to view the events.
Either double-click the blank space next to an event to auto-generate a handler, or single-click and select an existing handler from the dropdown.

How to show a VB.NET form based on a referenced C# assembly event firing?

My first time asking a question here so apologies upfront if I'm not doing this right. I could not find anything with the search.
I inherited a VB.NET legacy Windows Application project. Any and all new additions as far as possible I would like to code in C#, including new Windows Forms. So, I have a new Windows form in a C# assembly (not sure if this is the best way) being referenced by the VB.NET Windows application.
On initialising the C# form from the referenced assembly I pass it all the necessary parameters from which I will build up my grid columns and records in the C# Windows Form. No problems here so far.
On the cell-double click event of the C# grid, I would like to show an existing VB.NET Winform from the application referencing the C# assembly.
My question is: How do I get a handle on the VB.NET Windows Form to show it, or do anything else with it for that matter? Circular reference is obviously not allowed so how can I achieve this if at all possible, C# or otherwise?
You should be able to add an event handler to the object.
http://msdn.microsoft.com/en-us/library/6yyk8z93%28v=vs.90%29.aspx
One way would be to pass a generic form creation handler to your C# Form, given that it is instantiated from your VB.NET code. This way the form wouldn't have to know what exact type of form it must create and show; all it would know is that it's a Form, which is good enough if all you have to do is showing it.
You may also consider exposing your C# Form cell double click event as a public event that can be handled by your VB.NET code.
If it makes sense in your design, a third option would be to use a third assembly that acts as a bridge. This assembly could be referred by both your VB.NET and C# assemblies and hold interface types implemented / used in these.

How to automatically remove event reference in asp.net webcontrol when event is deleted from C# codebehind?

Everytime I accidentally doubleclick a webcontrol (ex GridView, DetailsView), VS creates an
unnecessary event in code (selectedindexchanged). I delete this event, but the reference still appears in webcontrol's properties, causing the CS1061 exception.
Is there a way to delete the reference in properties (or asp.net code) automatically when the event is deleted in C# code?
I know that when programming in VB, Visual Studio does it, so i'm looking for a similar solution in C#.
I've experienced the same problem as you before and while it's not really a problem, I feel your frustration when you run and then it all fails because you forgot to remove the code from the .aspx.
Unfortunately there's no way to change this behavior. No automatic way to do it, anyway. I too looked for a solution for this a while back and found literally nothing despite my hopes of finding a hidden option within Visual Studio.
It's only a little time taken from you though, so you should be alright.

How to get VC# to rebuild auto-generated .cs files?

I imagine this question has been asked and answered, but I cannot find it.
I wanted to make a simple GUI to interface to a windows 7 command. I usually use Wx, but since this was to be a windows-only thing, I decided to see if I could whip it out real fast using Visual C# 2010 Express, which I had never used before.
Things started off just great. I created a form, put a few buttons and text boxes and such on it, and hit Debug. It came up and ran just like that. No muss, no fuss. So I then designed the form just the way I wanted it, renamed the controls from "Button1" and so forth to meaningful names. But now it's a mess. By clicking around, I discovered that VC# had auto-generated two files called Form1.cs and Form1.Designer.cs. The later contains the bindings between functions and the events generated from user-clicks etc., and the former contains no-op code for those functions, for me to complete.
Problem is, the names are all still the original "Button1" and so forth, not the new ones, and the new controls I added after running the first time do not appear at all.
I want it to regenerate all that stuff afresh from the finished form. How to?
You probably don't have an actual problem, unless you edited something within Form1.Designer.cs. Provided you left that file alone, things should just work.
Whenever you double-click on a button, or use the events interface to create event handlers, the handlers are created with the following pattern:
ObjectName_EventName()
So, for example:
Button1_Click()
If you later change the name of Button1, the event handler's name is not changed, too. But it is still attached to the proper event, again - assuming you did not edit anything in Form1.Designer.cs
If you look (but don't touch!) inside Form1.Designer.cs, you will probably find something similar to:
MyNewButtonName.Click += Button1_Click;
(I don't recall the exact syntax the editor uses for wiring up event handlers; the principle is you should see your new button name being attached to the old handler name)

Visual studio 2010 showing available events from code behind

In work and in home I have VS2010 installed. But in work I have this one cool feature. On the code behind file I have two drop downs. When I select some object in the left one lets say a testButton or Default2 (a page class), on the right one I get all available events for that object and when I select an event Visual Studio autogenerates it in my code-behind file. In my home VS I actually have does 2 drop downs but they work different. I have available objects on the right one but when I select for example the testButton it shows me the aspx page and points this button :/ Should I set sommething in the VS configuration or maybe in work I have some addon which I'm not aware of? Any ideas?
This is not a VB-exclusive feature.
What you're looking for is called a navigation bar. In Visual Studio options, open Text Editor → C# → General, and you'll see an option called Navigation Bar. Enable it and click OK.
However, I believe this bar works a bit differently in VB and in C#. In C#, it only lists the existing classes and their methods; in VB, it will list all your controls and their events even if those don't exist yet.
Ok I think I've found the answer. It seems that this feature is only available when the current file is in VB.Net. In my opinion this suks :/
You need to go the design view, in the Properties box, click on Events (lighting bolt) and double click on the events you want to code behind:
(source: byte.net)
#shin is correct, this is a VB.NET only feature; Microsoft only had feedback reports dating back to 2004 mentioning this feature, so I have opened a new bug request with Microsoft regarding this; and also added feedback to Visual Studios account on UserVoice...
https://connect.microsoft.com/VisualStudio/feedback/details/688175/vs2010-c-add-page-event-handlers-to-asp-net-using-codebehind-navigation-bar-the-same-way-as-in-vb-net
http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2244558-allow-c-to-add-event-handlers-to-classes-using-co
I encourage everyone who this issue effects to go and vote up & confirm this issue with them.
I dislike that C# doesn't provide such a useful time-saving function.
Up to Visual Studio 2010, only Vb.net has this feature: on code-behind page, the code panel upper-left drop down shows a control's name, and the right drop down shows this control's available events. When clicking this event we can get the code-block for this event. This feature is not available in C#.
Without above feature, we can still get a control's available events for both C# and Vb.net: We must select the control on the aspx page and choose the design view at the lower left corner, then on the Properties panel click the Event button (the lighting sign), then available actions will be shown, double-click the name of the event you want, the event code will be generated in code-behind.
It is most certainly is a VB only feature and has been reported on numerous occassions to Microsoft.
It is possible to see all the available Events in C# & Asp.net too, but not inside code window as possible in VB. But you can see all Events of any selected Control in its Property Window. Open Properties, then Click Events.
This and the feature to generate event handlers without having to switch to Design view for C# is finally coming in Visual Studio 2012. It was about time.
Even the 2012 feature is not enough. You still have to guess for Page level events (Init, PreRender, etc) and also files that haven't already been filled out for you like Global.asax, which depending on the template you started with may or may not have all the event handlers already specified.
Agreed that Event Handlers is one of the things VB has for years and years and years handled way better than C#. I can't count the number of times I've had to remind a fellow C# developer that some events do, in fact, exist.

Categories