Visual Basic vraagjes.

Status
Niet open voor verdere reacties.

Jori13

Gebruiker
Lid geworden
18 jan 2010
Berichten
159
Hallo Helpmij forum mensen, :)

Ik heb een paar kleine vraagjes waar jullie mij misschien bij kunnen helpen.Ik ben bezig om in VB een VBScript editor te maken, vanzelf sprekend hoort daar een regel en karakter teller bij maar ook syntax highliting. Nou heb ik op internet voorbeelden gevonden voor beide. Het probleem is echter dat ze beide niet werken. Mijn vraag; weten jullie een site met wel werkende code (of een waar wordt uigelegd hoe je zelf die code kan schrijven) of weten jullie waarom deze niet werkt en wat ik er aan kan doen?


Alvast heel erg bedankt voor jullie antwoord(en)!


De links en codes:

Syntax-highliting:
http://support.microsoft.com/kb/154884


Code:
Option Explicit

      Private Sub Command1_Click()
        HighlightWords RichTextBox1, "text", vbRed
      End Sub

      Private Function HighlightWords(rtb As RichTextBox, _
                                  sFindString As String, _
                                  lColor As Long) _
                                  As Integer

        Dim lFoundPos As Long           'Position of first character
                                        'of match
        Dim lFindLength As Long         'Length of string to find
        Dim lOriginalSelStart As Long
        Dim lOriginalSelLength As Long
        Dim iMatchCount As Integer      'Number of matches

        'Save the insertion points current location and length
        lOriginalSelStart = rtb.SelStart
        lOriginalSelLength = rtb.SelLength

        'Cache the length of the string to find
        lFindLength = Len(sFindString)

        'Attempt to find the first match
        lFoundPos = rtb.Find(sFindString, 0, , rtfNoHighlight)
        While lFoundPos > 0
          iMatchCount = iMatchCount + 1

          rtb.SelStart = lFoundPos
          'The SelLength property is set to 0 as
          'soon as you change SelStart
          rtb.SelLength = lFindLength
          rtb.SelColor = lColor

          'Attempt to find the next match
          lFoundPos = rtb.Find(sFindString, _
            lFoundPos + lFindLength, , rtfNoHighlight)
        Wend

        'Restore the insertion point to its original
        'location and length
        rtb.SelStart = lOriginalSelStart
        rtb.SelLength = lOriginalSelLength

        'Return the number of matches
        HighlightWords = iMatchCount

      End Function


Regel en karakter teller:
http://www.vbforums.com/showthread.php?p=2161803#post2161803

Code:
Option Explicit

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

Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB

Private Sub GetCursorPos(RTB As RichTextBox, iLine As Integer, iPos As Integer)
Dim lCount As Long
Dim i As Long
Dim LN As Long
    lCount = SendMessage(RTB.hwnd, EM_GETLINECOUNT, 0&, 0&)
    LN = SendMessage(RTB.hwnd, EM_LINEINDEX, -1&, 0&)
    For i = 1 To lCount
        If LN = SendMessage(RTB.hwnd, EM_LINEINDEX, i - 1, 0) Then Exit For
    Next i
    iLine = i
    iPos = RTB.SelStart - LN + 1
End Sub
 
Laatst bewerkt:
Hi,

jouw probleem is zo moeilijk te beoordelen.

Wat voor versie VB gebruik jij?

Gebruik jij de code in een module of een form?

Wat zegt de debugger?

De MS-code lijkt goed. Niet de andere voor alle situaties denk ik.

Pls. wat meer details.
 
Ik gebruik Visual Basic 2008 Express Edition. Ik gebruik gewoon een standaard form(form1).

Hij geeft als error dat hwnd, Selstart, SelColor en SelLength geen leden(members) zijn van System.Windows.Forms.RichTextBox.

Jori.
 
Het is:

RichTextBox1.SelectionStart
RichTextBox1.SelectionColor
RichTextBox1.SelectionLength

Geen idee wat hwnd is. High Wind? :rolleyes:
 
Laatst bewerkt:
Heel erg bedankt voor je antwoord! Ik heb de code wat veranderd maar er blijven errors:

