Using VS2013 it was possible, at least with VB.NET, to double click on a control and then the default event would show up in the code file.
Above this there was a pull down list of the other possible events for this control.
Now I'm working in VS2015 and in C#, but that list is not there.
I can still double click on a control to get the default event, but I cannot add another event. I don't think I'm supposed to edit the designer file.
How do I do this now?
Do I need to add events in the code file now?
for example I want to be able to drop a file on my windows application.
So somewhere I need to add the event for this.
Winforms :
Wpf:
To see the properties window:
Using VS2013 it was possible, at least with VB.NET, to double click on
a control and then the default event would show up in the code file.
Above this there was a pull down list of the other possible events for
this control.
This is known as the Navigation Bar. You can toggle it on/off in Tools --> Options --> Text Editor --> {Select Language} --> Navigation Bar.
BUT...the Navigation Bar behaves differently in C# than it does in VB.Net. It won't do what you want in C#, sorry!
To wire up an event using the IDE in C#, you have to first select the thing in question, then go to the Properties Pane and switch it to the Events view with the "Lightning Bolt" icon as Empereur Aiman has shown in his post.
C#, however, can do something that VB.Net cannot. With C#, you can wire up an event by writing a line of code in the editor and have the IDE generate the event stub for you. For instance, in the snippet below, a dynamic button is being created:
Button btn = new Button();
If you want to wire up its Click() event, you'd type in:
btn.Click +=
After the equals sign = is typed, you'd press {Tab} and the event stub will be generated for you:
private void Btn_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
If you keep the mouse on the Button key word in xaml code and then click on the lightning icon, you will be able to see the click event.
Related
In VS, if I create a new event handler in XAML like ItemClick="GridView_ItemClick" , I can right click on this or hit f12 and it will take me to the c# behind.
Is there a way to get from the c# back to the XAML? Like can I right click or hotkey on "private void GridView_ItemClick(object sender, ItemClickEventArgs e)" and return to the exact event handler?
There's no way to do this by default in Visual Studio.
Add-ons such as Resharper or Telerik JustCode will add this, but otherwise you're best off just using good old CTRL-F to search your project
I am new to C# in Visual Studio 2015. I just realized that the old classical way of adding event handlers by double-clicking on the item no longer works for some items like the form (or the Window).
After some Google searches I still can't figure out a way to add the Load event of the form using the designer.
Do I have to manually write code for that unlike in Visual Basic in Visual Studio 2005?
You can set the Load event of a Control from its properties window here. You create the
private void Form1_Load(object sender, EventArgs e)
{
// my code
}
event in your form class, and fill in its name (Form1_Load) where the arrow points in the picture.
Doing it manually would be something like:
Form1.Load += new System.EventHandler(this.Form1_Load);
considering that you created the Form1_Load event.
But on top of all that, double clicking should work.
You may have an issue with your instillation. However, select the form and go to the actions window and select load. This should give the desired result
So, I would double click here on my designer
and it should create me the code, but well it doesn't. And there is no value changed event in the events either.
So if anyone knows how to fix this, it would be nice. (I doubt it) so how would I get around this? How would I go on about creating the code myself that should be created when I double click on it?
Click the form or control that you want to create an event handler for.
In the Properties window(F4), click the Events button
In the list of available events, click the event that you want to create an event handler for.
In the box to the right of the event name, type the name of the handler and press ENTER.
Add the appropriate code to the event handler.
To create an event handler in the code editor
Switch to the code editor by using one of the following techniques:
Create a new method like:
private void buttonName_Click(object sender, EventArgs e) { ... }
In the file YourFormName.Designer.cs find your button and add
this.buttonName.Click += new System.EventHandler(this.buttonName_Click);
Okay, so here's my problem: Whenever I create a button, textbox, listbox, then double-click to view the source code:
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
}
After this has been auto-created by Visual Studio, it won't allow me to change it later.
So when I go into properties and want to change "quitToolStripMenuItem" (in the Name property field) to "mnuQuit," it will show up in the properties window properly, and will change the name (for all intents and purposes), but when I double-click to view source - it still shows the 'quitToolStrip..." name.
If I rename it to
private void mnuQuit_Click(object sender, EventArgs e)
It will throw a big hissy fit, and then my form design will be gone and a (basically) 404 message will appear instead of the form.
How can I do it without deleting the item and then recreating?
If you want to rename the event handler method, go to the designer, select the object (the menu item in your case), and in the properties window click the events button (it looks like a lightning bolt), and rename the event handler method from there.
In C#, the event handler is linked to the object that raise the event by a delegate, so the name of the method does not matter. You can have a button called Jack and event handler called Jill_Click that will actually handle the resize event of Jack. If you open the designer code you will see something like:
this.Jack.Resize += new System.EventHandler(this.Jill_Click);
Click on the Events (lightning) icon at the top of the Properties window and delete the text in the Click field.
Just refactor the method name? This will update it on the .designer.cs, in your designer and of course in your code.
Select the method name, right click, Refactor → Rename.
I am new to Visual Studio and I am just messing around with the controls to see how things work. I made one form that had a single button that, when pushed, simply printed "Hello World" to the screen. To try something more complicated I deleted that button and added various other tools to the Form. However the code in the Form.cs file was not updated to reflect these changes to the design and I can find no way to update it manually.
Any advice is appreciated.
Regards.
If you look at Form1.Designer.cs (assuming your form is called Form1) you will see a list of all the code that was generated by building your app, within there you will see your button name, if the button is deleted you should be able to safely delete the code between the comments.
You can see the changes in Form1.Designer.cs file
the code is below these lines....
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
To update manually just call manual_update().
manual_update()
{
Button b = new Button();
b.Text = "new button";
b.Size = new Size(100, 20);
b.Location = new Point(20, 20);
this.Controls.Add(b);
}
You can do almost everything manually.
If your Button had an OnClick event, it will NOT be erased when the Button is erased. Visual Studio assumes that this code may be used somewhere else and is not data-destructive.
Every time you delete a control that you had events, they will remain. You must manually go through and clean up your code.
EDIT:
To make the "new code" appear for the newer controls, you must either double click the button to generate the default event for that control, or go into the Properties of that control and generate the code for the events there.
Also Try rebuilding.. if you removed it from the form sometimes VS does not remove that code from the designer.. and you will have to manually remove it / update it.. also make sure you don't have any compile errors as well
if you have errors .. they changes may not be shown until the error(s) have been resolved.
Since you mentioned that you added other tools in the front-end Form file, make sure that your webcontrols in the Form are connected to whatever event handlers you have.
Also, make sure you're re-compiling/re-building your page, just for sanity check and if needed.
If I understand your question correctly then the code that adds controls and changes control properties is all auto-generated and resides, for your example, in Form1.Designer.cs. This file should generally never be touched except by Visual Studio.
If you want to add controls manually you should do it in the Form1.cs after the InitializeComponent() call or in an event like the Form_Load event. Here is an example of adding a button in a form load event:
private void Form_Load(object sender, EventArgs e)
{
Button b = new Button();
b.Left = 10;
b.Top = 10;
b.Text = "Button!";
this.Controls.Add(b); //'this' would be the form self-pointer
}
Events are different. These are what will change in Form1.cs, but changes made to the designer will not always be reflected in the code-behind file for events. This is the nature of VS. Sometimes deleting a button will not delete the events from the code and vise-versa.
You may have to remove events from controls manually in the code file.
Code for added controls can usually be forced by changing the control in the designer, but at least one thing that won't change is event names when the control name changes. For instance, if a button is called button1 and links to the click event button1_click(object sender, EventAgrs e), changing the button name to button12345 will not change the event name.
You can change linked events in the designer by opening the control properties and clicking on the lightning bolt. This shows all events for a control. Double clicking in an event field will either take you to the linked event or generate an event if the field is blank. This dialog will also allow multiple controls to link to a single event.
Here is an example of the event properties dialog:
You may just have to fiddle with adding controls, linking events, removing controls, etc. to get a feel for when changes are updated across both designer and code-behind and when changes are not updated.
There is a Form1.Designer.cs file, the designer code lies in this file. If u cant see the file in solution explorer than there is a button on the solution explorer pane on top, that is view files. Click it.
Visual studio provides you drag and drop functionality of controls, and to change the properties just right click on the control and click 'properties'. So actually you may not need to change the designer code. But you can as u like...