Hi
First of all sorry to post it in General Discussion forum as I couldn't figure out in which forum exactly my problem should be posted.
I built an application (named: TextPad Project) That has a Text Box and an Edit Menu. The code for main form is as follows:
After typing some text in the Text Box and in the Edit Menu When I click Find Option, a Dialog named Find & Replace Pops up & I enter some Word(String), and then click find, it works, when I click Replace, it replaces the word but it also gives an error message like this:
The Code for this Dialog is as follows and while showing the above error message it also highlights the code line shown in red below:
I tried few things, but couldn't solve this problem. Help me understand how can it be done correctly.
Thanx
First of all sorry to post it in General Discussion forum as I couldn't figure out in which forum exactly my problem should be posted.
I built an application (named: TextPad Project) That has a Text Box and an Edit Menu. The code for main form is as follows:
VB.NET:
Public Class TXTPADForm
Inherits System.Windows.Forms.Form
Public Shared txtBox As TextBox
Public DLG As New FindReplaceForm()
Private Sub TXTPADForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtBox = Editor
End Sub
Private Sub EditFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditFind.Click
DLG.Show()
End Sub
End Class
(The string above "What a day............................" is the text I typed in the Text Box)An unhandled exception of type 'System.InvalidCastException' occured in microsoft.visualbasic.dll
Additional information: Cast from string "What a day? can we do something" to type 'Double' is not valid.
The Code for this Dialog is as follows and while showing the above error message it also highlights the code line shown in red below:
VB.NET:
Public Class FindReplaceForm
Inherits System.Windows.Forms.Form
Private Sub bttnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnFind.Click
Dim selStart As Integer
Dim srchMode As CompareMethod
If chkCase.Checked = True Then
srchMode = CompareMethod.Binary
Else
srchMode = CompareMethod.Text
End If
selStart = InStr(TXTPADForm.txtBox.Text, searchWord.Text, srchMode)
If selStart = 0 Then
MsgBox("Can't find word")
Exit Sub
End If
TXTPADForm.txtBox.Select(selStart - 1, searchWord.Text.Length)
bttnFindNext.Enabled = True
bttnReplace.Enabled = True
bttnReplaceAll.Enabled = True
TXTPADForm.txtBox.ScrollToCaret()
End Sub
Private Sub bttnFindNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnFindNext.Click
Dim selStart As Integer
Dim srchMode As CompareMethod
If chkCase.Checked = True Then
srchMode = CompareMethod.Binary
Else
srchMode = CompareMethod.Text
End If
[COLOR=red]selStart = InStr(TXTPADForm.txtBox.selStart + 2, TXTPADForm.txtBox.Text, searchWord.Text, srchMode)[/COLOR]
If selStart = 0 Then
MsgBox("Can't find word")
Exit Sub
End If
TXTPADForm.txtBox.Select(selStart - 1, searchWord.Text.Length)
bttnFindNext.Enabled = True
bttnReplace.Enabled = True
bttnReplaceAll.Enabled = True
TXTPADForm.txtBox.ScrollToCaret()
End Sub
Private Sub bttnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnReplace.Click
If TXTPADForm.txtBox.SelectedText <> "" Then
TXTPADForm.txtBox.SelectedText = replaceWord.Text
End If
bttnFindNext_Click(sender, e)
End Sub
Private Sub bttnReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnReplaceAll.Click
Dim curPos, curSel As Integer
curPos = TXTPADForm.txtBox.SelectionStart
curSel = TXTPADForm.txtBox.SelectionLength
TXTPADForm.txtBox.Text = Replace(TXTPADForm.txtBox.Text, Trim(searchWord.Text), _
Trim(replaceWord.Text))
TXTPADForm.txtBox.SelectionStart = curPos
TXTPADForm.txtBox.SelectionLength = curSel
End Sub
End Class
Thanx
Last edited by a moderator: