Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > auto delete a comma at the end of the insertion of a richtextbox
 

auto delete a comma at the end of the insertion of a richtextbox

Hi Professionals,

I am working on a mass emailing system. I have a label(lblTo), a richtextbox(rtbTo) next to the label and a browse button(btnBrowse) next to the textbox. I also included an OpenFileDialog. The requirements is that when a user click the browse button(btnBrowse), and select a .txt file which contains all the recipients' email addresses, all the email addresses will be displayed in the richtextbox(rtbTo), each seperated by commas. I have successfully been able to do that by using StreamReader.

My question is how do i make it so that there will not be a comma after the last email address because my program gives a format exception as there is a comma after the last email address.

Please advise.

Thank you.

Below are my codes for the StreamReader:-

    Private Sub ofdComma_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdComma.FileOk

        'Adding some characters in the file
        Dim sr As New StreamReader(ofdComma.FileName)

        While Not sr.EndOfStream

            rtbTo.AppendText(sr.ReadLine & ",")

            rtbTo.ScrollToCaret()

        End While

        sr.Close()

    End Sub


Below are my codes for calling the method:-

    Private Sub btnBrowseTo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseTo.Click

        'Call the OpenFileDialog()
        ofdComma.ShowDialog()

    End Sub

  • Edited byMd Azmil Friday, August 14, 2009 4:03 AM
  • Edited byMd Azmil Friday, August 14, 2009 4:02 AM
  •  
Md Azmil  Friday, August 14, 2009 3:59 AM

Its vb and c# mix , :) , Change it like

Private Sub ofdComma_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdComma.FileOk

'Adding some characters in the file
Dim sr As New StreamReader(ofdComma.FileName)

If Notsr.EndOfStream
rtbTo.AppendText(sr.ReadLine)


While Not sr.EndOfStream

rtbTo.AppendText( "," & sr.ReadLine)
rtbTo.ScrollToCaret()

End While
End If
sr.Close()

End Sub

  • Marked As Answer byMd Azmil Friday, August 14, 2009 8:40 AM
  • Edited byNareshG Friday, August 14, 2009 7:39 AMmoved end if after while
  •  
NareshG  Friday, August 14, 2009 7:32 AM

Private Sub ofdComma_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdComma.FileOk

'Adding some characters in the file
Dim sr As New StreamReader(ofdComma.FileName)

Dim
EntireFile As String

EntireFile = sr.ReadToEnd()

Dim emailIDs As String() = EntireFile.Split(New Char() {","c})

sr.Close()

End
Sub

now here emailIDs you can use.


Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
_SuDhiR_  Friday, August 14, 2009 4:24 AM
what you mean by "now here emailIDs you can use" and how do i use the emailIDs?
Md Azmil  Friday, August 14, 2009 6:12 AM
        For Each email As String In emailIDs

        Next email
  • Edited by_SuDhiR_ Friday, August 14, 2009 6:35 AM
  •  
_SuDhiR_  Friday, August 14, 2009 6:32 AM
Where should i put the For loop and how?



Md Azmil  Friday, August 14, 2009 6:39 AM
hello azmil,

if you want to remove only last comma, then you can try like

Private Sub ofdComma_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdComma.FileOk

'Adding some characters in the file
Dim sr As New StreamReader(ofdComma.FileName)

if(!sr.EndOfStream)
rtbTo.AppendText(sr.ReadLine)


While Not sr.EndOfStream

rtbTo.AppendText( "," & sr.ReadLine)
rtbTo.ScrollToCaret()

End While

sr.Close()

End Sub
NareshG  Friday, August 14, 2009 6:40 AM
hello azmil,

if you want to remove only last comma, then you can try like

Private Sub ofdComma_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdComma.FileOk

'Adding some characters in the file
Dim sr As New StreamReader(ofdComma.FileName)

if(!sr.EndOfStream)
rtbTo.AppendText(sr.ReadLine)


While Not sr.EndOfStream

rtbTo.AppendText( "," & sr.ReadLine)
rtbTo.ScrollToCaret()

End While

sr.Close()

End Sub

There should be a 'End If' somewhere am i right? and lets say i put the 'End If' after the 'End While", there is an error:-

Error Message: Leading '.' or '!' can only appear inside a 'With' statement.

Md Azmil  Friday, August 14, 2009 6:51 AM

Its vb and c# mix , :) , Change it like

Private Sub ofdComma_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdComma.FileOk

'Adding some characters in the file
Dim sr As New StreamReader(ofdComma.FileName)

If Notsr.EndOfStream
rtbTo.AppendText(sr.ReadLine)


While Not sr.EndOfStream

rtbTo.AppendText( "," & sr.ReadLine)
rtbTo.ScrollToCaret()

End While
End If
sr.Close()

End Sub

  • Marked As Answer byMd Azmil Friday, August 14, 2009 8:40 AM
  • Edited byNareshG Friday, August 14, 2009 7:39 AMmoved end if after while
  •  
NareshG  Friday, August 14, 2009 7:32 AM
That worked great. Thank you so much to all contributors. You all have been so helpful.
Md Azmil  Friday, August 14, 2009 8:40 AM

You can use google to search for other answers

Custom Search

More Threads

• Whats the best way to do this? Listbox controls datagridview contents
• Add new row to bound DataGridView - almost works, but need help
• ComboBox Items Not Showing On Dropdown
• Binding objects to DataGrid - public fieald instead of public properties
• Control.DataBindings
• Align data in drop down list
• WinForms multi-threaded databinding scenario, best practice?
• Numeric Up Down control is buggy
• Limit the column width
• Detecting if DB resources exist