2

In my application I am using a textbox control called Fastcoloredtextbox, although since it inherits the textbox control, it should be the same solution to do this.

I added the ability for the user to click on a word in my application and it then opens an open file dialog, the user can then select a file and it replaces the word that was clicked with the filename. This is what I am looking to accomplish, except for one problem...It replaces every instance of the same word in the textbox with this filename. I am unsure how to only have it replace the word that was clicked though. Any help would be appreciated.

Private Sub tb_VisualMarkerClick(sender As Object, e As VisualMarkerEventArgs)
Dim page As RadPageViewPage = RadPageView1.SelectedPage
Dim txt As FastColoredTextBox = TryCast(page.Controls(0), FastColoredTextBox)
Dim ofd As New OpenFileDialog
ofd.FileName = ""
ofd.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg"
If ofd.ShowDialog = DialogResult.OK 
    Then
        Dim ClickedWord As String = (TryCast(e.Marker, RangeMarker).range.Text)
        txt.Text = txt.Text.Replace(ClickedWord, ofd.FileName)
    End If
End Sub

The clickedword string is the actual word that was clicked.

EDIT: I have came up with a solution that starts a selection where the item was clicked and selects the full word. When it is selected, text can be inserted causing it to replace the selected word. Thank you for those who offered up advice.

    Private Sub tb_VisualMarkerClick(sender As Object, e As VisualMarkerEventArgs)
    Dim page As RadPageViewPage = RadPageView1.SelectedPage
    Dim txt As FastColoredTextBox = TryCast(page.Controls(0), FastColoredTextBox)
    txt.Invalidate()
    txt.Selection.Start = New Place((TryCast(e.Marker, RangeMarker).range).Start.iChar, (TryCast(e.Marker, RangeMarker).range).Start.iLine)
    txt.SelectionLength = (TryCast(e.Marker, RangeMarker).range).Text.Length
    Dim ClickedWord As String = (TryCast(e.Marker, RangeMarker).range.Text)
    If ClickedWord = "path" Then
        Dim ofd As New OpenFileDialog
        ofd.FileName = ""
        ofd.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg"
        If ofd.ShowDialog = DialogResult.OK Then

            txt.InsertText(ofd.FileName)

        End If
    End If
End Sub
user1632018
  • 2,485
  • 10
  • 52
  • 87

1 Answers1

1

You need to use the RangeMarker to get the actual location (position in the string) of the text, as well as, the text itself.

Once you have the start position of the text you can use Substring. Something like...

Dim ClickedWord As String = (TryCast(e.Marker, RangeMarker).range.Text)
Dim StartPosition As String = (TryCast(e.Marker, RangeMarker).range.Start)
txt.Text = txt.Text.Substring(0, StartPosition) + ofd.FileName + txt.Text.Substring(StartPosition + ClickedWord.Length)

NOTE .range.Start is just a guess on my part you'll have to check the documentation of whatever the range actually represents to get the correct property name.

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62
  • 1
    relevent: http://stackoverflow.com/questions/526540/how-do-i-find-the-position-of-a-cursor-in-a-text-box-c-sharp – Amir Dec 26 '12 at 20:18