473,609 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SystemNullRefer enceException with Richtextbox control

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

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

Similar topics

1
2573
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 other on a panel. The problem is that each control may have varying amounts of data, and so each RichTextBox (within each control) needs to size to fit the amount of text within that RichTextBox. I've tried multiplying the size of a single line...
1
2485
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 I get: System.ObjectDisposedException: Cannot access a disposed object named "RichTextBox". Object name: "RichTextBox". at System.Windows.Forms.Control.CreateHandle() at System.Windows.Forms.TextBoxBase.CreateHandle() at...
0
1656
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 that parses each line of the richTextBox and highlights specific words with a given color. I have been highlighting each of the words by using the Select method and changing the selection color beforehand. This works fine until the contents of the...
2
30520
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 writing "<asp:RichTextBox id="c1" />" in the ASPX file. The error message shows "Cannot find RichTextBox in the namespace of web controls" (something like that). Is there any way to add RichTextBox in ASPX file? Or where can I find a substitute?...
1
1678
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 value from combo box after entering of all the details i'll click on one of the command buttons then it will save the contents of the controls which i placed on the richtextbox into a file then i'll click on another command button so it will...
2
2873
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 has been found in the files. The problem is that when a search has been found that is at the very start of the file and the .select method is used to select just the first portion it ends up selecting everything in the RichTextBox control...
9
4858
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 can I print out this content to printer easily? Thanks! James
3
7169
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 just add plain (e.g.. rtf.Text += "new stuff..."; ) but some text, when added, I want to be in a different color or font size. Find() will not work because the text will vary. I have delimiters on it so I could recognize it with a regular ...
0
3433
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 available with .NET. As the email textbox can include the embedded images I want to provide this functionality in it. Now the next stuff is I want to prepare an html file from this all the
0
8139
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8555
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6064
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5524
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4032
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.