Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1
    Utente di HTML.it L'avatar di LMondi
    Registrato dal
    Sep 2004
    Messaggi
    1,291

    VB6 - acquisire str di RichTexBox

    Vorrei sapere se é possibile acquisire in una variabile la sola prima stringa di un testo presente in un controllo RichTextBox.
    Grazie per l'aiuto.
    LM

  2. #2
    Utente di HTML.it L'avatar di LMondi
    Registrato dal
    Sep 2004
    Messaggi
    1,291
    Ma secondo voi, la cosa é fattibile, o dovrei ricorrere ad un altro accorgimento ?
    LM

  3. #3
    Utente di HTML.it L'avatar di Brainjar
    Registrato dal
    Nov 2004
    Messaggi
    1,162
    Cosa intendi per 'la sola prima stringa' la prima parola o la
    prima frase ?

    Ciao,
    Ciao, Brainjar

  4. #4
    Utente di HTML.it L'avatar di LMondi
    Registrato dal
    Sep 2004
    Messaggi
    1,291
    Ciao Brainjar.
    Scusa, rettifico:
    Il controllo ha una lunghezza fissa delle singole righe (per riga o stringa, intendo: la prima sequenza di caratteri da sinistra a destra che compongono una riga) e complessivamente non può contenere più di 1550 caratteri.
    LM

  5. #5
    Utente di HTML.it L'avatar di Brainjar
    Registrato dal
    Nov 2004
    Messaggi
    1,162
    Controlla la presenza del carattere CHR$(10) che dovrebbe
    indicare la fine riga, con instr().

    Ciao,
    Ciao, Brainjar

  6. #6
    Utente di HTML.it L'avatar di LMondi
    Registrato dal
    Sep 2004
    Messaggi
    1,291
    "Controlla la presenza del carattere CHR$(10) " si ma dei predetti caratteri c'è ne sono uno per ciascuna riga, come faccio a prendere la prima riga.
    Però sò che di righe c'é ne sono max 11.
    La cosa mi sembra un pò ingarbugliata ed alla fine poco affidabile.
    E se prevedessi che al termine della stesura della prima riga, questa venisse automaticamente portata in una variabile ?
    Cosa ne pensi ?
    LM

  7. #7
    Utente di HTML.it L'avatar di Brainjar
    Registrato dal
    Nov 2004
    Messaggi
    1,162
    "Controlla la presenza del carattere CHR$(10) " si ma dei predetti caratteri c'è ne sono uno per ciascuna riga, come faccio a prendere la prima riga.
    Il primo che trovi, termina la prima riga, il secondo, la seconda ...

    Il meccanismo, appena abbozzato potrebbe essere questo :
    codice:
      
        Dim st As Integer, pt As Integer
        Dim Stringa As String
        
        st = 1
        pt = 1
        Do
            ' ....
            pt = InStr(st, Stringone, Chr$(10))
            If pt > 0 Then
               ' Trovata
               Stringa = Mid$(Stringone, st, pt + st)
            End If
            ' ....
        Loop
    Ciao,
    Ciao, Brainjar

  8. #8
    Utente di HTML.it L'avatar di LMondi
    Registrato dal
    Sep 2004
    Messaggi
    1,291
    Brainjar, hai ragione.
    Ora sistemo il codice del tuo esempio.
    Ciao e grazie !
    LM

  9. #9
    Utente di HTML.it L'avatar di LMondi
    Registrato dal
    Sep 2004
    Messaggi
    1,291
    Brainjar, nel ringraziarti posto il codice che restituisce la prima riga inserita in un RichTextBox:
    codice:
    Private Sub Command1_Click()
    
        Dim st As Integer, pt As Integer
        Dim Stringa As String
        
        st = 1
        pt = 1
        
        Do
            pt = InStr(st, RTtBox1.Text, Chr$(10))
            'La prima riga è stata trovata:
            If pt > 0 Then
               Stringa = Mid$(RTtBox1.Text, st, pt)
                Lbl1.Caption = Stringa
                Exit Do
            End If
        Loop
    End Sub
    LM

  10. #10
    Io avevo fatto in questo modo provo a postarlo magari può esserti utile.
    In un modulo di classe chiamato "CTextBoxEx"
    codice:
    Option Explicit
    
    'API function declarations
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    'Window messages sent to the textbox
    Private Const EM_CANUNDO = &HC6
    Private Const EM_GETFIRSTVISIBLELINE = &HCE
    Private Const EM_GETLINE = &HC4
    Private Const EM_GETLINECOUNT = &HBA
    Private Const EM_GETMODIFY = &HB8
    Private Const EM_LINEFROMCHAR = &HC9
    Private Const EM_LINEINDEX = &HBB
    Private Const EM_LINELENGTH = &HC1
    Private Const EM_SETMODIFY = &HB9
    Private Const EM_UNDO = &HC7
    
    'local variables to hold property values
    Private WithEvents mtxtBox As RichTextBox
    Private mhWnd As Long 'the hWnd of the textbox
    
    Public Function LineLen(CharPos As Long)
    'Returns the number of character of the line that
    'contains the character position specified by CharPos
    LineLen = SendMessage(mhWnd, EM_LINELENGTH, CharPos, 0&)
    End Function
    
    Public Function GetLineFromChar(CharPos As Long) As Long
    'Returns the zero based line number of the line
    'that contains the specified character index
    GetLineFromChar = SendMessage(mhWnd, EM_LINEFROMCHAR, CharPos, 0&)
    End Function
    
    Public Function LineCount() As Long
    'Returns the number of lines in the textbox
    LineCount = SendMessage(mhWnd, EM_GETLINECOUNT, 0&, 0&)
    End Function
    
    Public Function TopLine() As Long
    'Returns the zero based line index of the first
    'visible line in a multiline textbox.
    'Or the position of the first visible character
    'in a none multiline textbox
    TopLine = SendMessage(mhWnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
    End Function
    
    Public Function CanUndo() As Boolean
    'Returns True if it's possible to make an Undo
    Dim lngRetVal As Long
    
    lngRetVal = SendMessage(mhWnd, EM_CANUNDO, 0&, 0&)
    CanUndo = (lngRetVal <> 0)
    End Function
    
    Public Function GetCharFromLine(LineIndex As Long)
    'Returns the index of the first character of the line
    'check if LineIndex is valid
    If LineIndex < LineCount Then
      GetCharFromLine = SendMessage(mhWnd, EM_LINEINDEX, LineIndex, 0&)
    End If
    End Function
    
    Public Function GetLine(LineIndex As Long) As String
    'Returns the text contained at the specified line
    Dim bArray() As Byte 'byte array to contain the returned string
    Dim lngLineLen As Long 'the length of the line
    Dim sRetVal As String 'the return value
    
    'Check the LineIndex value
    If LineIndex >= LineCount Then
      GetLine = ""
      Exit Function
    End If
    'get the length of the line
    lngLineLen = LineLen(GetCharFromLine(LineIndex))
    If lngLineLen < 1 Then
      GetLine = ""
      Exit Function
    End If
    ReDim bArray(lngLineLen + 1)
    'The first word of the array must contain
    'the length of the line to return
    bArray(0) = lngLineLen And 255
    bArray(1) = lngLineLen \ 256
    SendMessage mhWnd, EM_GETLINE, LineIndex, bArray(0)
    'convert the byte array into a string
    sRetVal = Left(StrConv(bArray, vbUnicode), lngLineLen)
    'return the string
    GetLine = sRetVal
    End Function
    
    Public Sub Undo()
    'Undo the last edit
    SendMessage mhWnd, EM_UNDO, 0&, 0&
    End Sub
    
    Public Sub DelLine(LineIndex As Long)
    'Deletes the specified line from the textbox
    Dim lngSelStart As Long 'used to save the caret position
    Dim lngLineLen As Long  'the length of the line to delete
    Dim lngCharPos As Long  'the index of the first character on the line
    
    If LineIndex >= LineCount Then
      Exit Sub
    End If
    lngSelStart = mtxtBox.SelStart
    lngCharPos = GetCharFromLine(LineIndex)
    lngLineLen = LineLen(lngCharPos)
    mtxtBox = Left$(mtxtBox, lngCharPos) & Mid$(mtxtBox, lngCharPos + lngLineLen + 1)
    mtxtBox.SelStart = lngSelStart
    End Sub
    
    Public Property Let IsDirty(ByVal blnDirty As Boolean)
    Dim lngDirty As Long
    
    lngDirty = Abs(blnDirty) '1 = True in API functions not -1 as in VB
    SendMessage mhWnd, EM_SETMODIFY, lngDirty, 0&
    End Property
    
    Public Property Get IsDirty() As Boolean
    IsDirty = (SendMessage(mhWnd, EM_GETMODIFY, 0&, 0&) <> 0)
    End Property
    
    Public Property Set RichTextBox(txtNewBox As RichTextBox)
    Set mtxtBox = txtNewBox
    mhWnd = txtNewBox.hwnd
    End Property
    
    Public Property Get RichTextBox() As RichTextBox
    Set RichTextBox = mtxtBox
    End Property
    
    Private Sub mtxtBox_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim lngLineIndex As Long
    
    If Shift = vbCtrlMask Then
      Select Case KeyCode
        Case vbKeyA 'CTRL+A = Select all
          With mtxtBox
            .SelStart = 0
            .SelLength = Len(.Text)
          End With
        Case vbKeyY 'CTRL+Y = Cut current line and put it on the clipboard
          lngLineIndex = GetLineFromChar(mtxtBox.SelStart)
          Clipboard.SetText GetLine(lngLineIndex)
          DelLine GetLineFromChar(lngLineIndex)
      End Select
    End If
    End Sub
    In Form_Load

    codice:
       Set txtBox = New CTextBoxEx
       Set txtBox.RichTextBox = tua_RichTextBox
    Procedura

    codice:
    Private Sub Command_Click()
      Label1.Caption = txtBox.GetLine(txtBox.GetLineFromChar(textindirizzo.SelStart))
      Label2.Caption = txtBox.GetLine(txtBox.GetLineFromChar(textindirizzo.SelStart) + 1)
      Label3.Caption = txtBox.GetLine(txtBox.GetLineFromChar(textindirizzo.SelStart) + 2)
      Label4.Caption = txtBox.GetLine(txtBox.GetLineFromChar(textindirizzo.SelStart) + 3)
    Sembra più macchinoso ma è più gestibile
    Ciao
    I'll see you on the dark side of the moon

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.