"Object reference not set to an instance of an object." when selecting text on richtextbox
-
14 April 2012 16:35
Hello everyone! I have been getting some nullexceptions on the following lines of code...:
CommandFontBold.Checked = richtext.SelectionFont.Bold CommandFontItalic.Checked = richText.SelectionFont.Italic CommandFontUnderline.Checked = richText.SelectionFont.Underline CommandFontStrike.Checked = richText.SelectionFont.Strikeout************** Exception Text ************** System.NullReferenceException: Object reference not set to an instance of an object. at 78.frmDocument.UpdateSelectionCommandsState() at 87.frmDocument.TextSelectionChanged(Object sender, EventArgs e) at System.Windows.Forms.RichTextBox.OnSelectionChanged(EventArgs e) at System.Windows.Forms.RichTextBox.WmSelectionChange(SELCHANGE selChange) at System.Windows.Forms.RichTextBox.WmReflectNotify(Message& m) at System.Windows.Forms.RichTextBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Its a text editor that i am working on. When a text is bolded and i have selected it, the button for bolding text becomes checked and same goes to the other ones like italic,underline and strikeout.
but if I have selected a text with bold and italics and multiple ones, it gives me a "Object reference not set to an instance of an object." How could i solve this?
I could provide a picture if needed
Thanks
- Diedit oleh Darkside1222 14 April 2012 16:40
Semua Balasan
-
14 April 2012 16:46
May be that when you select multiple font types , there isn't any returned.
Use an IF to check for that
If Not( richtext.SelectionFont Is Nothing) Then
'''Your code
End if
Muscalu Ștefan ~~~~ Student
- Diedit oleh Muscalu Stefan Gabriel 14 April 2012 16:49
-
14 April 2012 16:51Check if the SelectionFont is Nothing? According to thehelp, it will be Nothing if more than one font applies to the selection (and I take that to mean bold and italic count as separate fonts).
Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute. -
14 April 2012 17:07
Thanks for your answer. I have tried it and so far it hasn't been giving me any errors but now one last problem, everything I select the text again, it automaticly converts it to size 13 font? This code has also been giving me object errors.CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString()
-
14 April 2012 17:15Isn't this the same problem? I.e. you need to check that the font is not nothing because it might have multiple styles?
Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute. -
14 April 2012 17:16Can you present the full code in the selection event please?
Muscalu Ștefan ~~~~ Student -
14 April 2012 17:17Yea I have fixed the object errors in the commandfontsize thing but it still oddly makes every text i select into size 13
-
14 April 2012 17:21
Can you present the full code in the selection event please?
Muscalu Ștefan ~~~~ Student
Private Sub Updee() CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString If Not (rtfText.SelectionFont Is Nothing) Then CommandFontBold.Checked = rtfText.SelectionFont.Bold() CommandFontItalic.Checked = rtfText.SelectionFont.Italic CommandFontUnderline.Checked = rtfText.SelectionFont.Underline CommandFontStrike.Checked = rtfText.SelectionFont.Strikeout End If If rtfText.SelectionFont IsNot Nothing Then CommandFont.Text = rtfText.SelectionFont.Name Else CommandFont.Text = "" End If ' CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString() CommandAlignLeft.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Left) CommandAlignRight.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Right) CommandAlignCenter.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Center) End Sub
-
14 April 2012 17:31
Can you present the full code in the selection event please?
Muscalu Ștefan ~~~~ Student
Private Sub Updee() CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString If Not (rtfText.SelectionFont Is Nothing) Then CommandFontBold.Checked = rtfText.SelectionFont.Bold() CommandFontItalic.Checked = rtfText.SelectionFont.Italic CommandFontUnderline.Checked = rtfText.SelectionFont.Underline CommandFontStrike.Checked = rtfText.SelectionFont.Strikeout End If If rtfText.SelectionFont IsNot Nothing Then CommandFont.Text = rtfText.SelectionFont.Name Else CommandFont.Text = "" End If ' CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString() CommandAlignLeft.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Left) CommandAlignRight.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Right) CommandAlignCenter.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Center) End Sub
CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString ' should also be in the If statement
Also, the first if statement and the second one are the same thing:
If Not (rtfText.SelectionFont Is Nothing) is the same thing as If rtfText.SelectionFont IsNot Nothing
There isn't any code in here that would change the selected font's properties. is there any other code in the SelectionChanged event?
Muscalu Ștefan ~~~~ Student
- Diedit oleh Muscalu Stefan Gabriel 14 April 2012 17:33
-
14 April 2012 17:37
Updee() Dim combo As ComboBoxItem = TryCast(sender, ComboBoxItem) If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing Then Dim font As New Font(rtfText.SelectionFont.FontFamily, Integer.Parse(combo.SelectedItem.ToString())) rtfText.SelectionFont = font End If CommandExecuted() End Sub
Nope there isn't any other code in the selectionchanged
-
14 April 2012 17:52
Updee() Dim combo As ComboBoxItem = TryCast(sender, ComboBoxItem) If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing Then Dim font As New Font(rtfText.SelectionFont.FontFamily, Integer.Parse(combo.SelectedItem.ToString())) rtfText.SelectionFont = font End If CommandExecuted() End Sub
Nope there isn't any other code in the selectionchanged
If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing Then ' should be
If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing AndAlso rtfText.SelectionFont IsNot Nothing Then
because you use rtfText.SelectionFont.FontFamily
I can't see the problem why it's changing automaticaly...
Muscalu Ștefan ~~~~ Student
- Diedit oleh Muscalu Stefan Gabriel 14 April 2012 18:01
-
14 April 2012 18:07Thanks :D It fixed part of the issue but im still confused because I have never set combo.selecteditem to 13 or anything else. When I start the application, it automatically chooses 8 as font size.
-
14 April 2012 18:14If it's ok with you, can you send me via http://ge.tt/ the source file to look deeper into it?
Muscalu Ștefan ~~~~ Student -
14 April 2012 18:19
Ok heres the link
removed
- Diedit oleh Darkside1222 14 April 2012 18:21
-
14 April 2012 18:41
when does the size changes automatically ?
The only possible way (in the code sent) that you can change size is at CommandFontSize_Executed
In which circumstance does [Command].Executed is raised?
I also noticed that you transform a "Command" type into a "ComboBox" at CommandFontSize_Executed. That may/will produce an error. I can't run your code because you have external library's that i don't have and when i try to run your exe, the process starts, the exits without UI.
Muscalu Ștefan ~~~~ Student
- Diedit oleh Muscalu Stefan Gabriel 14 April 2012 18:43
-
14 April 2012 18:43Should i resend you the source with the external librays then?
-
14 April 2012 18:44
Should i resend you the source with the external librays then?
Sure...
Muscalu Ștefan ~~~~ Student -
14 April 2012 18:48
I binded the commandfontsize in Private Sub BindDocumentCommands()
making combofont in the UI into commandfontsize.
If you can't get the interface loading just import the dotnetbar2.dll in the project
- Diedit oleh Darkside1222 14 April 2012 19:07
-
14 April 2012 19:11
Look's like when you select text with multiple seizes, the Richtextbox control interprets it as size 13 and that rises the font size change in CommandFontSize that actually changes the font size. :|
That is weird, because a font with different sizes should be 2 different font values and the richtextbox should return nothing, but it return's the common cont family with size 13.
This happens when text with the same font but different sizes is selected
Muscalu Ștefan ~~~~ Student
- Diedit oleh Muscalu Stefan Gabriel 14 April 2012 19:18
-
14 April 2012 19:16Oh is there a reason why the richtextbox control interprets it as 13? is there any way to fix it
-
14 April 2012 19:27
"Object reference not set to an instance of an object." when selecting text on richtextbox.....
Did you declare any Subs, variables or controls as Shared?
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
http://thesharpercoder.blogspot.com/
- Diedit oleh Rudedog2MVP 14 April 2012 19:31
-
14 April 2012 19:31
Oh is there a reason why the richtextbox control interprets it as 13? is there any way to fix it
I don't this is something that you can fix.
here's an example http://ge.tt/9CQ5aKG
Select the different fonts and you'll get a message of "Multiple selected" in the text of the form
Select the same font, different size and you'll get that font and size 13 in the text of the form
The only thing that i can think of that you can do is to put a statement to cancel the change
Add this If rtfText.SelectionFont.Size = 13 Then Exit Sub Just below If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing AndAlso rtfText.SelectionFont IsNot Nothing Then
This may be a bug in Richtextbox, you can open a new thread regarding this bug, and get help from people that know more than i do
Muscalu Ștefan ~~~~ Student
- Diedit oleh Muscalu Stefan Gabriel 14 April 2012 19:42
-
14 April 2012 19:41
No i meant is there anyway to stop the combofont from changing to size 13 even though it interprets it as 13?
In your example i selected the same fonts but diffrent sizes and they do not change to 13 and they both remain the same size
Oh btw thanks for helping me a lot :D sorry if im wasting your time or something- Diedit oleh Darkside1222 14 April 2012 19:41
-
14 April 2012 19:44
No i meant is there anyway to stop the combofont from changing to size 13 even though it interprets it as 13?
In your example i selected the same fonts but diffrent sizes and they do not change to 13 and they both remain the same size
Oh btw thanks for helping me a lot :D sorry if im wasting your time or somethingSorry, but i have no idea why. I helped you to determine the source of the problem, now is the turn of the experts to tell you why that happens.
BTW, no problem, i like to help when i can, where i can and how i can.
BTW2, your source code is safe with me.(already deleted it :D)
Muscalu Ștefan ~~~~ Student
- Diedit oleh Muscalu Stefan Gabriel 14 April 2012 19:57
-
17 April 2012 5:07
hi darkside,
How about your issue going?
It seems that you doesn't solve your issue. would you like to post the information about it?
No code, No fact.
- Diedit oleh calanghei 17 April 2012 5:09