Help! Insert Text Into Rich Text Box

I can't seem to figure out how to take text read from a listbox and insert it into a richtextbox.

I thought this would work:

Dim insertTextAsString = ListBox1.SelectedItem.ToString

myPos = RichTextBox1.SelectionStart

RichTextBox1.Text.Insert(myPos, insertText)

What am I doing wrong?

Help!!!!

Thanks!

[546 byte] By [S.Rains] at [2007-12-24]
# 1
Not really knowing what you are trying to accomplish, how about something as simple as:

RichTextBox1.Text = ListBox1.SelectedItem.ToString

?

-Olice

ocertain at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Thanks for the quick response!

I should have explained better... My bad...

I am populating RichTextBox1 with what amounts to a tab-delimited text file that I am reading from my UNIX server:

Dim file As New System.IO.StreamReader(editFile)

words = file.ReadToEnd()

file.Close()

Kill(editFile)

Me.Hide()

Dim frm15 As New Form15

frm15.RichTextBox1.Text = words

frm15.TextBox9.Text = limFile

frm15.Activate()

frm15.Show()

Once that is complete (this works to this point), I want the user to have the ability to edit the text in the RichTextBox manually (which also works) or insert text at the current location within the file from the ListBox (this is where it no longer works as expected):

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim textSelected As String = "Valid"

Dim myPos As Integer

Try

Dim listOption As String = ListBox1.SelectedItem.ToString

Catch ex As Exception

textSelected = "Error"

End Try

Select Case textSelected

Case "Valid"

Dim insertText As String = ListBox1.SelectedItem.ToString

myPos = RichTextBox1.SelectionStart

RichTextBox1.Text.Insert(myPos, insertText)

ListBox1.ClearSelected()

Case "Error"

MessageBox.Show(" " & vbCrLf & "No text selected!" & vbCrLf, "Error!!!", _

MessageBoxButtons.OK, MessageBoxIcon.Error)

End Select

End Sub

I know the sub is working (or at least accessed) because my listbox selection is cleared, but nothing is actually inserted into the richtextbox... Help!?!?

S.Rains at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
Please, this code was very helpful. And I want to thank you.
However, i want to request from you to send to me how to connect to Microsoft Access Database through VB codes. I also need your help on how to write backups in Visual basic. Your contribution and help will go a long way to enhancing my abilities in VB.
Please I need your help. My email address is egoehseth@yahoo.com
Thank you very much. And I know that you will send it to me. God bless you
EgoehSethGogo at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Thanks! I'm apparently not bright enough to read

This worked:

RichTextBox1.SelectedText = insertText

Instead of:

RichTextBox1.Text.Insert(myPos, insertText)

Amazing how the obvious evades us in the time of need...

Thanks!

S.Rains at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
;)

De nada!

ocertain at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...