Error 1 Argument not specified for parameter 'end' of 'Public Function Find(str As String, start As Integer, end As Integer, options As System.Windows.Forms.RichTextBoxFinds) As Integer'.

Error 2 Value of type 'Long' cannot be converted to 'System.Drawing.Color'.

Error 3 Argument not specified for parameter 'end' of 'Public Function Find(str As String, start As Integer, end As Integer, options As System.Windows.Forms.RichTextBoxFinds) As Integer'.

De nieuwe code is:

Code:
Private Function HighlightWords(ByVal rtb As RichTextBox, _
                                ByVal sFindString As String, _
                                ByVal lColor As Long) _
                                As Integer

        Dim lFoundPos
        Dim lFindLength
        Dim lOriginalSelStart
        Dim lOriginalSelLength
        Dim iMatchCount

        'Save the insertion points current location and length
        lOriginalSelStart = rtb.SelectionStart
        lOriginalSelLength = rtb.SelectionLength

        'Cache the length of the string to find
        lFindLength = Len(sFindString)

        'Attempt to find the first match
        lFoundPos = rtb.Find(sFindString, 0, , rtfNoHighlight)
        While lFoundPos > 0
            iMatchCount = iMatchCount + 1

            rtb.SelectionStart = lFoundPos
            'The SelLength property is set to 0 as
            'soon as you change SelStart
            rtb.SelectionLength = lFindLength
            rtb.SelectionColor = lColor

            'Attempt to find the next match
            lFoundPos = rtb.Find(sFindString, lFoundPos + lFindLength, , rtfNoHighlight)
        End While

        'Restore the insertion point to its original
        'location and length
        rtb.SelectionStart = lOriginalSelStart
        rtb.SelectionLength = lOriginalSelLength

        'Return the number of matches
        HighlightWords = iMatchCount

    End Function
 
Laatst bewerkt:
Heel erg bedankt voor je antwoord! Ik heb de code wat veranderd maar er blijven errors:

Error 1 Argument not specified for parameter 'end' of 'Public Function Find(str As String, start As Integer, end As Integer, options As System.Windows.Forms.RichTextBoxFinds) As Integer'.

Error 2 Value of type 'Long' cannot be converted to 'System.Drawing.Color'.

Error 3 Argument not specified for parameter 'end' of 'Public Function Find(str As String, start As Integer, end As Integer, options As System.Windows.Forms.RichTextBoxFinds) As Integer'.

De nieuwe code is:

Code:
Private Function HighlightWords(ByVal rtb As RichTextBox, _
                                ByVal sFindString As String, _
                                ByVal lColor As Long) _
                                As Integer

        Dim lFoundPos
        Dim lFindLength
        Dim lOriginalSelStart
        Dim lOriginalSelLength
        Dim iMatchCount

        'Save the insertion points current location and length
        lOriginalSelStart = rtb.SelectionStart
        lOriginalSelLength = rtb.SelectionLength

        'Cache the length of the string to find
        lFindLength = Len(sFindString)

        'Attempt to find the first match
        lFoundPos = rtb.Find(sFindString, 0, , rtfNoHighlight)
        While lFoundPos > 0
            iMatchCount = iMatchCount + 1

            rtb.SelectionStart = lFoundPos
            'The SelLength property is set to 0 as
            'soon as you change SelStart
            rtb.SelectionLength = lFindLength
            rtb.SelectionColor = lColor

            'Attempt to find the next match
            lFoundPos = rtb.Find(sFindString, lFoundPos + lFindLength, , rtfNoHighlight)
        End While

        'Restore the insertion point to its original
        'location and length
        rtb.SelectionStart = lOriginalSelStart
        rtb.SelectionLength = lOriginalSelLength

        'Return the number of matches
        HighlightWords = iMatchCount

    End Function

Hi Jori,
errors 1 en 3 betekenen dat er geen of geen geldige value is ingevuld in de methode find van de richtextbox rtb.

lFoundPos = rtb.Find(sFindString, 0, , rtfNoHighlight)

voor zover ik weet mogen er geen lege parameters een functie ingestuurd worden (,, tussen 0 en rtfnohighlight.)


Dat kan de oorzaak zijn van foutmelding 2 waar in dat geval een ongeldige kleurconstante aankomt.

Ik denk dat jouw versie van VB en de versie van de richtextbox afwijkend zijn en raad aan om de properties en methodes van jouw richtextbox te vergelijken met wat in de functie gebeurt en dan de functie op jouw versie aan te passen.

ANTWOORD is aangepast omdat ik te vroeg op de verzendbutton clickte

@mighty atom: hwnd is een verwijzing naar een window in de application
 
Laatst bewerkt:
Heel erg bedankt voor je antwoord! Ik heb de code veranderd en heb geen errors meer :)
De code is nu:

Code:
    Private Function HighlightWords(ByVal rtb As RichTextBox, _
                                ByVal sFindString As String, _
                                ByVal lColor As Color) _
                                As Integer



        lOriginalSelStart = rtb.SelectionStart
        lOriginalSelLength = rtb.SelectionLength

        lFindLength = Len(sFindString)

        lFoundPos = rtb.Find(sFindString, 0, rtfNoHighlight)
        While lFoundPos > 0
            iMatchCount = iMatchCount + 1

            rtb.SelectionStart = lFoundPos
            rtb.SelectionLength = lFindLength
            rtb.SelectionColor = lColor

            lFoundPos = rtb.Find(sFindString, lFoundPos + lFindLength, rtfNoHighlight)
        End While

        rtb.SelectionStart = lOriginalSelStart
        rtb.SelectionLength = lOriginalSelLength

        HighlightWords = iMatchCount

    End Function

En:

Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

        HighlightWords(RichTextBox1, "test", vbRed)

Ens Sub

Het probleem is echter dat het gewoon niet werkt, als ik nu in mijn RichTextBox1 "test" typ dan blijft hij gewoon zwart.

Iemand een idee?
 
Hi,
voor de juiste gebruik van de find-method pls check http://msdn.microsoft.com/en-us/library/aa335476(VS.71).aspx (=vb 2008)

In de plaats van de textcolor zou je mogen besluiten de background (backcolor) een andere kleurtje te geven.

Somewhere herinner ik mij een backcolor voor de selection. Vergeet niet dat jouw applicatie debugged kan worden om alle variabelen waarden te vergelijken. BTW; in welk event laat je een jump naar de highlight function maken?



Heel erg bedankt voor je antwoord! Ik heb de code veranderd en heb geen errors meer :)
De code is nu:

Code:
    Private Function HighlightWords(ByVal rtb As RichTextBox, _
                                ByVal sFindString As String, _
                                ByVal lColor As Color) _
                                As Integer



        lOriginalSelStart = rtb.SelectionStart
        lOriginalSelLength = rtb.SelectionLength

        lFindLength = Len(sFindString)

        lFoundPos = rtb.Find(sFindString, 0, rtfNoHighlight)
        While lFoundPos > 0
            iMatchCount = iMatchCount + 1

            rtb.SelectionStart = lFoundPos
            rtb.SelectionLength = lFindLength
            rtb.SelectionColor = lColor

            lFoundPos = rtb.Find(sFindString, lFoundPos + lFindLength, rtfNoHighlight)
        End While

        rtb.SelectionStart = lOriginalSelStart
        rtb.SelectionLength = lOriginalSelLength

        HighlightWords = iMatchCount

    End Function

En:

Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

        HighlightWords(RichTextBox1, "test", vbRed)

Ens Sub

Het probleem is echter dat het gewoon niet werkt, als ik nu in mijn RichTextBox1 "test" typ dan blijft hij gewoon zwart.

Iemand een idee?
 
Bedankt voor je antwoord. Ik heb het aan de praat gekregen, maar het is niet wat ik zoek. Hij is meer bedoeld dat als je op een knop druk de code "gehighlight" word.

Dus ik heb verder gezocht op internet naar een geschiktere code. Ik vond deze:

Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

Dim words As New List(Of String)

words.Add("Select")

words.Add("Insert")

If RichTextBox1.Text.Length > 0 Then

Dim selectStart As Integer = RichTextBox1.SelectionStart

