How to copy events associated with control when copying controls? - c#

When I need to re arrange positions of buttons of controls I use copy/cut and paste but I lose the asociation of the events and need to re assign manually.
Is there a way to do that without losing them?
For example I cut a button and paste it elsewhere.
It had associated the bt_ClickEventFunction, but when pasting it, it loses this.

You can drag them on the Form in order to change their location, you don't need to cur&paste them.
Copy&Paste creates another control with new name and Text while Cut&Paste deletes everything from designer and then regenerates the control as it were but without lines for adding events. I presume that this is because the code for events handlers is in file other that the file created by designer and when you cut the control, event handler doesn't gets deleted from the code since it can be the event handler for some other control also. So when pasting, designer actually only creates the new control as it would when you drag it from the Toolbox but sets the properties from the control hat has been cut.
EDIT Edit based on your comments.
You can do drag&drop even with TabPages. You need to drag it for the small rectangle with arrows like shown on the image:
and you can drag it to the other TabControl, in that case, TabPage will retain all "associated" event handlers.

To restore all the event procedures to their respective controls, go to the VBA code window of the form and then cut, copy and paste the whole module. You may press Ctrl+A, Ctrl+X and Ctrl+V to do so.

Related

In Winforms, whenever I edit the form I lose other changes I've made to Designer.cs, but I can't figure out how to add a "Resize Event" normally

I'm trying to get a resize event working, and I tried just adding "this.Reszie += whatever" and that worked fine, but whenever I make other changes to the Form through the editor, it completely erases that line (as well as anything else in the Form1.Designer.cs class). My questions is, how can I edit this as intentioned, like how double clicking on a button or text box will automatically do this all? Thanks
You can select event handlers in the properties window. You must click on the flash symbol, to switch form properties to events view.
Also, before you do manual changes to ".designer.cs", close the form designer (because saving the form overwrites the ".designer.cs" file).
Usually, you should avoid doing changes in the ".designer.cs" file, but there are rare occasions where this is helpful. E.g., you have used a TextBox and want to replace it by a custom textbox or third-party textbox. Then changing the type manually will allow you to do it without removing and re-adding these controls.

visual studio c# windows form checkBox

I need your help
as you can see from the screenshot image
I am trying to make a font dialog where i have:
a Label to test the change that will happen when I
click in the checkbox .as you can see I have 3 checkboxs.
the problem is that I cant make all the checkboxs work together
so the text changed to Bold,Italic and underline..help me please
.....
my Form design
i try to use if else statement
and also switch one and still I don't know how to do it that is why i m here
It's a bit unclear, as others have noted, but.. if you are wanting to combine the results of checked and/or unchecked options to display the results of a user's font styling choices?
THEN:
1.) double-click in your V.S. form designer one of the checkboxes, this will create an event handler in your form's C# code file. The function created will be something like checkbox1_checked(object sender, EventArgs e)
2.) In the event handler function that Visual Studio has scaffolded for you, write all the code necessary to consider the .Checked state of all checkbox controls and update your Label1 control appropriately (depending on your goal, this may require custom .NET painting logic that is too much for a beginner to think about)
3.) Go back to the other controls on your design surface, now select them.. keeping an eye on your lower-right 'Properties' panel, (make sure its switched to the 'events' tab - i.e. click the 'lightning bolt' button). Ensure that each of the other radio buttons that do not yet have an event handler get mapped: Find the row in the Properties panel that shows the word "click" (err.. 'checked' event?). When you click into the white space on the row, next to the word 'checked', it will allow you to select the pre-existing "checkbox1_checked" event handler than you created in step 1.
UPDATED:
4.) the end goal is to wire all checkbox controls to the same event handler function in your form's code-behind.. if you double-click each checkbox at design time.. you'll create separate event handlers.. but with some learning (or following my directions above) you can point them all to the same event handler.

Retain Event handler assignments when cutting and pasting WinForms controls in VS2012 C# project?

Is there a way to maintain the Event handler assignments when cutting and pasting controls in the VS2012 designer? For example, I have a graph control that has a Load event with code in the Event stub. If I cut and paste the graph control, the Load event no longer shows as having an Event handler assignment in the Properties Editor. I have to use the drop-down box next to the Load event to reselect my existing Event handler code for the Load event (redo the assignment). Is there a way to cut and paste controls without losing the Event handler assignments?
In Delphi I could just cut and paste controls freely and the event handler assignments were part of the clipboard text so they were retained when a control was pasted onto a form or frame. I am hoping there is a way to do the same with Visual Studio 2012 and a C# project.
You need to use the Document Outline
View > Other Windows > Document Outline
And find your desired control and then drag and drop it to target container.
In this case you wont miss any events.
When you cut or copy a control using the Visual Studio designer only the properties of the controls are copied. This is by design and in my opinion, is justified. It would be counter intuitive most of the time if you where copying a control and all of the same event handlers where assigned or copied over.
It sounds like the control is remaining in the same Form. In which case dragging and placing the control should suffice for the most part.
One way to reassign the events is as the following:
Double click on the pasted controls, it creates an event with a name like:
button1_Click_1(...)
Now change the function name to the original name button1_Click, the IDE shows a warning about the duplication of functions, click OK and then remove the function you already created.

Strange Button Behavior

My form has several textboxes and several buttons. The textboxes are loaded with data by the load event. If I make changes to the textboxes and then move the cursor over a button, the textboxes are suddenly loaded again with the original information. I don't click a button. I just move the mouse over one. Why is this happening and how do I stop it?
This cannot happen by itself. I suggest you check all event-settings.
For instance, you could have, by accident, linked the Load event to the Button's OnMouseEnter or something like that.
After your comment:
You should absolutely not use the paint event to initialize things. The paint event will be called after every change in the Form.
So move that code to the Load event.

Drag, Drop and Delete

I have a question about drag and drop in WinForms. I know how to enable the user to drag and drop controls around inside the form, but what I'm now trying to do is enable them to drag a LinkLabel ontop of a"Recycle Bin" icon inside my Form and when it detects that something has been dropped onto the Recycle Bin icon, that control will be removed from the Form.
How would I detect if something's been dropped on another control? Would I still use Control.DragEnter & Control.DragDrop?
Thank you
yes, DragEnter and DragDrop is the right way to go, also you need to handle DragOver.
Typically, in these handler you specify what kind of drag-drop is allowed, and in DragDrop do your staff of deleting.
Here is the helpful link for you which can explain you about the DragDrop.
http://www.codeproject.com/KB/combobox/LarryDragAndDrop.aspx
You require to work on following Events:
1. MouseDown 2.DragEnter 3. DragDrop

Categories