Error in Designer Generated code.

T

Tom P.

I'm getting the following error when I open a form that holds several
userControls. I'm hiding and showing the usercontrols as the user
navigates a treeview of preferences. But if I create the preferences
window more than twice I get he exception below. It happens when the
form tries to instantiate a control but it's designer-generated code
so I had nothing to do with it.

I call Dispose() on all the usercontrols, I call dispose on the
preferences form... I set any old member variables to null... I can't
think of anything else.

Any help would be appreciated, I can't figure this one out.
Tom P.


System.ArgumentException was unhandled
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Font.GetHeight(Graphics graphics)
at System.Drawing.Font.GetHeight()
at System.Drawing.Font.get_Height()
at System.Windows.Forms.Control.get_FontHeight()
at System.Windows.Forms.ComboBox.get_PreferredHeight()
at System.Windows.Forms.ComboBox.get_DefaultSize()
at System.Windows.Forms.Control..ctor(Boolean
autoInstallSyncContext)
at System.Windows.Forms.ListControl..ctor()
at System.Windows.Forms.ComboBox..ctor()
at FileTracker.userControlGeneral.InitializeComponent() in C:\
$hank\Projects\FileTracker\FileTracker\UserControls
\userControlGeneral.designer.cs:line 32
at FileTracker.userControlGeneral..ctor() in C:\$hank\Projects
\FileTracker\FileTracker\UserControls\userControlGeneral.cs:line 18
at FileTracker.FormPreferences.FormPreferences_Load(Object
sender, EventArgs e) in C:\$hank\Projects\FileTracker\FileTracker\Forms
\FormPreferences.cs:line 67
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.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.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef
hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner
(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop
(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at FileTracker.formMain.toolStripMenuItemPreferences_Click
(Object sender, EventArgs e) in C:\$hank\Projects\FileTracker
\FileTracker\FormMain.cs:line 1111
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key,
EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp
(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive
(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e,
ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp
(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.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.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW
(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner
(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop
(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at FileTracker.FileTrackerMain.Main() in C:\$hank\Projects
\FileTracker\FileTracker\Program.cs:line 22
at System.AppDomain._nExecuteAssembly(Assembly assembly, String
[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
 
P

Peter

Tom said:
I'm getting the following error when I open a form that holds several
userControls. I'm hiding and showing the usercontrols as the user
navigates a treeview of preferences. But if I create the preferences
window more than twice I get he exception below. It happens when the
form tries to instantiate a control but it's designer-generated code
so I had nothing to do with it.

I call Dispose() on all the usercontrols, I call dispose on the
preferences form... I set any old member variables to null... I can't
think of anything else.

Are you calling dispose on only objects you create?
 
J

Jesse Houwing

Hello Tom P.,

It looks like the designer has trouble parsing the value for height (possibly
font height) in one of your usercontrols.


System.ArgumentException was unhandled
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
** > at System.Drawing.Font.GetHeight(Graphics graphics)
at System.Drawing.Font.GetHeight()
at System.Drawing.Font.get_Height()
at System.Windows.Forms.Control.get_FontHeight()
at System.Windows.Forms.ComboBox.get_PreferredHeight()
at System.Windows.Forms.ComboBox.get_DefaultSize()
at System.Windows.Forms.Control..ctor(Boolean
autoInstallSyncContext)
at System.Windows.Forms.ListControl..ctor()
at System.Windows.Forms.ComboBox..ctor()
at FileTracker.userControlGeneral.InitializeComponent() in C:\
$hank\Projects\FileTracker\FileTracker\UserControls
** > \userControlGeneral.designer.cs:line 32
 
T

Tom P.

You didn't post any code.  How is anyone supposed to help you fix the  
code, if you don't post the code?

Maybe you've found a bug in .NET.  Maybe there's a bug in your own code..  
There's no way for anyone else to be able to tell the difference unless  
you provide a concise-but-complete code example that reliably reproduces  
the problem.

Pete

What code should I post? I didn't write any code, the designer did. I
can post the designer-writen code that creates the error if that
helps.
Tom P.
 
T

Tom P.

You didn't post any code.  How is anyone supposed to help you fix the  
code, if you don't post the code?

Maybe you've found a bug in .NET.  Maybe there's a bug in your own code..  
There's no way for anyone else to be able to tell the difference unless  
you provide a concise-but-complete code example that reliably reproduces  
the problem.

Pete

The Designer-generated code is as follows:

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label2 = new System.Windows.Forms.Label();
this.comboBoxRightSortColumn = new
System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.comboBoxLeftSortColumn = new
System.Windows.Forms.ComboBox();
this.checkBoxConfirmDelete = new
System.Windows.Forms.CheckBox();
this.comboBoxSizeUnit = new System.Windows.Forms.ComboBox
();
this.label3 = new System.Windows.Forms.Label();
this.textBoxPrecision = new System.Windows.Forms.TextBox
();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();

The specific line:
this.comboBoxRightSortColumn = new System.Windows.Forms.ComboBox();


Call Stack:
formPreferences.ShowDialog(this); --calls-> private void
FormPreferences_Load(object sender, EventArgs e)

_generalControl = new userControlGeneral(); --calls-> public
userControlGeneral()

InitializeComponent(); --calls-> private void InitializeComponent()

error -->this.comboBoxRightSortColumn = new
System.Windows.Forms.ComboBox();<-- error

Let me know if there's any other code that might shed some light.
Tom P.
 
T

Tom P.

Are you calling dispose on only objects you create?

Yep, I went through and checked. I'm only calling dispose on the
userControls that I create with "new".

I have a base object that has an OnClose event and a couple of buttons
(OK, Apply, and Cancel). When the user clicks one of the buttons it
calls the OnClose method and raises a Close event.

In the parent form I subscribe to the close event and dispose of all
the userControls then close the form. When I get back to the main form
I dispose of the Preferences form and set it to null.

I didn't think any of this was wrong but...

Tom P.
 
T

Tom P.

Actually it's for a standard ComboBox on one of the controls. I'm not
resetting the font or doing anything to this ComboBox in any way. It
was working fine until I added a textbox to the form.

I don't understand it.

Tom P.
 
J

Jesse Houwing

Hello Tom P.,
Actually it's for a standard ComboBox on one of the controls. I'm not
resetting the font or doing anything to this ComboBox in any way. It
was working fine until I added a textbox to the form.

I don't understand it.

Could you post the code for this usercontrol? Includign the code behind.
I guess that the contents of the InitializeComponent might suffice, but anything
you do up until the form has loaded would help.

Jesse
 
T

Tom P.

Hello Tom P.,



Could you post the code for this usercontrol? Includign the code behind.
I guess that the contents of the InitializeComponent might suffice, but anything
you do up until the form has loaded would help.

Jesse

OK, above is the Designer generated code and some call stack
information (I tried to be as complete as I could, let me know if I'm
missing something critical).

Here's the "Initialize" method I call immediatly after creating the
control:
public override void Initialize()
{

_configSettings = new Settings();

//Set the AskBeforeDelete flag
checkBoxConfirmDelete.Checked =
_configSettings.CurrentConfiguration.AskBeforeDelete;

//Set the Sort dropdowns
//Check the config file for the left side
switch
(_configSettings.CurrentConfiguration.LeftDefaultSortColumn)
{
case Utilities.Column.None:
comboBoxLeftSortColumn.Text = "(None)";
break;
case Utilities.Column.Name:
comboBoxLeftSortColumn.Text = "Name";
break;
case Utilities.Column.Size:
comboBoxLeftSortColumn.Text = "Size";
break;
case Utilities.Column.Date:
comboBoxLeftSortColumn.Text = "Date";
break;
}

//Check the config file for the left side
switch
(_configSettings.CurrentConfiguration.RightDefaultSortColumn)
{
case Utilities.Column.None:
comboBoxRightSortColumn.Text = "(None)";
break;
case Utilities.Column.Name:
comboBoxRightSortColumn.Text = "Name";
break;
case Utilities.Column.Size:
comboBoxRightSortColumn.Text = "Size";
break;
case Utilities.Column.Date:
comboBoxRightSortColumn.Text = "Date";
break;
}

switch (_configSettings.CurrentConfiguration.SizeUnit)
{
case SizeUnitType.Bytes:
comboBoxSizeUnit.SelectedItem = "bytes";
break;
case SizeUnitType.Kilobytes:
comboBoxSizeUnit.SelectedItem = "Kilobytes";
break;
case SizeUnitType.Megabytes:
comboBoxSizeUnit.SelectedItem = "Megabytes";
break;
case SizeUnitType.Gigabytes:
comboBoxSizeUnit.SelectedItem = "Gigabytes";
break;
}

//textBoxPrecision.Text =
_configSettings.CurrentConfiguration.SizePrecision.ToString();

buttonApply.Enabled = false;
buttonOK.Enabled = false;
buttonCancel.Enabled = true;

_dirtyFlag = false;

}

Just setting a bunch of flags. This shouldn't be a big deal.

I don't even know what to look at to solve this. I could zip up the
control and post it later tonight (probably can't do it from work).

I'm gonna try and use the control in an empty project and then I'm
gonna try to comment out the control and see how the preferences form
works. Maybe it has something to do with how or when it's created or
disposed.

Tom P.
 
J

Jesse Houwing

Hello Tom P.,

I read through it and apart from the hitn to have a look at Enum.ToString()
to remove a lot of these switch/cases I see nothing that could be the cause
of your problem...

Jesse
 
T

Tom P.

You should post a concise-but-complete code example that reliably  
demonstrates the problem.

It's not true that you "didn't write any code", because the default, empty  
Forms application template doesn't behave the way you describe.  And just  
because the exception occurs in the InitializeComponent() method, that  
doesn't mean that's where the bug is (in fact, it's a near-certainty that 
it's _not_ where the bug is...the chances of the bug being in your code,  
rather than in the Designer-generated code, are so incredibly high that  
IMHO it's not even worth considering the Designer-generated code as a  
likely source of the problem until you have exhausted all other  
possibilities).

Create and post a code example that is both the bare minimum amount of  
code required to reproduce the problem, and at the same time a complete,  
compilable, runnable code example.

Pete


It's getting worse - I can't seem to recreate it.

I tried copying my project and slowly removing things until I had the
least that would cause the error. But the first thing I removed, a
custom control that "should" have nothing to do with setting the
Preferences (it applies the preferences after they are set but...)
When I removed it, the error stopped.

I'm gonna have to dig. There may be resource problems. There's a lot
of fonts wizzing back and forth and I think something isn't getting
destroyed on it's trip from being set to being used. The practical
upshot is I "run out" of font resource after a while. But, like I
said, I've got some digging to do.

Thanks for the input.
Tom P.
 
T

Tom P.

The mere description "fonts wizzing back and forth" sounds problematic to 
me.  Maybe it's appropriate in your specific design, but I can say that's  
not a phrase that would apply to most correct designs.  :)

Pete

OK, this one was weird.

First, a quick description of my project and app is in order.

I'm writing a side-by-side File Manager. I've customized the ListView
and ListViewItems to support filesystem objects and calls. Because the
network at work has gotten so slow I've decided the addition of items
(files or directories) to a view will be done by a BackgroundWorker.
Also, when the user updates their preferences I update all the custom
controls to reflect the changes.

So, when a user changes their preferences it calls the FillDirectory
method on the ListView.
The FillDirectory method checks if the BackgroundWorker is busy, if so
it cancels it and waits for it to stop.
Then it clears all the items and starts the BackgroundWorker to fill
the items in the background.
Now, Because I am extending the ListViewItem I have to dispose of some
stuff I've added to it.
I figured, as long as I'm in here I may as well dispose of everything,
so I dispose of this.Font (which is inherited from the ListViewItem
object).

!! Apparently that is a bad idea !!

As soon as I found and commented out the line:

this.Font.Dispose();

....the error stopped happening. I would love to know why, but so long
as it's gone, I'm happy.

Thanks for the effort guys. This just goes to show - Don't dispose of
things you didn't create.

Tom P.
 
T

Tom P.

The mere description "fonts wizzing back and forth" sounds problematic to 
me.  Maybe it's appropriate in your specific design, but I can say that's  
not a phrase that would apply to most correct designs.  :)

Pete

OK, I found it. But you guys never would have, the problem had nothing
to do with the error I was getting.

First a little something about my application. I am writing a side-by-
side File Manager. I have extended the ListView and ListViewItem
objects to handle filesystem objects and calls. Since our network at
work is so slow I decided to use the BackgroundWorker to add items
(fiels and directories) to the ListView. When a user changes the
preferences I refresh the ListViews so the changes show up right away.

So, because I was extending the ListViewItem I had to make sure I
disposed of my additions. Well, I figured, as long as I'm here I may
as well dispose of the Font too (it is inherited).

!! Apparently this is a bad idea !!

I did as Pete suggested and kept commenting out until I found the
background worker was causing the problem. Well, I knew the error had
something to do with Fonts so I commented out the Dispose to see if
that would fix it real quick - it did. I don't know why, but that was
the issue. Hey, as long as I'm not getting that error any more, I'm
happy.

Just goes to show you, don't dispose of stuff that's not yours.

Thanks for the help,
Tom P.
 
T

Tom P.

If the designer generated code creates something then it can be relied
on to dispose of it correctly.  It will also assume that the object is
always present between the time of creation and the time of disposal.
By prematureley disposing of the font you broke that assumption, hence
the error.

rossum

The "bizarre" part to me is that by disposing of the ListViewItem.Font
by hand I caused an error in the creation of a ComboBox on a
completely different "Preferences" window. How did the font fo a
ListViewItem get over there?

I'm thinking there's some pool that I messed up when I "butted in".

Tom P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top