473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

SystemNullReferenceException with Richtextbox control

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)
Nov 20 '05 #1
0 2064

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: vanvee | last post by:
Hi I have a user control that contains a RichTextBox in vb.net. In my program, I create multiple instances of this control (with the RichTextBox being in each one), that appear one above the...
1
by: Nathan Carroll | last post by:
In an mdi environment I constructed a child for with a richtextbox control that is used to load .rtf's. This works fine on the intiatial load of the form but when for is closed and reopened later...
0
by: Brian Scott | last post by:
I am currently trying to control the syntax highlighting of a richtextbox control whilst the user types. At the moment I have implemented a thread which calls the richTextBox.Invoke on a method...
2
by: Neo Chou | last post by:
Greetings! I'm trying to make my ASPX page able to process WORD format document. I learned that RichTextBox might help me, but I can't use it either by adding it to my Web Control list or by...
1
by: tulasi | last post by:
I am placing textbox controls and combo box controls on richtext box and 2 command buttons on form control. after executing the application i'll place some text into textbox controls and select a...
2
by: AWesner | last post by:
I am currently working to create a VB program that will do more detailed in-file text search functions than windows has to offer. I have a form with a RichTextBox control to display the text that...
9
by: James Wong | last post by:
Hi, I use the RichTextBox in my program. It will use different language in this RichTextBox (chinese and english characters), and it set the "DualFont" and use different fonts. By the way, how...
3
by: michael sorens | last post by:
The documentation for the RichTextBox is sketchy at best. I want to do a very simple task but I cannot find information on this. I am using a RichTextBox as an output window. Some text I want to...
0
by: shivaranjaniadimulam21 | last post by:
Hello, I need to implement an email editor control in my application. The control should allow me to type in text in any format,font etc. This i have managed using the richtextbox control...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.