RichTextBox1.Select(0, RichTextBox1.Text.Length)

RichTextBox1.SelectionColor = Color.Black

RichTextBox1.DeselectAll()

For Each oneWord As String In words

Dim pos As Integer = 0

Do While RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0

pos = RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)

RichTextBox1.Select(pos, oneWord.Length)

RichTextBox1.SelectionColor = Color.Blue

pos += 1

Loop

Next

RichTextBox1.SelectionStart = selectStart

End If

End Sub

Deze werkt wel prima! Alleen het probleem is dat als je tekst groter wordt hij steeds heen en-weer-schiet, dat maakt het typen ondragelijk!

Heb je (of iemand anders) een idee hoe ik dat kan oplossen?
 
@city guy
Heel erg bedankt voor je post, het is gelukt!

EDIT:

Te vroeg gejuigd :(. Ik heb een nieuw probleem ontdekt. De code op dit moment voor de sytax highliting is:

Code:
        'Syntax highliting
        '=================================================================
        LockWindowUpdate(RichTextBox1.Handle)
        Dim wordsBlue As New List(Of String)
        Dim wordsGreen As New List(Of String)
        Dim wordsPurple As New List(Of String)
        wordsBlue.Add("-")
        wordsBlue.Add("=")
        wordsBlue.Add("+")
        wordsBlue.Add("&")
        wordsBlue.Add("%")
        wordsBlue.Add("\")
        wordsGreen.Add("MsgBox")
        wordsGreen.Add("InputBox")
        wordsPurple.Add("*")
        If RichTextBox1.Text.Length > 0 Then
            Dim selectStart As Integer = RichTextBox1.SelectionStart
            RichTextBox1.Select(0, RichTextBox1.Text.Length)
            RichTextBox1.SelectionColor = Color.Black
            RichTextBox1.DeselectAll()
            For Each oneWord As String In wordsBlue
                Dim pos As Integer = 0
                Do While RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
                    pos = RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
                    RichTextBox1.Select(pos, oneWord.Length)
                    RichTextBox1.SelectionColor = Color.Blue
                    pos += 1
                Loop
            Next
            For Each oneWord As String In wordsGreen
                Dim pos As Integer = 0
                Do While RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
                    pos = RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
                    RichTextBox1.Select(pos, oneWord.Length)
                    RichTextBox1.SelectionColor = Color.Green
                    pos += 1
                Loop
            Next
            For Each oneWord As String In wordsPurple
                Dim pos As Integer = 0
                Do While RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos) >= 0
                    pos = RichTextBox1.Text.ToUpper.IndexOf(oneWord.ToUpper, pos)
                    RichTextBox1.Select(pos, oneWord.Length)
                    RichTextBox1.SelectionColor = Color.Purple
                    pos += 1
                Loop
            Next
            RichTextBox1.SelectionStart = selectStart
        End If
        LockWindowUpdate(0)
        '=================================================================

Dit staat in RichTextBox1_TextChanged event.

Het probleem is dat als je achter een gekleurde letter/teken tekst wilt invoegen, er ineens allerlei rare selectie gebieden ontstaan waardoor typen (weer) ondoenlijk is geworden.

Plaatjes ter illustratie:

Ik typ MsgBox en dat wordt gehighlight

fnsj20.png


Daarna typ ik de letter "x" voor MsgBox

eiuygw.png


En dan typ ik nog een letter "x" en is de MsgBox weg.

2e4g1fl.png


Iemand een oplossing?
 
Laatst bewerkt:
Hi,

ik moet me even beperken maar geef 2 punten aan je:

- als in een blokje geselecteerde tekst getoetst wordt dan wordt de selectie opgeheven of vervangen.
- als je een toets indrukt in de rtb dan moet de selectie opgeven worden

Zelf zou ik een soort keypress event gebruiken, maar dat is uit mijn VB-6 tijd.

Jij bent in een fase dat ik zonder projectcode niet veel meer kan doen.

Vergeet vooral niet gebruik te maken van de debug mogelijkheden en het inbouwen van tussenstops om te kijken wat er in de variabelen staat.

Have to go.
Good luck
 
Bedankt voor je hulp, ik zal kijken wat ik er aan kan doen :-). Zou je ook nog even naar mijn andere vraag willen kijken als je tijd en zin hebt? Wat bedoel je trouwens met project code?

