I don't do much WinFom development so I am not too familiar with the MenuStrip control. I have added a menu strip to my form and added (1) item to it. All of this was done using the designer.
So I have Utilities -> Download Utility. When I double click on 'Download' in the designer an event handler is created for me.
private void downloadUtilityToolStripMenuItem_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Ding!");
}
UPDATE:
I noticed that the IntializeComponent() in the constructor of my form never seems to be run. I have placed a breakpoint in the constructor and it never hits. I refactored this form to change the name from the default (form1) to 'main'. I assume this is the problem but I don't see why. All of the form1 references seemed to have been updated. I did this with the IDE.
When I debug this application I can never seem to get this event to fire. What am I missing here?
-Nick
Check on the property page of the menu item (under events - click the lightning icon) if the Click event has a handler.
Check:
Properties Window for the menu, click on the menu item in question for the 'Download'
Click on the 'Lightening Bolt', a small icon below the top of the Properties Window, if you were to mouse over it, it would display 'Events' in the tooltip.
Scroll down and look for the 'Click Event' under Actions, double click it, to let VS automatically fill in the event handler for you
OR
Double click on the menu item within the Forms Designer, that will default to the menu item's click event and fill in the code for the 'Download' Menu item, i.e. MessageBox.Show("Ding");
Hope this helps,
Best regards,
Tom.
I got it working. Apparently when debugging the project it wasn't rebuilding. After refactoring the name of my form it was necessary to 'Rebuild' the solution. Now all over my events work as they should. Thanks for the help.
Related
I am just testing with the buttons and textboxes when I found that 2 buttons and a textbox isn't doing anything while the 2 buttons are working just fine.
0 references is showing over these buttons
private void button1_Click(object sender, EventArgs e)
{
}
The buttons does exist in Form (Design).
those which are working shows 1 references
Just because the method button1_Click (please name your buttons something better; it takes just a couple of seconds to rename a button after you add it to a form) exists in Form1.cs does not mean it's wired up to be the button click handler
Stop the app if it is running,
go into the forms designer,
get properties on the button (click on it and look in the grid in the right, or right click and choose properties if no grid is showing under solution explorer),
click the lightning bolt at the top of the properties grid,
scroll to the Click line,
drop down the setting to the right of the click and choose button1_Click
repeat for other unlinked buttons
ps this is how you can link multiple controls to the same handler
Oh, and take the time to rename all your buttons, so they are called like saveButton, cancelButton - it'll make your program a lot easier to read both for yourself and others you ask for help from eg on SO. Then rename your click handlers by focusing the caret on the method name and pressing Ctrl-R-R. Files like Form1 can be renamed and if the class inside them is also called Form1 then VS will offer to rename the class too, bonus! :)
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.
I'm new to Visual Studio and am converting a C# console app to VS so that I can give it a GUI. The GUI piece is definitely a learning curve.
I have a main Form1 with Tab1 and Tab2. I can call this.AcceptButton, but it only appears to be at the form level. Is there a way for each tab to have an AcceptButton? I can't call this.Tab1.AcceptButton, however when I am in Tab2, the function of AcceptButton doesn't seem to trigger. Is it because the button doesn't exist on the tab that is in focus?
I could certainly forgo using AcceptButton if there's not a clean way to do this, but it would increase the usability of the application.
Thanks in advance!
You can listen to SelectedIndexChanged event on your TabControl. So whenever tab is change this event will fire and you can use assign another button to AcceptButton.
Have a look at this: Change accept button with tabs
which event to use to get the menu strip item clicked?
I've tried the menuStrip_ItemClicked event of the menuStrip; but it fires only on the top menus only ( like "File", "Edit", "Windows").
I want to catch the sub menu items clicked event.
Thanks.
If you are talking about WindowsForms and use VisualStudio:
Go to design of your menu, rigth click on menu item you want, select properties, in property grid select events tab, select Click or MenuClick (don't remember precisely) event and double click on it.
Done
I think you should hook to click event of each ToolStripMenuItem. It can be the done with the same event handler if you prefer to have logic for it in one function.
Every subitem has its own event for clicking. In the winforms designer, you can just double click on the correct item you want to let happen. It will create a saveButton_clicked event or whatever event you would like and you can specify the information for that specific action in the method.
You can even let the shortcut commands work like CTRL+S when assigning them in the designer of visual studio.
I would like to intercept the event in a .NET Windows Forms TabControl when the user has changed tab by double-clicking the tab (instead of just single-clicking it).
Do you have any idea of how I can do that?
The MouseDoubleClick event of the TabControl seems to respond just fine to double-clicking. The only additional step I would do is set a short timer after the TabIndexChanged event to track that a new tab has been selected and ignore any double-clicks that happen outside the timer. This will prevent double-clicking on the selected tab.
For some reason, MouseDoubleClick, as suggested by Jason Z is only firing when clicking on the tabs and clicking on the tab panel does not do anything, so that's exactly what I was looking for.
How about subclassing the TabControl class and adding your own DoubleClick event?