Click here to Skip to main content
15,888,351 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: string convert to operator Pin
dimuthuvbnet24-May-08 18:37
dimuthuvbnet24-May-08 18:37 
QuestionRegular Expressions Pin
georgegarvasis23-May-08 18:07
georgegarvasis23-May-08 18:07 
AnswerRe: Regular Expressions Pin
Garth J Lancaster24-May-08 2:39
professionalGarth J Lancaster24-May-08 2:39 
QuestionLoop question (beginner) Pin
mark19223-May-08 5:04
mark19223-May-08 5:04 
AnswerRe: Loop question (beginner) Pin
darkelv23-May-08 5:12
darkelv23-May-08 5:12 
GeneralRe: Loop question (beginner) Pin
mark19223-May-08 5:18
mark19223-May-08 5:18 
GeneralRe: Loop question (beginner) Pin
darkelv23-May-08 20:29
darkelv23-May-08 20:29 
QuestionRichTextBox, StreamReader & IndexOf problem... Please Help. Pin
infernus4223-May-08 4:30
infernus4223-May-08 4:30 
Hey developers. Smile | :)

Now before i even get to the asking, id like you to know that am a total n00b to programming, so please try to phrase your answers in the simplest form also... (in a form i like to think of as 'idiot-proof' Laugh | :laugh: )

THE CHALLENGE

Now, I'm trying to create a small program which is required to locate the occurences of any pre-determined character, word, block of characters, basically any search strings, which is entered by a user in a textbox.The search string should be highlighted in some way in the plain text file (.txt).

When a user instigates a search, all occurences of matching the string should be recorded & stored into suitable array. This array should build up information about the occurences in the text of a number of strings and at any stage in a session, a user may request a summary of all this information to display in such a way to show the frequency in the text of each search string.

I'm using a Rich Text Box control to hold the text and the IndexOf method to for the search function. Am also using the Stream reader function to import the text file and am also planning to use it to write to a separate text file.

