Inserting proceedures using the Code Model
I have an addin that I want to be able to insert handlers for the click event of buttons. I found a sample that uses the code model to insert a function, but there are 2 important parts i have not been able to locate. The first is, where can i specify the parameter to the proceedure that is being made? the second is, how can i add the "handles label1.click" at the end?
I know this can also be done with the TextSelection.Insert directly, but using the code model seems to be the better approach.
The article i found is herehttp://forums.microsoft.com/msdn/ShowPost.aspx?PostID=830 and gives exmaples of both ways (very nice by the way). Like I said, I still have the 2 portions ive been unable to answer.
Thanks
Bill
Bill
I could be wrong, but it has been my experience that the codemodel is readonly in VB.NET projects. I have done what you are asking, but have always done it brute force by creating the code for the events, including the handles clause and then inserting the code at the end of the class.
In C# projects some of the codemodel methods write code, but usually get an unimplemented exception in VB.NET.
If you will email me at les@knowdotnet.com, I will try to send you some sample code. if you want it.
HTH
No, it most certainly can be done. at least in 2005 CTP. Refer to the post
BUG: Changing the name of a project item causes data loss which i had posted which has some code
however as i said, there are apparently some aspects that are missing, and I'm wondering if I'm missing something.
Hello Mr. Foust,
I looked into this and added some more lines to do what you're looking for. To insert parameters in a function, you can use the handy CodeFunction2.AddParameter method. There is no method I could find to add a "Handles" statement to the end of a function (or its handler name), but you can go to the end of the function header and insert one manually. The string you pass to the Insert method could probably be intelligently constructed, but in this example, I just pass in a literal string to show one way of doing it. Perhaps there's a more elegant method, but this works. 
Sub AddFunctionExample(ByVal dte As DTE2)
' Before running this example, open a code document from a project
' and place the insertion point inside a class definition.
Try
' Retrieve the CodeClass at the current insertion point.
Dim sel As TextSelection = _
CType(dte.ActiveDocument.Selection, TextSelection)
Dim cls As CodeClass2 = _
CType(sel.ActivePoint.CodeElement( _
vsCMElement.vsCMElementClass), CodeClass2)
' Initialize variables for the new function
' and a textpoint for it.
Dim func As CodeFunction2
Dim funcOffset As TextPoint
' Create a new function in the class.
func = cls.AddFunction("DoSomething",
vsCMFunction.vsCMFunctionFunction, _
vsCMTypeRef.vsCMTypeRefInt, , , )
' Add the first parameter, an integer.
func.AddParameter("firstParam", vsCMTypeRef.vsCMTypeRefInt, 0)
' Get the endpoint of the function header (the first part of the function).
funcOffset = func.GetEndPoint(vsCMPart.vsCMPartHeader)
' At the end of the header, insert the Handles statement.
funcOffset.CreateEditPoint().Insert(" Handles label1.click")
Catch ex As System.Exception
MsgBox(ex.ToString)
End Try
End Sub
Hope this helps!
Regards,
- Kemp Brown [MSFT]