.net Windows窗体“;System.ArgumentException:参数无效";从Font.GetHeight(图形)

.net Windows窗体“;System.ArgumentException:参数无效";从Font.GetHeight(图形),.net,winforms,gdi+,argumentexception,.net,Winforms,Gdi+,Argumentexception,我使用dotnet 3.5和ComponentFactory Krypton v4.4.0.0支持winforms应用程序,最近我实现了AppDomain.CurrentDomain.UnhandledException和application.ThreadException处理程序来通知我客户端上发生的异常,并在日志中发现了大量错误。这一次让我头疼不已: System.ArgumentException: Parameter is not valid. at System.Drawing.F

我使用dotnet 3.5和ComponentFactory Krypton v4.4.0.0支持winforms应用程序,最近我实现了AppDomain.CurrentDomain.UnhandledException和application.ThreadException处理程序来通知我客户端上发生的异常,并在日志中发现了大量错误。这一次让我头疼不已:

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)
请注意,stacktrace完全在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+方法:

我的状态错误为Invalidparameter,如下所述:

不幸的是,这些都不能帮助我解决图形对象的问题。这是由于现场用户未处理的异常造成的。我最近有一个内存泄漏,它是由一个泄漏的EventHandler和消耗所有可用的GDI句柄引起的,但我已经修复了它,所以现在我不确定这是内存泄漏,GDI句柄泄漏,还是用户做了一些不寻常的事情而触发的错误配置


有人有什么想法吗?非常感谢您的帮助

我使用的是Krypton 4.4和.NET 4.0,也经历了同样的行为,但它处理的是KryptonComboBox。与matao一样,错误似乎根本没有通过我的代码,而是通过Krypton框架和.NET本身

在对Krypton源代码进行了一些调查并查看了抛出此错误时的堆栈跟踪之后,我注意到KryptonComboBox(或Krypton框架的一部分)连接到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
没有连接到控制管道,Krypton框架在尝试更新KryptonCombBox控件的调色板时出现了一些问题

我注意到的一个奇怪的细微差别是,在Krypton框架中抛出第一个异常之后,它似乎破坏了图形对象。后续更新调色板的调用(切换Krypton控件的颜色),错误总是通过调用堆栈中的不同区域抛出(总是从我的源代码中的某个地方启动)

Matao,我不确定这是否回答了您的问题,但我相信您可能对如何在DataGridView中连接控件存在一些问题。也许您正在将控件与DataGridView中某个对象的Tag属性相关联

我希望这能让你对你的问题有所了解

*编辑*

根据Matao的问题,我通过删除将
KryptonComboBox
添加到
ToolStripMenuItem
的标记属性的代码修复了这个问题。相反,为了获得我想要的功能,我只是在
mItem
DropDownItems
属性中添加了一些额外的
ToolStripMenuItems


我将查看您的源代码,以查找没有添加到控制管道中的任何动态创建的控件

我刚刚遇到了这个问题,花了几个小时才弄明白,我追溯到代码库的其他部分,从控件中获取对字体的引用,然后对该字体执行Dispose()。我可以通过创建一个Windows窗体项目,添加一个TreeView和一个DataGridView,并在TreeView上执行这项操作来复制它

treeView.Font.Dispose()


希望我的发现对任何遇到这个问题的人都有帮助。

不要将DataGrid视图停靠到父布局

对我来说,这是因为DefaultFont的Dispose()的缘故

如果您尝试
this.Font=新字体(dataGridViewCellStyle.Font)当调用堆栈没有通过我的代码时,这有什么帮助?我有一种感觉,其他东西正在破坏图形对象,但我不知道是什么…我遇到了同样的问题。你用什么方法解决了吗?没有,还在等着:-(如果我真的找到了解决方法,我会发布!还有,你是在使用氪星还是其他第三方控件库?呸!我真的放弃了这个,谢谢你的回答!我现在正在做另一个项目,但我会把你的回答标记为已被接受。你的理论听起来不错,很可能我在DataGridVie中犯了一个错误。)ws某处。我应该寻找什么,我已经找到的控件