Visual Basic 2010 - Code Aanpassen

Status
Niet open voor verdere reacties.

jyppino

Gebruiker
Lid geworden
6 sep 2009
Berichten
285
Hallo,

Ik heb 2 forms, op form 1 een richtextbox en op form 2 een textbox + button.
Het doel is dat je met form 2 tekst kunt vinden dat in form 1 staat.
Precies hetzelfde als in notepad (kladblok) de zoekfunctie.

Ik heb de volgende code voor form 2:

Code:
Public Class Find
    Dim start As Integer = 0
    Dim indexOfSearchText As Integer = 0
    Dim startindex As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text.Length > 0 Then
            startindex = FindMyText(TextBox1.Text.Trim(), start, Form1.RichTextBox1.Text.Length)
        End If

        ' If string was found in the RichTextBox, highlight it
        If startindex >= 0 Then
            ' Set the highlight color as red
            Form1.RichTextBox1.SelectionColor = Drawing.Color.Red
            ' Find the end index. End Index = number of characters in textbox
            Dim endindex As Integer = TextBox1.Text.Length
            ' Highlight the search string
            Form1.RichTextBox1.Select(startindex, endindex)
            ' mark the start position after the position of
            ' last search string
            start = startindex + endindex
        Else
            If MessageBox.Show("Couldn't find the word(s):" + " " + TextBox1.Text, _
                                                   "Results", MessageBoxButtons.OK, MessageBoxIcon.Information) = DialogResult.OK Then
            End If
        End If

    End Sub

    Public Function FindMyText(ByVal txtToSearch As String, ByVal searchStart As Integer, ByVal searchEnd As Integer) As Integer

        ' Unselect the previously searched string
        If searchStart > 0 AndAlso searchEnd > 0 AndAlso indexOfSearchText >= 0 Then
            Form1.RichTextBox1.Undo()
        End If

        ' Set the return value to -1 by default.
        Dim retVal As Integer = -1

        ' A valid starting index should be specified.
        ' if indexOfSearchText = -1, the end of search
        If searchStart >= 0 AndAlso indexOfSearchText >= 0 Then
            ' A valid ending index
            If searchEnd > searchStart OrElse searchEnd = -1 Then
                ' Find the position of search string in RichTextBox
                indexOfSearchText = Form1.RichTextBox1.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None)
                ' Determine whether the text was found in richTextBox1.
                If indexOfSearchText <> -1 Then
                    ' Return the index to the specified search text.
                    retVal = indexOfSearchText
                Else

                End If
            End If
        End If
        Return retVal
    End Function

    Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        start = 0
        indexOfSearchText = 0
    End Sub
End Class

Als je het nu gaat testen, lukt het om te zoeken naar de tekst die je intypt in textbox1 van form 2.
Nu wil ik 2 radiobuttons toevoegen (net als bij notepad) met 'Up' en 'Down'.

Hoe moet ik de code aanpassen om, als radiobuttonUP checked is, het programmaatje naar boven zoekt, en als radiobuttonDOWN checked is, het naar beneden zoekt?


Alvast bedankt.


PS. Ik heb de code niet zelf gemaakt, maar van internet gehaalt.
 
zet voor ieder ding wat je van een ader form wil hebben de naam dus
button1_click
form1.textbox1.text = "hoi"

dus als je op de button klikt dat in form1. de tekst verander in hoi
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan