Hello
I am trying to run this and I keep getting a "SystemNullRefe renceException: Object reference not set to instance of an object" error. I have three menu items bold, italic and underline. When i select the text in my RichTextBox, in order to format it bold, italic or underline, I get that error. The debugger points to my Private Sub UpdateMenuItems - the first line of that sub ->where it says "If rtbMain.Selecti onFont.Italic = True ".I want the menu items to display a check mark when I am selecting the text - at the same time determining whether the selected text is bold, italic or underline, the menu items should be checked. Otherwise unchecked. Is this a general error that I am experiencing? I am using VS .NET 2003 Standard. When I use error handling, the text does not format. I have a RichTextbox, OpenFileDialog, FontDialog, SaveFileDialog, ColorDialog and a main menu control added to the form. Hope the code below helps. The text file the richtextbox reads in and displays is 6 lines long. The text file (plain text) that is diplayed in my richtextbox is as follows
To: some text Arial 1
From: some text Arial 10 Bol
Subject: some text Arial 10 Bol
datadata data some dat
data data data some dat
data data data some tex
Private Sub mmuFileOpen_Cli ck(ByVal sender As Object, ByVal e As System.EventArg s) Handles mmuFileOpen.Cli c
Dim psrdCurrent As System.IO.Strea mReade
Dim pstrLine As Strin
ofdOpenFile.Fil ter = "Text Files (*.txt)|*.txt|R ich Text Files (*.rtf)|*.rtf|A ll Files (*.*)|*.*
ofdOpenFile.Fil terIndex =
ofdOpenFile.Tit le = "Open File
ofdOpenFile.Sho wDialog(
Tr
psrdCurrent = New System.IO.Strea mReader(ofdOpen File.FileName
'Read First Lin
pstrLine = psrdCurrent.Rea dLine(
If Not (pstrLine.Subst ring(0, 5) = "From:") The
Throw New System.FormatEx ceptio
End I
pstrLine = psrdCurrent.Rea dLine(
If Not (pstrLine.Subst ring(0, 3) = "To:") The
Throw New System.FormatEx ceptio
End I
pstrLine = psrdCurrent.Rea dLine(
psrdCurrent.Clo se(
If Not (pstrLine.Subst ring(0, 8) = "Subject:") The
Throw New System.FormatEx ceptio
End I
Catch ex As System.Argument Exceptio
'If user hits cancel there will be a blank file nam
'This will exit and do nothing if user hits cance
Exit Su
Catch ex As System.Exceptio
MessageBox.Show (ex.Message, "Error"
End Tr
End Su
Private Sub FormatRTFtext(
Dim psrdCurrent As System.IO.Strea mReade
Dim pstrLine As Strin
Dim pintStart As Intege
Dim pintLen As Intege
Dim pfntFont As Fon
Tr
psrdCurrent = New System.IO.Strea mReader(ofdOpen File.FileName
'Read And Format First Line FROM
pstrLine = psrdCurrent.Rea dLine(
pintLen = Len(pstrLine
rtbMain.Select( pintStart, pintLen
pfntFont = New Font("Arial", 12, FontStyle.Bold
rtbMain.Selecti onFont = pfntFon
rtbMain.Selecti onColor = Color.Blu
pintStart = pintStart + pintLen +
'Read And Format 2nd Line TO
pstrLine = psrdCurrent.Rea dLine(
pintLen = Len(pstrLine
rtbMain.Select( pintStart, pintLen
pfntFont = New Font("Arial", 10, FontStyle.Bold
rtbMain.Selecti onFont = pfntFon
pintStart = pintStart + pintLen +
'Read And Format 3rd Line SUBJECT
pstrLine = psrdCurrent.Rea dLine(
pintLen = Len(pstrLine
rtbMain.Select( pintStart, pintLen
rtbMain.Selecti onFont = pfntFon
pintStart = pintStart + pintLen +
pfntFont = New Font("Arial", 8, FontStyle.Regul ar
'Read the remaining lines and left justify them from pintStart to en
Do Until pstrLine = Nothin
pstrLine = psrdCurrent.Rea dLine
pintLen = Len(pstrLine)
rtbMain.Select( pintStart, pintLen)
rtbMain.Selecti onFont = pfntFont
rtbMain.Selecti onAlignment = HorizontalAlign ment.Left
pintStart = pintStart + pintLen + 1
Loop
'Deselect the text
rtbMain.Selecti onLength = 0
psrdCurrent.Clo se()
Catch ex As System.Exceptio n
MessageBox.Show (ex.Message, "Error")
End Try
End Sub
Private Sub ofdOpenFile_Fil eOk(ByVal sender As Object, ByVal e As System.Componen tModel.CancelEv entArgs) Handles ofdOpenFile.Fil eOk
Try
rtbMain.LoadFil e(ofdOpenFile.F ileName, RichTextBoxStre amType.PlainTex t)
mblnDirty = False
Catch ex As System.Exceptio n
MessageBox.Show (ex.Message, "Error", MessageBoxButto ns.OK, MessageBoxIcon. Information)
Finally
Call FormatRTFtext()
End Try
End Sub
Private Sub UpdateMenuItems ()
If rtbMain.Selecti onFont.Italic = True Then
mmuFormatItalic .Checked = True
Else
mmuFormatItalic .Checked = False
End If
If rtbMain.Selecti onFont.Bold = True Then
mmuFormatBold.C hecked = True
Else
mmuFormatBold.C hecked = False
End If
If rtbMain.Selecti onFont.Underlin e = True Then
mmuFormatUnderl ine.Checked = True
Else
mmuFormatUnderl ine.Checked = False
End If
End Sub
Private Sub rtbMain_Selecti onChanged(ByVal sender As Object, ByVal e As System.EventArg s) Handles rtbMain.Selecti onChanged
Call UpdateMenuItems ()
End Sub
Private Sub fdFonts_Apply(B yVal sender As Object, ByVal e As System.EventArg s) Handles fdFonts.Apply
rtbMain.Selecti onFont = fdFonts.Font
rtbMain.Selecti onColor = fdFonts.Color
UpdateMenuItems ()
End Sub
Private Sub mmuFormatBold_C lick(ByVal sender As Object, ByVal e As System.EventArg s) Handles mmuFormatBold.C lick
Dim pfntCurrent As System.Drawing. Font
Dim pintStyle As Integer
If rtbMain.Selecti onFont.Underlin e = True Then
pintStyle += System.Drawing. FontStyle.Under line
End If
If rtbMain.Selecti onFont.Italic = True Then
pintStyle += System.Drawing. FontStyle.Itali c
End If
If rtbMain.Selecti onFont.Bold = False Then
pintStyle += System.Drawing. FontStyle.Bold
mmuFormatBold.C hecked = True
Else
mmuFormatBold.C hecked = False
End If
pfntCurrent = rtbMain.Selecti onFont
rtbMain.Selecti onFont = _
New System.Drawing. Font( _
pfntCurrent.Fon tFamily, pfntCurrent.Siz e, _
CType(pintStyle , System.Drawing. FontStyle))
mmuFormatBold.C hecked = True
End Sub
Private Sub mmuFormatItalic _Click(ByVal sender As Object, ByVal e As System.EventArg s) Handles mmuFormatItalic .Click
Dim pfntcurrent As System.Drawing. Font
Dim pintStyle As Integer
If rtbMain.Selecti onFont.Underlin e = True Then
pintStyle += System.Drawing. FontStyle.Under line
End If
If rtbMain.Selecti onFont.Bold = True Then
pintStyle += System.Drawing. FontStyle.Bold
End If
If rtbMain.Selecti onFont.Italic = False Then
pintStyle += System.Drawing. FontStyle.Itali c
mmuFormatItalic .Checked = True
Else
mmuFormatItalic .Checked = False
End If
pfntcurrent = rtbMain.Selecti onFont
rtbMain.Selecti onFont = _
New System.Drawing. Font( _
pfntcurrent.Fon tFamily, pfntcurrent.Siz e, _
CType(pintStyle , System.Drawing. FontStyle))
End Sub
Private Sub mmuFormatUnderl ine_Click(ByVal sender As Object, ByVal e As System.EventArg s) Handles mmuFormatUnderl ine.Click
Dim pfntCurrent As System.Drawing. Font
Dim pintStyle As Integer
If rtbMain.Selecti onFont.Italic = True Then pintStyle += System.Drawing. FontStyle.Itali c
End If
If rtbMain.Selecti onFont.Bold = True Then pintStyle += System.Drawing. FontStyle.Bold
If rtbMain.Selecti onFont.Underlin e = False Then
pintStyle += System.Drawing. FontStyle.Under line
mmuFormatUnderl ine.Checked = True
Else: mmuFormatUnderl ine.Checked = False
End If
pfntCurrent = rtbMain.Selecti onFont
rtbMain.Selecti onFont = New System.Drawing. Font( pfntCurrent.Fon tFamily, pfntCurrent.Siz e, CType(pintStyle , System.Drawing. FontStyle))
End Sub
Private Sub mmuFileSave_Cli ck(ByVal sender As Object, ByVal e As System.EventArg s) Handles mmuFileSave.Cli ck
sfdSaveFile.Sho wDialog()
End Sub
Private Sub sfdSaveFile_Fil eOk(ByVal sender As Object, ByVal e As System.Componen tModel.CancelEv entArgs) Handles sfdSaveFile.Fil eOk
rtbMain.SaveFil e(sfdSaveFile.F ileName)