2013-03-27 114 views
3

我支持使用dotnet 3.5和ComponentFactory氪v4.4.0.0 winforms应用程序,我最近实现了AppDomain.CurrentDomain。 UnhandledException和Application.ThreadException处理程序通知我发生在客户端上的异常,并发现日志中出现大量错误。这一个是在做我的头在此刻:从窗体窗体“System.ArgumentException:参数无效”Font.GetHeight(图形图形)

System.ArgumentException: Parameter is not valid. 
at System.Drawing.Font.GetHeight(Graphics graphics) 
at System.Drawing.Font.GetHeight() 
at System.Drawing.Font.get_Height() 
at System.Windows.Forms.Control.set_Font(Font value) 
at System.Windows.Forms.DataGridViewComboBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyledataGridViewCellStyle) 
at System.Windows.Forms.DataGridView.BeginEditInternal(Boolean selectAll) 
at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m) 
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m) 
at System.Windows.Forms.Control.WmKeyChar(Message& m) 
at System.Windows.Forms.Control.WndProc(Message& m) 
at System.Windows.Forms.DataGridView.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) 

注意堆栈跟踪完全是在Windows代码。还有一个通过我的课的一个通行证:

System.ArgumentException: Parameter is not valid. 
    at System.Drawing.Font.GetHeight(Graphics graphics) 
    at System.Drawing.Font.GetHeight() 
    at System.Drawing.Font.get_Height() 
    at System.Windows.Forms.Control.set_Font(Font value) 
    at MyOrg.MyApp.WindowsClient.GuiControls.MaskedTextBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyledataGridViewCellStyle) 
    at System.Windows.Forms.DataGridView.BeginEditInternal(Boolean selectAll) 
    at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m) 
    at System.Windows.Forms.Control.ProcessKeyMessage(Message& m) 
    at System.Windows.Forms.Control.WmKeyChar(Message& m) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.DataGridView.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) 

下面是该段代码:

public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle) 
{ 
    this.Font = dataGridViewCellStyle.Font; 
    this.ForeColor = dataGridViewCellStyle.ForeColor; 
    this.BackColor = dataGridViewCellStyle.BackColor; 
    this.TextAlign = translateAlignment(dataGridViewCellStyle.Alignment); 
} 

不告诉我了。

“System.ArgumentException:参数无效”。错误是很可悲的,给我很少去上,但使用dotPeek我看着为Font.Get_Height(图形G)的代码,并发现这是一个GDI +的错误,特别是GetFontHeight:

public float GetHeight(Graphics graphics) 
{ 
    if (graphics == null) 
    { 
    throw new ArgumentNullException("graphics"); 
    } 
    else 
    { 
    float size; 
    int fontHeight = SafeNativeMethods.Gdip.GdipGetFontHeight(new HandleRef((object) this, this.NativeFont), new HandleRef((object) graphics, graphics.NativeGraphics), out size); 
    if (fontHeight != 0) 
     throw SafeNativeMethods.Gdip.StatusException(fontHeight); 
    else 
     return size; 
    } 
} 

这是本GDI +方法: http://www.jose.it-berater.org/gdiplus/reference/flatapi/font/gdipgetfontheight.htm

,我的状态错误是无效参数,如记录在这里: http://msdn.microsoft.com/en-us/library/windows/desktop/ms534175(v=vs.85).aspx

遗憾的是没有这种帮助我解决什么错Graphics对象。这是来自该领域用户未处理的异常。我最近发生了内存泄漏,这是由于泄漏的EventHandler和使用了所有可用的GDI句柄引起的,但是我已经修复了这个问题,所以现在我不确定这是内存泄漏,GDI句柄泄漏还是坏的配置某处由用户执行一些不寻常的事情触发。

任何人有什么想法?非常感谢!

+0

如果你尝试'this.Font = new Font(dataGridViewCellStyle.Font);' – Vladimir 2013-03-27 08:12:01

+0

如果callstack没有通过我的代码,这会有什么帮助?我有这样的感觉,其他东西正在腐蚀图形对象,但我不知道是什么... – matao 2013-03-28 02:18:55

+0

我遇到了同样的问题。你在某些方面解决了吗? – 2013-11-12 15:10:24

回答

2

