Question Delete Object - Create Object and ReUse Code

gmacpro

Member
Joined
Jun 19, 2012
Messages
9
Programming Experience
Beginner
Is there a way to automatically have code inserted when an object is created and code exists?

In other words, if you create a Button and within the Click Event have the following:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Text = "Hello"
End Sub

Then you delete the Button. When you create the next Button although it is called Button1, the code doesn't appear within the Click Event.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Button1.Text = "Hello"
End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub

I can either cut and paste the code or cut and paste the handle info ie: "Handles Button1.Click" and delete the other. Is there a way for the code to automatically be inserted?

TIA

gmacpro
 
In that case, you use the Properties window to select the existing event handler instead of creating a new one. Select your control, open the Properties window, click the Events button and then use the drop-down for the event of interest to select an existing method.
 
Thanks for the reply. I attempted your suggestion but after I delete the Button1, it no longers appears in the Properties Window. The code is still within my project but Button1 is gone.

gmacpro
 
I got it - I created another Button and then did your suggestion and that did the trick.

Perfect - thanks very much.

gmacpro
 
If you can drag-drop the control instead of cut-paste the event handlers will remain.
 
Thanks John,

I know that but I'm a VB6 guy and you couldn't do that in VB6 so what I did was cut the Ojbects and pasted them into the new group. In VB6, all the code would align with the same objects. Now I do stuff and then realize what happened so I'm learning fast on how to fix it.


gmacpro
 
I didn't realise that you were cutting and pasting controls. That would probably have been good information to provide in the first place. As well as dragging and dropping controls themselves, you can also use the Document Outline window to move controls into and out of containers and change the z-order. This is useful for situations where even drag and drop won't work easily, e.g. moving controls from on TabPage to another.
 
Back
Top