~ Jori.
 
Bedankt voor je hulp, ik zal kijken wat ik er aan kan doen :-). Zou je ook nog even naar mijn andere vraag willen kijken als je tijd en zin hebt? Wat bedoel je trouwens met project code?

~ Jori.

Hi Jori

Believe it or not, this is your line-counter

http://www.youtube.com/watch?v=p3OfstfL2F8

Meer praktisch is het VB6 project wat jou laat werken met de cursor positie. http://www.freevbcode.com/ShowCode.asp?ID=4531Bedenk ook dat de property Lines van de rtb een string - ARRAY is. die je voor allemaal bewerkingen naar een eigen array kan kopiëren.

De projectcode is alle code uit die je hebt gemaakt voor dit project. Maar ik merk dat het goed gaat, dus dat is nou niet nodig.
 
Hoi City guy,

Ik heb mijn selection probleem opgeslost :-) door:

Code:
RichTextBox1.DeselectAll()

Aan het einde van de Syntax highliting toe tevoegen. Ik geloof dat er nog maar een probleem over is bij de highliting en dat is dat hoe ondoenlijk traag wordt naarmate er meer code in komt te staan, is daar een oplossing voor?

Ik zal ook even naar de line counter kijken :-) Bedankt voor je post iniedergeval!

~ Jori.
 
Hoi City guy,

Ik heb mijn selection probleem opgeslost :-) door:

Code:
RichTextBox1.DeselectAll()

Aan het einde van de Syntax highliting toe tevoegen. Ik geloof dat er nog maar een probleem over is bij de highliting en dat is dat hoe ondoenlijk traag wordt naarmate er meer code in komt te staan, is daar een oplossing voor?

Ik zal ook even naar de line counter kijken :-) Bedankt voor je post iniedergeval!

~ Jori.

Hi Jori,
de snelheid kun jij opvoeren door te onderzoeken naar acties die onnodig steeds herhaald worden. Als je die vindt, probeer zo met arrays of switches een bereikte beginstand die nodig is voor een volgende actie vast te houden. Voorbeeld: als jij een woord zoekt in een string en dat vindt en je weet dat bij een volgende zoekactie jij altijd na dat gevonden woord gaat zoeken (haal maar even een beetje adem nou :rolleyes:).....) dan kun jij in een arraytje of andere variabele vastleggen waar je de vorige woordje gevonden hebt ( adem please) en die stand kun jij beginstand maken van je nieuwe find/zoekactie.

De VBEXPRESS interpreteert en moet iedere instructie onieuw "vertalen". In een .exe hoeft dat niet en ook dat is snelheidsverhogend.
Wil jij pls aantal VB-jaren ervaring vertellen? Thnx
 
Ongeveer 0.5 jaar. Ik ben meer thuis in VC++ en de WSH scripts (JScript, VBScript).

Ik denk trouwens niet dat de zoek opdracht eenvoudiger kan worden gemaakt, immers als je een gehighlit woord veranderd moet hij ook weer de normale text kleur krijgen.

Als iemand anderd nog ideeën heeft hoor ik dat graag.

~Jori.
 
Ongeveer 0.5 jaar. Ik ben meer thuis in VC++ en de WSH scripts (JScript, VBScript).

Ik denk trouwens niet dat de zoek opdracht eenvoudiger kan worden gemaakt, immers als je een gehighlit woord veranderd moet hij ook weer de normale text kleur krijgen.

Als iemand anderd nog ideeën heeft hoor ik dat graag.

~Jori.

Hi,
misschien kun jij iets nog met het gegeven dat de richtextbox een string-object is met een arraystructuur (lines-property). Met jouw andere ervaring zou je daaruit misschien een versnelling kunnen afleiden. Zelf zou ik daar nog even naar kijken.

BTW: mijn ervaring basic/assembler/fortran/cobol/clipper/QBasic/VB6/asp/vbscript. Ben gewoon beetje nieuwsgierig denk ik :)
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan