VB.Net macros - how to fetch string in clipboard into a string variable

  • Thread starter Thread starter JohnR
  • Start date Start date
J

JohnR

Hi all,

I'm finally sick and tired of manually generating get/set properties for
each private variable in a class so I'm trying to create a macro to do it.

I'm stuck because I can't figure out how to get a string that is in the
clipboard into a variable in the macro. I tried this:

Dim x as string = DTE.ActiveDocument.Selection.Paste() thinking it would
"paste" the clipboard into the string variable X, but it just pasted the
string into the document.

Can somebody give me a code example that transfers the string in the
clipboard into a variable in the macro?

Thanks,
John
 
JohnR said:
Hi all,

I'm finally sick and tired of manually generating get/set properties for
each private variable in a class so I'm trying to create a macro to do it.

I'm stuck because I can't figure out how to get a string that is in the
clipboard into a variable in the macro. I tried this:

Dim x as string = DTE.ActiveDocument.Selection.Paste() thinking it would
"paste" the clipboard into the string variable X, but it just pasted the
string into the document.

Can somebody give me a code example that transfers the string in the
clipboard into a variable in the macro?

Thanks,
John

this defeats the purpose of having private member variables -- the idea
is to perform some sort of testing in the "set". if this is not
necessary, then make the variable public and be done with it...
 
Look up 'Clipboard' & 'Text'. You will then find an example in the built in
MSDN Documentation

Crouchie1998
BA (HONS) MCP MCSE
 
I'm sorry, I must not have made my question clear.

When I'm actually writing the code in the IDE and I type "private _myvar as
string" I want to have a macro automatically generate
the Property get-set code. This question has nothing at all to do about
actually running the program. One way of doing this involves
copying the "private _myvar as string" line into the clipboard,
repositioning the cursor to where you want the property code to be, and
then running the macro that will copy the string in the clipboard into a
string variable and then process it... That is the genesis of my question
of "how do you get a string from the clipboard into a string variable in a
macro?"

John
 
Here is a couple of macros that I use. To use them, you would declare
your private variables like this: Private m_SomeName As Integer. It
will take the name of the property excluding the m_ from the front. If
you don't use m_ you will have to change the InsertProperty method to
handle whatever convention you use. The ReservedWords array is for
property names that are reserved words. I only show a few in this
post, but you can add any reserved word to this array and the macro
will put it in square brackets as needed.

Friend ReservedWords() As String = {"ASSEMBLY", "CLASS", "FORM"}

Public Sub PropertyFromVar()
Dim ts As TextSelection = CType(DTE.ActiveWindow.Selection,
TextSelection)
Dim m_PropertyList(ts.TextRanges.Count - 2) As String
Dim m_Index As Integer
Dim start As EditPoint = ts.TopPoint.CreateEditPoint()
Dim endpt As TextPoint = ts.BottomPoint
Dim sb As New StringBuilder

'Make these upper case


Try
Do While (start.LessThan(endpt))
m_PropertyList(m_Index) =
start.GetText(start.LineLength).Trim.Replace("New", "").Replace("()",
"")
start.LineDown()
start.StartOfLine()
m_Index += 1
Loop
Catch ex As System.Exception
Debug.WriteLine(ex)
End Try

Dim cnt As Integer = m_Index

For m_Index = 0 To m_PropertyList.GetUpperBound(0)
sb.Append(InsertProperty(m_PropertyList(m_Index)))
Next


ts.MoveToPoint(ts.ActivePoint.CodeElement(vsCMElement.vsCMElementClass).GetEndPoint(vsCMPart.vsCMPartWhole))
ts.LineUp()
ts.EndOfLine()
ts.NewLine()
ts.NewLine()

ts.Insert(sb.ToString)

DTE.ExecuteCommand("Edit.FormatDocument")

End Sub
 
Back
Top