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! :)
Related
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'm working on a VB Winforms project (although I'm just as fine with a C# solution) and have the following set up:
I have a ContextMenuStrip on the form, msCreateReports
I have a MenuStrip at the top of the form with one menu item being Create Reports and its DropDown set to msCreateReports
I have a command button on the form cmdCreateReports
Now, for my command button, I have the following code for its click event:
Private Sub cmdCreateReports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateReports.Click
msCreateReports.Show(Cursor.Position.X, Cursor.Position.Y)
End Sub
EDIT (Update / Explanation):
Basically, what I'm looking for functionality-wise is to have this contextmenustrip be able to show up in one of 2 different places, depending on where the user clicks...
If they click on the menu option, the context menu appears as a drop-down on the Create Reports menu item OR if the user clicks the command button, the menu will apeear as a context menu on the side of the mouse pointer. I want the same menu to be able to appear on either one of these two locations depending on where the user clicks to make this menu appear.
Now, my problem is that only the first time the command button is clicked, the ContextMenuStrip appears up top by the menu, rather than on top of the command button, as I would like it to.
After the first click, the menu appears in the correct location... What did I do wrong / how can I fix this??
Thanks!!
I have the same problem (I'm using Visual Studio 2010 SP1, and C#). I don't think we did anything wrong, it looks like a Winforms bug to me.
I fixed it like this:
1) I have unset (using the visual designer) the DropDown property of the main strip item.
2) I have defined the Opening event on the contextMenuStrip, and the DropDownOpening event on the main strip item like this:
private void toolStripMyMenuItem_DropDownOpening(object sender, EventArgs e)
{
toolStripMyMenuItemMyLists.DropDown = contextMenuStrip;
}
private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
{
toolStripMyMenuItemMyLists.DropDown = contextMenuStrip;
}
And I don't have this problem anymore. Hope it will help you too :-)
Cursor.Position.X and Cursor.Position.Y are relative to Form, you need to use the Overloaded method ContextMenuStrip.Show(Control control, Point pos)
Example :
//control = the control you have added context menu
msCreateReports.Show(control, new Point(Cursor.Position.X, Cursor.Position.Y));
I had the same problem. I believe this to be a bug also as this behavior only happens the first time. So I force it to open and then close it before the user has a chance to interact with it. So in effect I use it once, before the user does and then it behaves correctly. Here is the code I added in the form load method. My combo box is used for allowing the user to select from a list of printers.
printerToolStripMenuItem.ShowDropDown();
toolStripComboBoxPrinter.Owner.Hide();
printerToolStripMenuItem.Owner.Hide();
I am having a strange problem with the .NET TabControl in C# (Visual Studio 2010). Start a Windows Forms Application. Add a tab control and a button. Add two different labels to the two tab pages so you can differentiate them. The purpose of the button is just to act as a next button; subscribe to the its Click event with the code:
tabControl1.SelectTab(1);
Let's assume the user entered something wrong on the first tab, so when they try to go to the second tab we want to send them back, so subscribe to the tab control's SelectedIndexChanged event with the code:
if(tabControl1.SelectedIndex == 1)
{
tabControl1.SelectTab(0);
}
Now run the program and click the button. You will notice that as judged by the highlighted tab at the top, the first tab page is the one that appears to be selected, as you'd expect. However, as judged by the tab page that actually appears in the body of the tab control, it's still the second tab page that shows up! Calls to various controls' Focus(), Update(), and Refresh() functions don't seem to help. What is going on here?
I repro. This is a generic problem with event handlers, you can confuse the stuffing out the native Windows control by jerking the floor mat like that. TreeView is another control that's very prone to this kind of trouble.
There's an elegant and general solution for a problem like this, you can use Control.BeginInvoke() to delay the command. It will execute later after the native control is done with the event generation and all side-effects have been completed. Which solves this problem as well, like this:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
if (tabControl1.SelectedIndex == 1) {
this.BeginInvoke(new Action(() => tabControl1.SelectTab(0)));
}
}
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...
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.