Hi Professionals,

I am using Microsoft Visual Studio 2005. I would like to know how to separate content in a richtextbox(rtbTo) in windows form with a comma automatically. For example after copying email addresses from a .txt file and pasting it to the richtextbox(rtbTo), there should be a comma inserted automatically, seperating each email address. I tried to find such settings in the properties of the richtextbox but is unsuccessful.

Any help or advice will be appreciated.

Best Regards,
Md Azmil

the question is how are the email addresses are saved in the text file?
if they are seperated by newline you can just replace newline with comma while adding to the rtbTo

the question is how are the email addresses are saved in the text file?
if they are seperated by newline you can just replace newline with comma while adding to the rtbTo

Yes, there are seperated by newline. Replacing newline with comma while adding to the rtbTo makes sense. But what if i have 1000 email addresses? i can't possibly insert a comma one by one am i right?

alright how about this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sr As New StreamReader("H:\Program Files\emails.txt")
        While Not sr.EndOfStream
            rtbTo.AppendText(sr.ReadLine & ", ")
            rtbTo.ScrollToCaret()
        End While
        sr.Close()
    End Sub

alright how about this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sr As New StreamReader("H:\Program Files\emails.txt")
        While Not sr.EndOfStream
            rtbTo.AppendText(sr.ReadLine & ", ")
            rtbTo.ScrollToCaret()
        End While
        sr.Close()
    End Sub

Thank you for your time. Well, i created a button on the form and inserted the codes you've posted and there seemed to be an error on this line:-

Dim sr As New StreamReader("C:\Program Files\emails.txt")
Error message: Type 'StreamReader' is not defined

import System.IO
or change it to: IO.StreamReader

Thank you so much. Its perfectly working.

So lets say if i do not want to route it to C:\ProgramFiles\..... like in this line:-

Dim sr As New StreamReader("C:\ProgramFiles\emails.txt")

and i want it to work this way:-

-> For example, the user opens the .txt file, copies the email addresses,which is separated by newline, and paste them in the RichTextBox(rtbTo) and there should automatically be comma separating each email address.

Please advise.

add a "openfiledialog" to your Project and set the properties to your needs (extensions and so on)
then change the code a bit. something like:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Dim sr As New StreamReader(OpenFileDialog1.FileName)
        While Not sr.EndOfStream
            rtbTo.AppendText(sr.ReadLine & ", ")
            rtbTo.ScrollToCaret()
        End While
        sr.Close()
    End Sub

mark the thread as solved if you are happy with the solution ;)

add a "openfiledialog" to your Project and set the properties to your needs (extensions and so on)
then change the code a bit. something like:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Dim sr As New StreamReader(OpenFileDialog1.FileName)
        While Not sr.EndOfStream
            rtbTo.AppendText(sr.ReadLine & ", ")
            rtbTo.ScrollToCaret()
        End While
        sr.Close()
    End Sub

mark the thread as solved if you are happy with the solution ;)

Thank you so much for your time. Appreciate them alot. The codes you posted worked great.

This question has already been answered. Start a new discussion instead.