我正在使用氪4.4和.NET 4.0并遇到相同的行为,但它处理了KryptonComboBox。和matao一样,这个错误似乎并没有通过我的代码,而是通过Krypton框架和.NET本身。

在对氪源代码进行了一些调查并在抛出此错误时查看堆栈跟踪之后,我注意到KryptonComboBox(或氪框架的一部分)连接到Microsoft.Win32.SystemEvents.OnUserPreferenceChanged事件这让我想到了。也许这不是我的代码的原因是,这个事件是在某个时候从操作系统触发的。它仍然没有解释为什么错误被抛出,但我开始以不同的方式思考这个问题。

现在,无论何时发生此错误,它总是穿过KryptonComboBox,但它以任何方式都不一致。这实际上很难调用。但是,由于OnUserPreferenceChanged事件触发,我开始查看诸如全局策略更改或触发该事件的事情。通过一些好运,我注意到如果我的WinForms应用程序运行并启动Internet Explorer,我可以可靠地得到这个异常弹出。不要问我为什么会发生这种情况,但显然启动IE以某种方式触发OnUserPreferenceChanged事件。

现在我有一个可靠的方法来触发异常,我开始在我的代码中查看KryptonComboBox实例,并注释掉整个模块以查看是否可以返回此异常。最终,我发现了这个错误,结果是我的WinForms应用程序中的代码被连接起来时出错。具体而言,这里是错误:

KryptonComboBox detailView = new KryptonComboBox(); 
    detailView.Sorted = true; 
    detailView.Text = "View"; 
    detailView.Items.Add(new KryptonListItem("Details", "Detail View")); 
    detailView.Items.Add(new KryptonListItem("List", "List")); 
    detailView.Items.Add(new KryptonListItem("Tile", "Tiles")); 

    ToolStripMenuItem mItem = new ToolStripMenuItem(); 
    mItem.Tag = detailView; 
    mItem.Text = detailView.Text; 
    mItem.Click += new EventHandler(contextItem_Click); 

我的猜测是因为detailView未连接到控制管道,氪框架有一些问题,尝试更新KryptonCombBox控制面板。

我注意到的一个奇怪的细微差别是,在氪框架内抛出第一个异常后,它似乎破坏了Graphics对象。随后调用更新Palette(切换Krypton控件的颜色),错误总是会抛出,但是通过调用堆栈中的不同区域(始终从我的源代码中的某处启动)。

Matao,我不确定这是否会回答您的问题,但我相信您可能会在DataGridView中如何连接控件时遇到一些问题。也许你正在将一个控件与DataGridView中的Tag属性关联起来?

我希望这可以让你对你的问题有所了解。

*编辑*

每马唐的问题,我通过删除被添加KryptonComboBoxToolStripMenuItem的标签属性的代码解决了这一问题。相反,为了获得我想要的功能,我只是在mItemDropDownItems属性中添加了一些额外的ToolStripMenuItems

我会查看您的源代码中的任何动态创建的控件没有被添加到控制管道。

+0

phew!我真的放弃了这一个,谢谢对于你的回答,我现在正在研究另一个项目,但我会将你的答案标记为已接受。你的理论听起来不错,我很可能在我的DataGridView的某个地方犯了一个错误。 我应该看什么for,动态创建的控件可能不是控制管道的一部分?我的屏幕上有一个TooStripMenuItem,但不是我通常看到这个错误的那个,你是如何修复这个bug的? – matao 2014-02-10 00:41:30

+1

“我动态创建的控件可能不是控制管道的一部分?“是的。我相信这是我的问题的主要原因。 – bertoman 2014-02-10 13:48:08

3

我刚刚遇到了这个问题,并花了我几个小时弄清楚,我将其追溯到代码库的其他部分,从控件获取对字体的引用,但随后对其执行Dispose()那个字体。我能够通过创建一个Windows窗体项目,添加一个TreeView和一个DataGridView,并在TreeView上执行此操作来重现此操作。treeView.Font.Dispose();

我希望,我发现有助于任何人遇到此问题。

+0

谢谢!我现在要通过我的整个项目并尝试找到任何我们正在处理字体的地方。 嗯,在通常的设计器文件之外没有引用(我可以找到!),bugger: -/ – matao 2014-08-20 05:38:05

+0

我们在哪里使用Dispose()函数? – PurTahan 2017-07-11 11:21:21