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)
Related
I'm blind. I have figured most things out, though, as the properties window is pretty accessible.
One thing I could not figure out (at least in Visual C# and Winforms) is how to add an eventhandler.
Let's assume we have a menu item "exit". If I select properties, I can change lots of things from font size over text alignment to accessible description, but nothing seems to match events or event handlers.
I folded out all parts of the property Window, but it's nowhere to be found.
Does anyone know where exactly I can find this in the properties window?
Or if not, where would Visual Studio add events and event handlers so I can do this manually?
I'm honestly a bit confused by all the separate files and unsure where to add what.
(Using Visua Studio 2017)
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.
When I'm making a Windows form application, and I use the toolbox and drag an item onto the form, I get a nice tool on the form, like button, listbox, etc. When I double click on the button, listBox, etc it goes to the text code part where all the delegates are. Also known as the event handlers. But what happens when I want to delete an item on the visual part of the application (the Code.cs[design] part)? I select it, then right click it and then delete it. It's off the screen. But, the problem is with all the event handlers. They're still left in the text-based code section but I don't need them there at all, and they generate a whole bunch of errors because references are gone, etc.
So, basically, I'm asking someone on the forum how to completely get rid of a button, listBox, both the visual part of it in code.cs[design] and in the code.cs part? I don't want to go line-by-line in deleting junk, I want to get rid of the button/listBox and it's accompanying code in one fell swoop.
It will delete such an automatically added event handler. But only if you didn't modify it and left it untouched with no code added. Visual Studio will not delete code that you have written. The reason for that should be obvious.
If this happens a lot then you need to get a pair of scissors and cut the mouse umbilical cord. Design first, program later.
I can't be done and it shouldn't.
You first delete the visible control and after that you delete the code stubs.
The handler assignments in the Designer.cs are then being deleted with the visible control automagically, as they were created.
Just make sure to get the order right: first delete the controls then the code.
Unless you mix those up there is no need to ever edit the designer.cs file. The code may contain important stuff you wrote. Therefore is is not deleted by studio.
You create it, you delete it! Two steps, period.
PS: The code stubs are (unfortunately) added chronologically. It would be nice if they were grouped by the controls they belong to. If you are in doubt, you can always use the 'find all references' command to find those methods buried in the past..
If you have a code stub generated, delete the method. Once that's done, Click on your 'forms view' - you will have an error screen shown.
Click on the link, and it will being you to the designer view, with your cursor shown on a specific line. Delete this line of code. Having that done, you could then delete the physical element from your designer.
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!
in c# how can I get the available events of a class in order to begin programming code inside of it. I know that in form or usercontrol you can select the control and click the events button and click on the selected event to begin coding, but I mean a derived class which I want to code its methods or events.
Thanks!
The simplest way is to look in the documentation, to be honest.
If you're within the designer, your approach of using the properties window will work for other controls (buttons, textboxes etc) as well... and if you're in the IDE, you can type this. from any instance context (e.g. the constructor) and get a list of members up, including the events.
I would still suggest reading the documentation though - MSDN allows you to look at all members, or just the events (or methods, or whatever) at any one time. It's not terribly hard to peruse the list that way - and you can then check the details of the event so you can make sure you're using it properly, rather than just by guessing based on the name.
EDIT: As Aren mentions, there's also the Object Browser. Not my personal preference, but it's an option.
The events are listed in the IntelliSense popup window you'll get when you type the class reference variable name followed by a dot. They have a lightning bolt icon.
Many more organized ways to know what a class can do. You could read its documentation. Or browse its source code. Or use Reflector if there isn't any.
in Visual Studio 2010, they have got this new Help Library Manager where you can check for MSDN updates and download any updates or go to online directly. (you can also install Vendor API docs quite easily as well)