2009-09-10 7 views

Répondre

14

Si vous voulez dire WinForms, utilisez la propriété SeletionStart pour accéder à la position actuelle du carret. Voici le code pour obtenir l'index, la ligne actuelle et la colonne actuelle.

int index = myTextBox.SelectionStart; 
int currentLine = myTextBox.GetLineFromCharIndex(index); 
int currentColumn = index - myTextBox.GetFirstCharIndexFromLine(currentLine); 
+0

Ça l'a fait, merci! – Cyclone

1
' 
' 
' 
'Imports 
Public Class frmMain 
    ' 
    '- See more at: http://www.visual-basic-tutorials.com/Tutorials/Strings/count-how-many-times-a-string-occurs-in-visual-basic.htm#sthash.zPPNxNjl.dpuf 
    ' But I heavily modidied it to suit My style and needs. VS2012 
    ' 
    'Used by the Sub StripPath(ByRef sPath As String) 
    Public PathOnly As String = Nothing 'Global 
    Public FileOnly As String = Nothing 'Global 
    ' 
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     ' 
     Label1.Text = Nothing 
     ' 
     Me.Text = "Text Operations" 
     ' 
    End Sub 
    ' 
    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click 
     ' 
     MsgBox("The word ''" & tbxSearch.Text & "'' occurs: " & vbCrLf & FindWords(tbxText.Text, tbxSearch.Text) & " time(s).") 
     ' 
    End Sub 
    ' 

    ' 
    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click 
     ' 
     Dim RetVal As String = GetOpenFileName() 
     ' 
     If RetVal <> Nothing Then 
      StripPath(RetVal) 
      ReadAllText(RetVal) 
      Me.Text = FileOnly + " - " + "Text Operations" 
      Label1.Text = PathOnly 
      'MsgBox(PathOnly) 
     End If 
     ' 
    End Sub 
    ' 
    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click 
     ' 
     Application.Exit() 
     ' 
    End Sub 
    ' 
    Private Sub WordCountToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WordCountToolStripMenuItem.Click 
     ' 
     Dim Prompt As String = "Type the word that you wish to search for." 
     Dim Title As String = "Word Count" 
     Dim DefaultStr As String = "ico" 
     Dim RetVal As String = InputBox(Prompt, Title, DefaultStr) 
     ' 
     If RetVal <> Nothing Then 
      ' 
      tbxSearch.Text = RetVal 
      ' 
      MsgBox("The word ''" & tbxSearch.Text & "'' occurs: " & vbCrLf & FindWords(tbxText.Text, tbxSearch.Text) & " time(s).") 
      ' 
     End If 
     ' 
    End Sub 
    ' 
    Private Function FindWords(ByVal TextSearched As String, ByVal Paragraph As String) As Integer 
     ' 
     Dim location As Integer = 0 
     ' 
     Dim occurances As Integer = 0 
     ' 
     Do 
      ' 
      location = TextSearched.IndexOf(Paragraph, location) 
      ' 
      If location <> -1 Then 
       ' 
       occurances += 1 
       ' 
       location += Paragraph.Length 
       ' 
      End If 
      ' 
     Loop Until location = -1 
     ' 
     Return occurances 
     ' 
    End Function 
    ' 
    Private Sub StripPath(ByRef sPath As String) 
     ' 
     'oFileInfo.DirectoryName returns only the path without the "\" 
     'oFileInfo.Name returns only the filename.and extension. "some file.???" 
     Dim sFile As String = sPath '"c:\mydir\subdir\temp\myfile.txt" 
     Dim ioFileInfo As New System.IO.FileInfo(sFile) 
     PathOnly = ioFileInfo.DirectoryName 'Global 
     FileOnly = ioFileInfo.Name 'Global 
     ' 
    End Sub 'StripPath() 
    ' 
    Private Sub tbxText_Click(sender As Object, e As EventArgs) Handles tbxText.Click 
     'Your Code 
     ''int index = myTextBox.SelectionStart; 
     ''int currentLine = myTextBox.GetLineFromCharIndex(index); 
     ''int currentColumn = index - myTextBox.GetFirstCharIndexFromLine(currentLine); 
     'End Your Code 
     ' 
     Dim index = tbxText.SelectionStart 
     Dim currentLine = tbxText.GetLineFromCharIndex(index) 
     Dim currentColumn = index - tbxText.GetFirstCharIndexFromLine(currentLine) 
     tsslLine.Text = "Ln: " & currentLine 
     tsslCol.Text = "Col: " & currentColumn 
     ' 
    End Sub 
    ' 
    Private Sub tbxText_DoubleClick(sender As Object, e As EventArgs) Handles tbxText.DoubleClick 
     ' 
     tbxSearch.Text = tbxText.SelectedText 
     ' 
    End Sub 
    ' 
End Class 
+0

Ma conversion en VS2012 .. c'est dans le - Private Sub tbxText_Click (Expéditeur As Object, e As EventArgs) Gère tbxText.Click – ZipFileX

Questions connexes