Domanda

So I looked through other user's question but couldn't find on specifically upon what I am looking for. What I am trying to do is very simple. I am writing a Visual Studio Macro and am trying to obtain the number of the current line that the TextSelection is on. So that's it really, my question is quite simple. How do you get the number of the line that the selection is currently at? Any help is greatly appreciated!

Just so it's clear to anybody reading this, I am using VB and am writing Visual Studio Macro.

È stato utile?

Soluzione

Keep in mind that a TextSelection can span multiple lines, so there's potentially a range of lines.

Looking at the docs for TextSelection (i.e. I haven't tested this), you should be able to do something like this:

Dim mySelection As EnvDTE.TextSelection = ' however you get the active selection
mySelection.TopPoint.Line ' gets the line of the top of the selection

If you want to get it based on where the cursor is (top or bottom of the selection) you can try this:

mySelection.ActivePoint.Line

It looks like the TextRanges might also be useful, but it sounds like it's for box selection only, so it might not apply.

mySelection.TextRanges.Item(0).Line

Altri suggerimenti

There is probably a better way than this, but the first way that comes to my mind is something like this.

First, make sure that the last line in your file is "xyz"

 Dim linenumber As Integer = 1
 dim mystring as string = ""

 Using myfile As New IO.StreamReader("C:/myfile")

         mystring = myfile.readline()
         while mystring <> "xyz"

              linenumber += 1
              messagebox.Show(mystring & " is on line " & linenumber)
         end while
 End Using

So if the contents of C:/myfile looked like this....

I

Love

Pie

Then you would get as output....

"I is on line 1"

"Love is on line 2"

"Pie is on line 3"

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top