Runtime error

Hey,

I am back, ok so I have a runtime error with the code in my program that somehow has to do with what I am doing, insert Table feature, I found an example on Code Project that told you how to...But When I insert the table I get this runtime error in Program.cs:

An unhandled exception of type 'System.StackOverflowException' occurred in Unknown Module.

And this code was highlighted in green:

Application.Run(newForm1());

Do you know why?

Or will you have to see the code?

Thanks :)

[633 byte] By [programmer01] at [2007-12-25]
# 1
Yes, we need to see the code or at least a part of the stack trace
MikeDanes at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

Ok, I will give you it all here:

Add Table Click event:

privatevoid tableToolStripMenuItem_Click(object sender, EventArgs e)

{

AddTable newtable = newAddTable();

if (newtable.ShowDialog() == DialogResult.OK)

{

RtextboxUC thisrichtextbox = GetCurrentTextBox();

int columns = int.Parse(GlobalVars.thecolumnsize);

int rows = int.Parse(GlobalVars.therowsize);

int width = (int)pageSetupDialog.PageSettings.PrintableArea.Width;

int margin = pageSetupDialog.PageSettings.Margins.Left + pageSetupDialog.PageSettings.Margins.Right;

thisrichtextbox.InsertTheTable(columns, rows, width - margin);

}

}

2 of those variables are set here:

publicclassGlobalVars

{

publicstaticint addthenewtab;

publicstaticstring tabtitle;

publicstaticstring thecolumnsize;

publicstaticstring therowsize;

}

and then inside the user control for the rich textbox I added:

publicInt32 HundredthInchToTwips(int n)

{

return (Int32)(n * 14.4);

}

publicvoid InsertTheTable(int Column, int row, int pwidth)

{

int width = HundredthInchToTwips(pwidth);

string border = "\\brdrs\\brdrw15";

StringBuilder tablestring = newStringBuilder();

tablestring.Append("\\trowd\\trgraph70\\trleft170");

tablestring.Append("\\trbrdrt" + border);

tablestring.Append("\\trbrdrl" + border);

tablestring.Append("\\trbrdrb" + border);

tablestring.Append("\\trbrdrr" + border);

for (int kolom = 0; kolom < Column; kolom++)

{

tablestring.Append("\\clbrdrt" + border);

tablestring.Append("\\clbrdrl" + border);

tablestring.Append("\\clbrdrb" + border);

tablestring.Append("\\clbrdrr" + border);

int breedte = (width / Column) * (kolom + 1);

tablestring.Append("\\cellx");

tablestring.Append(breedte.ToString());

}

tablestring.Append("\\pard");

for (int rij = 0; rij < row; rij++)

{

tablestring.Append("\\intbl");

for (int kolom = 0; kolom < row; kolom++)

{

tablestring.Append("\\cell");

}

tablestring.Append("\\row");

}

tablestring.Append("\\pard\\par}");

InsertTextAsRtf(tablestring.ToString());

}

and here is the code in the form called AddTable:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Notebook_Pro

{

publicpartialclassAddTable : Form

{

public AddTable()

{

InitializeComponent();

Location = ActiveForm.Location + SystemInformation.CaptionButtonSize + SystemInformation.FrameBorderSize;

}

privatevoid button1_Click(object sender, EventArgs e)

{

DialogResult = DialogResult.Cancel;

this.Close();

}

privatevoid timer1_Tick(object sender, EventArgs e)

{

GlobalVars.thecolumnsize = textBox1.Text.ToString();

GlobalVars.therowsize = textBox2.Text.ToString();

}

privatevoid button2_Click(object sender, EventArgs e)

{

if (textBox1.Text != "" && textBox2.Text != "")

{

DialogResult = DialogResult.OK;

}

}

}

}

and....I think..thats all...

Thanks :)

programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

Hmm... I don't see any reason for this code would throw a StackOverflowException. I tried to get it work in a test project but all I get is "File format is not valid" when inserting the RTF. By the way how does InsertTextAsRtf looks like ?

And on a separate note: why are you using a timer to transfer the value from textboxes into the variables. You can do it just in button2_Click. After all you don't want to modify something if the user did not press the OK button.

MikeDanes at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

I agree with Mike.

I was looking at the code much earlier too and nothing looks as if it should throw that exception. I guess it's best to debug each line in this case and seeing where the problem hits. StackOverFlow exception can also occur if a property is being called recursively on itself, but this is not the only reason for the exception. There could be 1001 reasons why and the best thing would be to hit a breakpoint just before it hits the error....this would be where you have to figure out after what series of actions you do that makes the error come up, so place a debug point there and step into the debugger seeing what's going on and what's causing it

ahmedilyas at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5

ok, I will do that, I also took out the timer and put the code int he ok button so now the ok button code is:

private void button2_Click(object sender, EventArgs e)

{

if (textBox1.Text != "" && textBox2.Text != "")

{

GlobalVars.thecolumnsize = textBox1.Text.ToString();

GlobalVars.therowsize = textBox2.Text.ToString();

DialogResult = DialogResult.OK;

}

}

so, ok, I will try the debug thing...

ok, becuase this is my first time doing this, I dont know if this is the line of code but I debuged it and when I pressed the Add Table option it made my first line of code that has to do with this in the Add Table form:

Location = ActiveForm.Location + SystemInformation.CaptionButtonSize + SystemInformation.FrameBorderSize;

highlighted in yellow, does that mean there is somthin wrong with it? If so I will tell you the info it gave me about it...

Thanks :)

programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6

well, will it do that runtime error thing if I find the code that is doing this? Or is there somthing specific that it will say?

Thanks :)


EDIT: I found it!

ok this is the part causing the runtime error, I was the variable

here thye are highlighted in red:

public class GlobalVars

{

public static int addthenewtab;

public static string tabtitle;

public static string thecolumnsize;

public static string therowsize;

}

Aparently it is the way I am setting the variable...what is a better way to do this then?

Thanks :)

Edit: in the example they dont use a variable but they use a way I cant get to work, mabee you could tell me how? this how it does it:

int kolommen = int.Parse(dlg.textBoxKolommen.Text.ToString());

dlg is the variable for opening the dialog for setting it's size:

Add_Table dlg = new Add_Table();

but in my program it is this:

private void tableToolStripMenuItem_Click(object sender, EventArgs e)

{

AddTable newtable = new AddTable();

if (newtable.ShowDialog() == DialogResult.OK)

{

RtextboxUC thisrichtextbox = GetCurrentTextBox();

int columns = int.Parse(GlobalVars.thecolumnsize);

int rows = int.Parse(GlobalVars.therowsize);

int width = (int)pageSetupDialog.PageSettings.PrintableArea.Width;

int margin = pageSetupDialog.PageSettings.Margins.Left + pageSetupDialog.PageSettings.Margins.Right;

thisrichtextbox.InsertTheTable(columns, rows, width - margin);

}

}

and in the other it is this:

private void tableToolStripMenuItem_Click(object sender, EventArgs e)

{

Add_Table dlg = new Add_Table();

if (dlg.ShowDialog() == DialogResult.OK)

{

int index = ZoekTab();

int kolommen = int.Parse(dlg.textBoxKolommen.Text.ToString());

int rijen = int.Parse(dlg.textBoxRijen.Text.ToString());

int width = (int)pageSetupDialog.PageSettings.PrintableArea.Width;

int margin = pageSetupDialog.PageSettings.Margins.Left + pageSetupDialog.PageSettings.Margins.Right;

richTextBox[index].InsertTable(kolommen, rijen, width - margin);

}

}

but if I try to do this: newtable.textbox1.text.ToString()

the textbox1 does not show up in the intellisence list......

programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7

I don't understand how using those variables can cause a StackOverflow... it must be something else.

Anyway, I consider both using those "global variables" and the example above to be bad programming practice.

For example think about what happens if the user introduces some text that is not a number. In both cases you Parse the text after the dialog has been closed. Normally you need to catch the exceptions from the Parse method and tell the user that the input value is not correct (for example by using a MessageBox). But this must be done in the form itself because if you do it after ShowDialog then the form won't be visible anymore and the user will be confused why is getting an error message AFTER the dialog has been closed.

So a better idea would be to do something like this:

publicpartialclassAddTable : Form

{

private int columnSize;

private int rowSize;

....

privatevoid button2_Click(object sender, EventArgs e)

{

try {

columnSize = int.Parse(textbox1.Text);

}

catch (FormatException ex) {

MessageBox(this, "Column size must be a number", "Add Table", MessageBoxButtons.OK, MessageBoxIcon.Error);

return;

}

try {

rowSize = int.Parse(textbox2.Text);

}

catch (FormatException ex) {

MessageBox(this, "Row size must be a number", "Add Table", MessageBoxButtons.OK, MessageBoxIcon.Error);

return;

}

DialogResult = DialogResult.OK;

}

// Public properties to access the column size and row size. After ShowDialog you can get the column size and row size directly from here without using the GlobalVars class.

public int ColumnSize {

get {

return columnSize;

}

}

public int RowSize {

get {

return rowSize;

}

}

}

MikeDanes at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8

ok but what about this part:

privatevoid tableToolStripMenuItem_Click(object sender, EventArgs e)

{

AddTable newtable = newAddTable();

if (newtable.ShowDialog() == DialogResult.OK)

{

RtextboxUC thisrichtextbox = GetCurrentTextBox();

int columns = int.Parse(newtable.);

int rows = int.Parse(GlobalVars.therowsize);

int width = (int)pageSetupDialog.PageSettings.PrintableArea.Width;

int margin = pageSetupDialog.PageSettings.Margins.Left + pageSetupDialog.PageSettings.Margins.Right;

thisrichtextbox.InsertTheTable(columns, rows, width - margin);

}

}

what should that be like?

programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9

Simple :

privatevoid tableToolStripMenuItem_Click(object sender, EventArgs e)

{

AddTable newtable = newAddTable();

if (newtable.ShowDialog() == DialogResult.OK)

{

RtextboxUC thisrichtextbox = GetCurrentTextBox();

int width = (int)pageSetupDialog.PageSettings.PrintableArea.Width;

int margin = pageSetupDialog.PageSettings.Margins.Left + pageSetupDialog.PageSettings.Margins.Right;

thisrichtextbox.InsertTheTable(newTable.ColumnSize, newTable.RowSize, width - margin);

}

}

MikeDanes at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 10

k no more runtime errors ont hat, but now there is a new one: An unhandled exception of type 'System.StackOverflowException' occurred in Notebook Pro.exe

on this code:

InsertTextAsRtf(_text);

in:

publicvoid InsertTextAsRtf(string _text)

{

InsertTextAsRtf(_text);

}

and that is in the RtextboxUC user control("custom" richtextbox") here is all the code in the user control:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.Drawing.Printing;

using System.Collections.Specialized;

using System.IO;

using System.Drawing.Imaging;

namespace Notebook_Pro

{

publicpartialclassRtextboxUC : UserControl

{

public RtextboxUC()

{

InitializeComponent();

}

//This property is used to give you access to the RichTextBox

//Behind this control so you can load files, change styles ... etc.

publicRichTextBox TextBox { get { return richTextBox1; } }

protectedoverridevoid OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

Graphics g = e.Graphics;

Rectangle r = newRectangle(richTextBox1.Bounds.Location, richTextBox1.Bounds.Size);

r.Offset(5, 5);

g.FillRectangle(SystemBrushes.ButtonShadow, r);

//The following two lines are optional if you want a border,

//be warned though:

//it causes strange behaviour when the control is resized!

Rectangle d = newRectangle(richTextBox1.Bounds.X - 1, richTextBox1.Bounds.Y - 1, richTextBox1.Bounds.Width + 1, richTextBox1.Bounds.Height + 1);

g.DrawRectangle(Pens.Black, d);

}

protectedoverridevoid OnSizeChanged(EventArgs e)

{

Refresh();

base.OnSizeChanged(e);

}

privatevoid RtextboxUC_Load(object sender, EventArgs e)

{

}

publicInt32 HundredthInchToTwips(int n)

{

return (Int32)(n * 14.4);

}

publicvoid InsertTheTable(int Column, int row, int pwidth)

{

int width = HundredthInchToTwips(pwidth);

string border = "\\brdrs\\brdrw15";

StringBuilder tablestring = newStringBuilder();

tablestring.Append("\\trowd\\trgraph70\\trleft170");

tablestring.Append("\\trbrdrt" + border);

tablestring.Append("\\trbrdrl" + border);

tablestring.Append("\\trbrdrb" + border);

tablestring.Append("\\trbrdrr" + border);

for (int kolom = 0; kolom < Column; kolom++)

{

tablestring.Append("\\clbrdrt" + border);

tablestring.Append("\\clbrdrl" + border);

tablestring.Append("\\clbrdrb" + border);

tablestring.Append("\\clbrdrr" + border);

int breedte = (width / Column) * (kolom + 1);

tablestring.Append("\\cellx");

tablestring.Append(breedte.ToString());

}

tablestring.Append("\\pard");

for (int rij = 0; rij < row; rij++)

{

tablestring.Append("\\intbl");

for (int kolom = 0; kolom < row; kolom++)

{

tablestring.Append("\\cell");

}

tablestring.Append("\\row");

}

tablestring.Append("\\pard\\par}");

InsertTextAsRtf(tablestring.ToString());

}

publicvoid InsertTextAsRtf(string _text)

{

InsertTextAsRtf(_text);

}

}

}

thanks :)

programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 11

Now it's clear !

The InsertTextAsRtf function calls itself:

publicvoid InsertTextAsRtf(string _text)

{

InsertTextAsRtf(_text);

}

Why are you doing what ? It makes no sense to call InsertTextAsRtf function from itself.

Did you intend to call a function with the same name from another class perhaps ?

MikeDanes at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 12
I dont know what it does...but I know that I commented it all out and there was no runtime error after that but nothing happens when I Make a table...
programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...