Bugzilla – Bug 323679
NullReferenceExeption inside RichTextBox
Last modified: 2007-09-15 21:24:06 UTC
---- Reported by linux.sarge@gmail.com 2007-03-01 14:50:42 MST ---- Please fill in this template when reporting a bug, unless you know what you are doing. Description of Problem: First: Sorry for my bad english... Here is a screenshot of the bug: http://cyberstudio.110mb.com//MonoBugs/ScreenHunter_1.jpg Ok, Lets gonna say that you need to scan a RichTextBox Text property in search of every "A" in the text. When you find it you need to put it in Bold, Underline and Blue color. My approach runs well under .net 1.1 and 2.0, But mono fails. Here is a small console program that reproduces de bug. just compile it with references to: *System *System.Windows.Forms *System.Drawing Ok, here is the program: ======================================== using System; using System.Text; using System.Windows.Forms; using System.Drawing; namespace MonoBugC { class Program { static void Main(string[] args) { Console.WriteLine(); Console.WriteLine("------------------------------------------"); Console.WriteLine("Press any key to start the bug"); Console.WriteLine("------------------------------------------"); Console.WriteLine(); //Create the objet and set his properties RichTextBox myRtb = null; myRtb = new RichTextBox(); myRtb.Font = new Font("Tahoma", 10, FontStyle.Regular); myRtb.Text = " THIS IS A TEXT TO SHOW A BUG IN RICHTEXTBOX IN MONO"; //Scan the text and put in Bold, Underline and Blue every "A" in the text for (int i = 0; i < myRtb.Text.Length; i++) { if (myRtb.Text[i].ToString()=="A") { myRtb.SelectionStart = i; myRtb.SelectionLength = 1; FontStyle newFontStyle; newFontStyle = FontStyle.Bold; myRtb.SelectionFont = new Font("Tahoma", 10, newFontStyle); myRtb.SelectionColor = Color.Blue; myRtb.SelectionFont = new Font(myRtb.SelectionFont, myRtb.SelectionFont.Style ^ FontStyle.Underline); } } Console.WriteLine("Test passed :D, There is no bug!. Maybe you are running this program under\n"+ "Microsoft .net Framework, or under mono version with the bug already fixed!"); Console.ReadLine(); } } } Actual Results: NullReferenceExeption Expected Results: I expect it to do it right How often does this happen? always Additional Information: Also tested under linux, the problem is the same. Sorry for my bad english. ---- Additional Comments From linux.sarge@gmail.com 2007-03-01 14:57:06 MST ---- Created an attachment (id=171573) TestCase, Source code of the TestCase, Screenshot of the testcase running over .net 1.1, 2.0 and mono ---- Additional Comments From linux.sarge@gmail.com 2007-03-01 14:59:22 MST ---- Sorry, Im new reporting bugs: 1)The attachment is a .zip file 2)The screenshot is located here: http://cyberstudio.110mb.com/MonoBugs/ScreenHunter_1.jpg I hope all this info helps :) ---- Additional Comments From georgegiolfan@yahoo.com 2007-03-06 07:14:04 MST ---- Created an attachment (id=171574) This seems to fix it. ---- Additional Comments From linux.sarge@gmail.com 2007-03-06 08:58:10 MST ---- Thanks! Will this fix be included in the next release? This bug made some of my programs incompatible with mono. ---- Additional Comments From jackson@ximian.com 2007-03-18 21:02:54 MST ---- This bug is now fixed in SVN. Thanks for the report. ---- Additional Comments From georgegiolfan@yahoo.com 2007-03-18 22:44:01 MST ---- Since I don't completely understand the code in TextControl.cs, TextBoxBase.cs, etc., I may be wrong. While the bug is technically fixed (no more NullReferenceExeption), the fix avoids the real problem. This has nothing to do with the selection length. The problem is that, in the SelectionFont getter, document.selection_start.tag has a wrong value. This is caused by the fact that, in the SelectionFont setter, the same logic is used to set both document.selection_start.tag and document.selection_end.tag. This is wrong to be because when the selection starts between tags the first tag in the selection is the one after the selection start (as opposed to the last tag in the selection, which would be the one before the selection end, if the selection ends between tags). So my previous patch works only in this case. Here is test (if you want to have a NullReferenceException, change SelectionLength = 1 to SelectionLength = 2). using System.Diagnostics; using System.Drawing; using System.Windows.Forms; class Test { static void Main() { RichTextBox t = new RichTextBox(); t.Text = "123"; t.SelectionStart = 1; t.SelectionLength = 1; Font f = new Font(FontFamily.GenericMonospace, 123); t.SelectionFont = f; Debug.Assert(t.SelectionFont.Size == f.Size); } } ---- Additional Comments From georgegiolfan@yahoo.com 2007-03-18 22:45:07 MST ---- Created an attachment (id=171575) Proposed patch ---- Additional Comments From jackson@ximian.com 2007-03-26 17:20:18 MST ---- OK, there is a more thorough fix in SVN now. ---- Additional Comments From gert.driesen@pandora.be 2007-03-30 12:24:12 MST ---- The A characters are indeed bold now, but they are not blue or underlined. Should I open a separate bug report for that ? ---- Additional Comments From jackson@ximian.com 2007-04-02 14:39:16 MST ---- Gert, thanks for pointing that out. I've fixed it in SVN. ---- Additional Comments From jackson@ximian.com 2007-04-02 14:39:29 MST ---- fixed. ---- Additional Comments From gert.driesen@pandora.be 2007-04-02 16:49:12 MST ---- I now get a NRE running my repro for this issue: System.NullReferenceException: Object reference not set to an instance of an object at System.Windows.Forms.LineTag.FormatText (System.Windows.Forms.Line line, Int32 start, Int32 length, System.Drawing.Font font, System.Drawing.SolidBrush color, System.Drawing.SolidBrush back_color, FormatSpecified specified) [0x00000] at System.Windows.Forms.Document.FormatText (System.Windows.Forms.Line start_line, Int32 start_pos, System.Windows.Forms.Line end_line, Int32 end_pos, System.Drawing.Font font, System.Drawing.SolidBrush color, System.Drawing.SolidBrush back_color, FormatSpecified specified) [0x00000] at System.Windows.Forms.RichTextBox.set_SelectionColor (Color value) [0x00000] at (wrapper remoting-invoke-with-check) System.Windows.Forms.RichTextBox:set_SelectionColor (System.Drawing.Color) at MainForm.MainForm_Load (System.Object sender, System.EventArgs e) [0x00000] at (wrapper delegate-invoke) System.MulticastDelegate:invoke_void_object_EventArgs (object,System.EventArgs) at System.Windows.Forms.Form.OnLoad (System.EventArgs e) [0x00000] at System.Windows.Forms.Form.OnCreateControl () [0x00000] at System.Windows.Forms.Control.CreateControl () [0x00000] at System.Windows.Forms.Control.WmShowWindow (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.ScrollableControl.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.ContainerControl.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Form.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] ---- Additional Comments From gert.driesen@pandora.be 2007-04-02 17:06:41 MST ---- Fixed by Jackson in r75297. Imported an attachment (id=171573) Imported an attachment (id=171574) Imported an attachment (id=171575) Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>Service Pack 2</cf_op_sys_details>