MY SOLUTION (which is flopping a whole lot more than i expected... D'Oh! | :doh: )

Using the above controls and methods (RichTextBox, StreamReader, IndexOf)i've created a simple user interface and in it i've written the following code:


'Import missing system class libraries
Imports System.Text.RegularExpressions

Public Class frmMain

    Public Function SearchText(ByVal textToFind As String, _
    Optional ByVal startPosition As Integer = 0, Optional _
    ByVal endPosition As Integer = 0, Optional _
    ByVal highlightText As Boolean = True, Optional _
    ByVal matchCase As Boolean = False) As Integer

        '

        'Contains the return value of the search. IT it returns -1, then a match was not found.


        Dim i As Integer
        Dim LastWordinRichText As Integer
        Dim fromFirstWord As Integer
        LastWordinRichText = rtbContent.Text.LastIndexOf(textToFind)

        For fromFirstWord = 0 To LastWordinRichText
            txtIndexLocation.Text = startPosition

            If endPosition < 1 Then

                If Not matchCase Then
                    textToFind = textToFind.ToLower

                    Dim temp As String = rtbContent.Text.ToLower

                    i = temp.IndexOf(textToFind, startPosition, Me.Text.Length)
                Else
                    i = rtbContent.Text.IndexOf(textToFind, startPosition, Me.Text.Length)
                End If
            Else
                If matchCase = False Then
                    textToFind = textToFind.ToLower
                    Dim temp As String = rtbContent.Text.ToLower

                    i = temp.IndexOf(textToFind, startPosition, endPosition)

                Else
                    i = rtbContent.Text.IndexOf(textToFind, startPosition, endPosition)
                End If
            End If

            If i > -1 Then
                If highlightText Then
                    rtbContent.Focus()
                    rtbContent.SelectionStart = i
                    rtbContent.SelectionLength = textToFind.Length
                End If
                startPosition = startPosition + 1
            End If

            '
            'Returns the position where the text was found , otherwise it will report -1 which 
            'means that the search string was not found.

        Next
        Return i

    End Function

    Private Sub tsExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsExit.Click
        Me.Close()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        SearchText(txtSearch.Text)
    End Sub

    Private Sub NewSessionToolStripMenuItem_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles NewSessionToolStripMenuItem.Click

        'Open a new file dialog box  to browse for a text file which can be opened for analysis.

        'Variable declaration
        Dim Open As New OpenFileDialog()
        Dim myStreamReader As System.IO.StreamReader

        Open.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
        Open.CheckFileExists = True
        Open.ShowDialog(Me)

        Try
            Open.OpenFile()
            myStreamReader = System.IO.File.OpenText(Open.FileName)
            rtbContent.Text = myStreamReader.ReadToEnd()
            Me.Text = "Text Editor - " & Open.FileName
        Catch ex As Exception
            ' Do nothing on Exception
        End Try

    End Sub

    Private Sub SaveCurrentSessionToolStripMenuItem_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles SaveCurrentSessionToolStripMenuItem.Click

        'Open a new dialog box to save current session file.

        Dim Save As New SaveFileDialog()
        Dim myStreamWriter As System.IO.StreamWriter
        Save.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*"
        Save.CheckPathExists = True
        Save.ShowDialog(Me)
        Try
            myStreamWriter = System.IO.File.AppendText(Save.FileName)
            myStreamWriter.Write(txtSearch.Text)
            myStreamWriter.Flush()
            Me.Text = "Text Editor - " & Save.FileName
        Catch ex As Exception
            ' Do nothing on Exception
        End Try
    End Sub
End Class


MY PROBLEM
Nothing works. Frown | :( Except for the importing of text files function, nothing else works. I think its something to do with the Public SearchText function which is triggered by clicking on the search button (btnSearch). i've gone over the code again and again, but the thing won't crack.

Someone please examine the code and point me in the right direction, and if possible suggest alternative ways to do using still the same controls and methods. All help, tips, tricks and suggestions will be highly appreciated. Smile | :) And if there is a similar request which i might have missed when i was searching the threads, please give me the link to the thread.

Thanks y'all.

Steve.

You can email me on nexxus89@hotmail.com

Live it. Learn it.

AnswerRe: RichTextBox, StreamReader & IndexOf problem... Please Help. Pin
Thomas Stockwell23-May-08 10:03
professionalThomas Stockwell23-May-08 10:03 
QuestionCrystal Report Printing from Windows Service Pin
netnest23-May-08 2:44
netnest23-May-08 2:44 
AnswerRe: Crystal Report Printing from Windows Service Pin
Steven J Jowett23-May-08 3:47
Steven J Jowett23-May-08 3:47 
GeneralRe: Crystal Report Printing from Windows Service Pin
netnest23-May-08 3:56
netnest23-May-08 3:56 
GeneralRe: Crystal Report Printing from Windows Service Pin
Steven J Jowett23-May-08 4:18
Steven J Jowett23-May-08 4:18 
GeneralRe: Crystal Report Printing from Windows Service Pin
netnest23-May-08 4:26
netnest23-May-08 4:26 
GeneralRe: Crystal Report Printing from Windows Service Pin
Steven J Jowett23-May-08 4:36
Steven J Jowett23-May-08 4:36 
QuestionMultiple assemblies - System.IO.FileNotFound Exception [modified] Pin
theitmueller23-May-08 1:28
theitmueller23-May-08 1:28 
QuestionScanning/Enumeration of Registry keys Pin
sonia.sardana22-May-08 22:35
sonia.sardana22-May-08 22:35 
AnswerRe: Scanning/Enumeration of Registry keys Pin
Tony Richards22-May-08 23:45
Tony Richards22-May-08 23:45 
GeneralRe: Scanning/Enumeration of Registry keys Pin
sonia.sardana23-May-08 0:02
sonia.sardana23-May-08 0:02 
GeneralRe: Scanning/Enumeration of Registry keys Pin
Tony Richards23-May-08 0:05
Tony Richards23-May-08 0:05 
GeneralRe: Scanning/Enumeration of Registry keys Pin
sonia.sardana23-May-08 0:07
sonia.sardana23-May-08 0:07 
GeneralRe: Scanning/Enumeration of Registry keys Pin
Tony Richards23-May-08 0:18
Tony Richards23-May-08 0:18 
GeneralRe: Scanning/Enumeration of Registry keys Pin
Steven J Jowett23-May-08 3:39
Steven J Jowett23-May-08 3:39 
QuestionHow to get more 1000 records from a active directory using ado.net Pin
Chaitanya kumar CVSS22-May-08 22:05
Chaitanya kumar CVSS22-May-08 22:05 
AnswerRe: How to get more 1000 records from a active directory using ado.net Pin
jzonthemtn23-May-08 15:58
jzonthemtn23-May-08 15:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.