IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

[VB2005] recuperer la ligne et la colonne en cours d'une richtextbox


Sujet :

Windows Forms

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    121
    Détails du profil
    Informations personnelles :
    Âge : 48
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 121
    Points : 96
    Points
    96
    Par défaut [VB2005] recuperer la ligne et la colonne en cours d'une richtextbox
    Bonjour

    1 --------------------------------------------------------------------
    je cherche le code qui permet d'indiquer :
    - la ligne
    - la colonne
    (- et meme le caratere)

    en cours au fur et a mesure de la saisie dans une richtextbox ou a l'evenement mousedown de la souris
    afin de l'afficher bien entendu dans la barre de statut

    en fait comme visual studio fait

    bon deja pour la ligne, ca doit etre quelque chose comme cela mais quest ce que je mets pour recuperer la ligne courante de la richtextbox (c un integer) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Dim nligne As Integer = RichTextBox1.GetLineFromCharIndex(...)
    2 --------------------------------------------------------------------
    a pi tiens pendant que jy suis
    quelquun pourrait il m'expliquer a quoi sert exactement chaque element (description) et comment on utilise API SendMessageW pour effectuer la coloration syntaxique dans un éditeur comme visual studio ou code snippet editor (mots cles du langage)
    EXEMPLES :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
     
    #Region "win API"
     
     
       Private Sub SetDefaultTabStop(ByVal tabSize As Int32)
          NativeMethods.SetDefaultTabStop(tabSize, Me)
       End Sub
     
       Private Function GetText() As String
          Return NativeMethods.GetText(Me)
       End Function
     
       Private Function GetSelection() As NativeMethods.CharRange
          Return NativeMethods.GetSelection(Me)
       End Function
     
       Private Sub SetSelection(ByVal range As NativeMethods.CharRange)
          NativeMethods.SetSelection(range, Me)
       End Sub
     
       Private Class NativeMethods
     
          Private Sub New()
             ' class is Shared
          End Sub
     
          Friend Shared Sub SetDefaultTabStop(ByVal tabSize As Int32, ByVal editor As CodeEditor)
             SendMessageW(New HandleRef(editor, editor.Handle), &HCB, New IntPtr(1), tabSize * 4)
          End Sub
     
          Friend Shared Function GetText(ByVal editor As CodeEditor) As String
             Dim href As New HandleRef(editor, editor.Handle)
             Dim length As Int32 = SendMessageW(href, WM_GETTEXTLENGTH, IntPtr.Zero, 0).ToInt32 + 2
             Dim sb As New System.Text.StringBuilder(length + 2)
             SendMessageW(href, WM_GETTEXT, New IntPtr(length), sb)
             Return sb.ToString
          End Function
     
          Friend Shared Function GetSelection(ByVal editor As CodeEditor) As NativeMethods.CharRange
             Dim range As CharRange
             SendMessageW(New HandleRef(editor, editor.Handle), EM_EXGETSEL, IntPtr.Zero, range)
             Return range
          End Function
     
          Friend Shared Sub SetSelection(ByVal range As NativeMethods.CharRange, ByVal editor As CodeEditor)
             SendMessageW(New HandleRef(editor, editor.Handle), EM_EXSETSEL, IntPtr.Zero, range)
          End Sub
     
          Friend Shared Function GetOLEInterface(ByVal editor As CodeEditor) As ITextDocument
             Dim iDoc As ITextDocument = Nothing
             SendMessageW(New HandleRef(editor, editor.Handle), EM_GETOLEINTERFACE, IntPtr.Zero, iDoc)
             Return iDoc
          End Function
     
     
    		'Friend Shared Function GetCurrentLine(ByVal editor As CodeEditor) As String
    		'   Return GetCurrentLine(New HandleRef(editor, editor.Handle))
    		'End Function
     
          Friend Shared Function GetCurrentLine(ByVal href As HandleRef) As String
             Return GetLine(href, GetCurrentLineNumber(href))
          End Function
     
    		'Friend Shared Function GetLine(ByVal editor As CodeEditor, ByVal lineNumber As Int32) As String
    		'   Return GetLine(New HandleRef(editor, editor.Handle), lineNumber)
    		'End Function
     
          Friend Shared Function GetLine(ByVal href As HandleRef, ByVal lineNumber As Int32) As String
             Dim lineLength As Int32 = GetLineLength(href, lineNumber)
     
             If lineLength <= 0 Then Return ""
     
             Dim sb As New StringBuilder(lineLength + 2)
             sb.Append(VB.ChrW(lineLength))
             Dim rtn As Int32 = SendMessageW(href, EM_GETLINE, New IntPtr(lineNumber), sb).ToInt32
     
             Return sb.ToString(0, rtn)
     
          End Function
     
     
     
    		'Friend Shared Function GetLineLength(ByVal editor As CodeEditor, ByVal lineNumber As Int32) As Int32
    		'   Return GetLineLength(New HandleRef(editor, editor.Handle), lineNumber)
    		'End Function
     
          Private Shared Function GetLineLength(ByVal href As HandleRef, ByVal lineNumber As Int32) As Int32
             Return SendMessageW(href, EM_LINELENGTH, New IntPtr(GetLineCharIndex(href, lineNumber)), 0).ToInt32
          End Function
     
     
    		'Friend Shared Function GetCurrentLineNumber(ByVal editor As CodeEditor) As Int32
    		'   Return GetCurrentLineNumber(New HandleRef(editor, editor.Handle))
    		'End Function
     
          Friend Shared Function GetCurrentLineNumber(ByVal href As HandleRef) As Int32
             Return SendMessageW(href, EM_LINEFROMCHAR, New IntPtr(-1), 0).ToInt32
          End Function
     
     
    		'Friend Shared Function GetLineCharIndex(ByVal editor As CodeEditor, ByVal lineNumber As Int32) As Int32
    		'   Return GetLineCharIndex(New HandleRef(editor, editor.Handle), lineNumber)
    		'End Function
     
          Friend Shared Function GetLineCharIndex(ByVal href As HandleRef, ByVal lineNumber As Int32) As Int32
             Return SendMessageW(href, EM_LINEINDEX, New IntPtr(lineNumber), 0).ToInt32
          End Function
     
     
     
     
          Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lparam As tom.ITextDocument) As IntPtr
          Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByVal lparam As StringBuilder) As IntPtr
          Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lparam As Int32) As IntPtr
          Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lparam As CharRange) As IntPtr
     
     
          Private Const WM_USER As Int32 = &H400
          Private Const EM_EXGETSEL As Int32 = (WM_USER + 52)
          Private Const EM_EXSETSEL As Int32 = (WM_USER + 55)
          Private Const EM_GETOLEINTERFACE As Int32 = (WM_USER + 60)
          Private Const EM_GETLINE As Int32 = &HC4
          Private Const EM_LINEFROMCHAR As Int32 = &HC9
          Private Const EM_LINELENGTH As Int32 = &HC1
          Private Const EM_LINEINDEX As Int32 = &HBB
          Private Const EM_SETTABSTOPS As Int32 = &HCB
          Private Const WM_GETTEXT As Int32 = &HD
          Private Const WM_GETTEXTLENGTH As Int32 = &HE
     
          Friend Structure CharRange
             Public Min As Int32
             Public Max As Int32
          End Structure
     
       End Class
     
     
    #End Region
    ------------------------------------------------------------------------
    oOOoo mince il m'a semblé avoir vu dans code ce que je vous demandait au débutt loooooll (trouver position ligne courante richtextbox)
    rooooo loooll

    bon c'est vrai chui un peu feignant la lool mais jai po envie de chercher svp

    merkii
    ++

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Décembre 2005
    Messages
    121
    Détails du profil
    Informations personnelles :
    Âge : 48
    Localisation : France

    Informations forums :
    Inscription : Décembre 2005
    Messages : 121
    Points : 96
    Points
    96
    Par défaut
    Bon comme je suis sympa je vous fait profiter de mon début de solution, ca ne doit pas etre parfait encore mais bon c'est sur la bonne voie
    Permet de recuperer la ligne ,colonne et caractere en cours dans un controle richtextbox un peu comme le fait visual studio lol


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
     
    Imports System.text
    Imports System.Runtime.InteropServices
    Imports Microsoft.VisualBasic.Strings
     
    Public Class Form1
        ''' <summary>
        ''' DECLARATIONS
        ''' </summary>
        ''' <remarks></remarks>
        Dim nligne_courante, nCaractere As Integer
     
    #Region "win API"
     
        Friend Shared Function GetCurrentLine(ByVal href As HandleRef) As String
            Return GetLine(href, GetCurrentLineNumber(href))
        End Function
     
        Friend Shared Function GetLine(ByVal href As HandleRef, ByVal lineNumber As Int32) As String
            Dim lineLength As Int32 = GetLineLength(href, lineNumber)
     
            If lineLength <= 0 Then Return ""
     
            Dim sb As New StringBuilder(lineLength + 2)
            sb.Append(ChrW(lineLength))
            Dim rtn As Int32 = SendMessageW(href, EM_GETLINE, New IntPtr(lineNumber), sb).ToInt32
     
            Return sb.ToString(0, rtn)
     
        End Function
     
     
        Private Shared Function GetLineLength(ByVal href As HandleRef, ByVal lineNumber As Int32) As Int32
            Return SendMessageW(href, EM_LINELENGTH, New IntPtr(GetLineCharIndex(href, lineNumber)), 0).ToInt32
        End Function
     
     
        Friend Shared Function GetCurrentLineNumber(ByVal href As HandleRef) As Int32
            Return SendMessageW(href, EM_LINEFROMCHAR, New IntPtr(-1), 0).ToInt32
        End Function
     
        Friend Shared Function GetLineCharIndex(ByVal href As HandleRef, ByVal lineNumber As Int32) As Int32
            Return SendMessageW(href, EM_LINEINDEX, New IntPtr(lineNumber), 0).ToInt32
        End Function
     
     
     
     
        ' Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lparam As ITextDocument) As IntPtr
        Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByVal lparam As StringBuilder) As IntPtr
        Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lparam As Int32) As IntPtr
        Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lparam As CharRange) As IntPtr
     
     
        Private Const WM_USER As Int32 = &H400
        Private Const EM_EXGETSEL As Int32 = (WM_USER + 52)
        Private Const EM_EXSETSEL As Int32 = (WM_USER + 55)
        Private Const EM_GETOLEINTERFACE As Int32 = (WM_USER + 60)
        Private Const EM_GETLINE As Int32 = &HC4
        Private Const EM_LINEFROMCHAR As Int32 = &HC9
        Private Const EM_LINELENGTH As Int32 = &HC1
        Private Const EM_LINEINDEX As Int32 = &HBB
        Private Const EM_SETTABSTOPS As Int32 = &HCB
        Private Const WM_GETTEXT As Int32 = &HD
        Private Const WM_GETTEXTLENGTH As Int32 = &HE
     
        Friend Structure CharRange
            Public Min As Int32
            Public Max As Int32
        End Structure
     
        ' End Class
     
     
    #End Region
     
     
        ''' <summary>
        ''' EVENEMENTS
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.afficher_postion_ligne_colonne_caratere()
        End Sub
     
        Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
            Me.afficher_postion_ligne_colonne_caratere()
        End Sub
     
        Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
            Me.afficher_postion_ligne_colonne_caratere()
        End Sub
     
        ''' <summary>
        ''' AFFICHAGE
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub afficher_postion_ligne_colonne_caratere()
     
            Try
     
                nligne_courante = Form1.GetCurrentLineNumber(New HandleRef(RichTextBox1, RichTextBox1.Handle))
                nCaractere = Form1.GetLineLength(New HandleRef(RichTextBox1, RichTextBox1.Handle), nligne_courante)
     
                tsligne.Text = "Ln " & (nligne_courante + 1).ToString
                tsligne.Text += Space(20) & "Col " & nCaractere.ToString
                tsligne.Text += Space(20) & "Car " & nCaractere.ToString
     
            Catch ex As Exception
                '//
            End Try
     
        End Sub
     
    End Class

    j'ai mm laissé en bonus GetLine qui permet de recuperer le texte de la ligne en cours

    voila
    merci moii looooooool
    ++

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XSLT 1.0] XSL connaitre la ligne et la colonne en cours
    Par djibril dans le forum XSL/XSLT/XPATH
    Réponses: 5
    Dernier message: 01/03/2013, 08h55
  2. Recuperer la ligne qui a la valeur minimum d'une variable
    Par cedrix57 dans le forum SAS Base
    Réponses: 7
    Dernier message: 06/03/2009, 10h28
  3. [VB.NET] Recuperer numero ligne dans DATAGRID apres event
    Par stephane93fr dans le forum Windows Forms
    Réponses: 4
    Dernier message: 13/01/2005, 11h58
  4. Recuperer la ligne de commande du DOS
    Par Kernel32.DLL dans le forum Autres éditeurs
    Réponses: 4
    Dernier message: 23/08/2004, 15h50
  5. Réponses: 4
    Dernier message: 21/05/2004, 09h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo