Hello
I am trying to run this and I keep getting a "SystemNullReferenceException: 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.SelectionFont.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_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mmuFileOpen.Clic
Dim psrdCurrent As System.IO.StreamReade
Dim pstrLine As Strin
ofdOpenFile.Filter = "Text Files (*.txt)|*.txt|Rich Text Files (*.rtf)|*.rtf|All Files (*.*)|*.*
ofdOpenFile.FilterIndex =
ofdOpenFile.Title = "Open File
ofdOpenFile.ShowDialog(
Tr
psrdCurrent = New System.IO.StreamReader(ofdOpenFile.FileName
'Read First Lin
pstrLine = psrdCurrent.ReadLine(
If Not (pstrLine.Substring(0, 5) = "From:") The
Throw New System.FormatExceptio
End I
pstrLine = psrdCurrent.ReadLine(
If Not (pstrLine.Substring(0, 3) = "To:") The
Throw New System.FormatExceptio
End I
pstrLine = psrdCurrent.ReadLine(
psrdCurrent.Close(
If Not (pstrLine.Substring(0, 8) = "Subject:") The
Throw New System.FormatExceptio
End I
Catch ex As System.ArgumentExceptio
'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.StreamReade
Dim pstrLine As Strin
Dim pintStart As Intege
Dim pintLen As Intege
Dim pfntFont As Fon
Tr
psrdCurrent = New System.IO.StreamReader(ofdOpenFile.FileName
'Read And Format First Line FROM
pstrLine = psrdCurrent.ReadLine(
pintLen = Len(pstrLine
rtbMain.Select(pintStart, pintLen
pfntFont = New Font("Arial", 12, FontStyle.Bold
rtbMain.SelectionFont = pfntFon
rtbMain.SelectionColor = Color.Blu
pintStart = pintStart + pintLen +
'Read And Format 2nd Line TO
pstrLine = psrdCurrent.ReadLine(
pintLen = Len(pstrLine
rtbMain.Select(pintStart, pintLen
pfntFont = New Font("Arial", 10, FontStyle.Bold
rtbMain.SelectionFont = pfntFon
pintStart = pintStart + pintLen +
'Read And Format 3rd Line SUBJECT
pstrLine = psrdCurrent.ReadLine(
pintLen = Len(pstrLine
rtbMain.Select(pintStart, pintLen
rtbMain.SelectionFont = pfntFon
pintStart = pintStart + pintLen +
pfntFont = New Font("Arial", 8, FontStyle.Regular
'Read the remaining lines and left justify them from pintStart to en
Do Until pstrLine = Nothin
pstrLine = psrdCurrent.ReadLine
pintLen = Len(pstrLine)
rtbMain.Select(pintStart, pintLen)
rtbMain.SelectionFont = pfntFont
rtbMain.SelectionAlignment = HorizontalAlignment.Left
pintStart = pintStart + pintLen + 1
Loop
'Deselect the text
rtbMain.SelectionLength = 0
psrdCurrent.Close()
Catch ex As System.Exception
MessageBox.Show(ex.Message, "Error")
End Try
End Sub
Private Sub ofdOpenFile_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdOpenFile.FileOk
Try
rtbMain.LoadFile(ofdOpenFile.FileName, RichTextBoxStreamType.PlainText)
mblnDirty = False
Catch ex As System.Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
Finally
Call FormatRTFtext()
End Try
End Sub
Private Sub UpdateMenuItems()
If rtbMain.SelectionFont.Italic = True Then
mmuFormatItalic.Checked = True
Else
mmuFormatItalic.Checked = False
End If
If rtbMain.SelectionFont.Bold = True Then
mmuFormatBold.Checked = True
Else
mmuFormatBold.Checked = False
End If
If rtbMain.SelectionFont.Underline = True Then
mmuFormatUnderline.Checked = True
Else
mmuFormatUnderline.Checked = False
End If
End Sub
Private Sub rtbMain_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rtbMain.SelectionChanged
Call UpdateMenuItems()
End Sub
Private Sub fdFonts_Apply(ByVal sender As Object, ByVal e As System.EventArgs) Handles fdFonts.Apply
rtbMain.SelectionFont = fdFonts.Font
rtbMain.SelectionColor = fdFonts.Color
UpdateMenuItems()
End Sub
Private Sub mmuFormatBold_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mmuFormatBold.Click
Dim pfntCurrent As System.Drawing.Font
Dim pintStyle As Integer
If rtbMain.SelectionFont.Underline = True Then
pintStyle += System.Drawing.FontStyle.Underline
End If
If rtbMain.SelectionFont.Italic = True Then
pintStyle += System.Drawing.FontStyle.Italic
End If
If rtbMain.SelectionFont.Bold = False Then
pintStyle += System.Drawing.FontStyle.Bold
mmuFormatBold.Checked = True
Else
mmuFormatBold.Checked = False
End If
pfntCurrent = rtbMain.SelectionFont
rtbMain.SelectionFont = _
New System.Drawing.Font( _
pfntCurrent.FontFamily, pfntCurrent.Size, _
CType(pintStyle, System.Drawing.FontStyle))
mmuFormatBold.Checked = True
End Sub
Private Sub mmuFormatItalic_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mmuFormatItalic.Click
Dim pfntcurrent As System.Drawing.Font
Dim pintStyle As Integer
If rtbMain.SelectionFont.Underline = True Then
pintStyle += System.Drawing.FontStyle.Underline
End If
If rtbMain.SelectionFont.Bold = True Then
pintStyle += System.Drawing.FontStyle.Bold
End If
If rtbMain.SelectionFont.Italic = False Then
pintStyle += System.Drawing.FontStyle.Italic
mmuFormatItalic.Checked = True
Else
mmuFormatItalic.Checked = False
End If
pfntcurrent = rtbMain.SelectionFont
rtbMain.SelectionFont = _
New System.Drawing.Font( _
pfntcurrent.FontFamily, pfntcurrent.Size, _
CType(pintStyle, System.Drawing.FontStyle))
End Sub
Private Sub mmuFormatUnderline_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mmuFormatUnderline.Click
Dim pfntCurrent As System.Drawing.Font
Dim pintStyle As Integer
If rtbMain.SelectionFont.Italic = True Then pintStyle += System.Drawing.FontStyle.Italic
End If
If rtbMain.SelectionFont.Bold = True Then pintStyle += System.Drawing.FontStyle.Bold
If rtbMain.SelectionFont.Underline = False Then
pintStyle += System.Drawing.FontStyle.Underline
mmuFormatUnderline.Checked = True
Else: mmuFormatUnderline.Checked = False
End If
pfntCurrent = rtbMain.SelectionFont
rtbMain.SelectionFont = New System.Drawing.Font( pfntCurrent.FontFamily, pfntCurrent.Size, CType(pintStyle, System.Drawing.FontStyle))
End Sub
Private Sub mmuFileSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mmuFileSave.Click
sfdSaveFile.ShowDialog()
End Sub
Private Sub sfdSaveFile_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles sfdSaveFile.FileOk
rtbMain.SaveFile(sfdSaveFile.FileName)