printDialog1.ShowDialog() == C# 中的 DialogResult.OK 正在工作,现在它已

本文关键字:工作 DialogResult ShowDialog printDialog1 中的 OK | 更新日期: 2023-09-27 17:56:56

在我的程序中,我有以下代码片段:

private void button1_Click(object sender, EventArgs e)
    {
        printDialog1.Document = printDocument1;
        string strText = "";
        foreach (object x in listBox1.Items)
        {
            strText = strText + x.ToString() + "'n";
        }
        myReader = new StringReader(strText);
        try
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
    }

这可以打印listBox1中的所有内容。

作为更新的一部分,我在代码的另一部分更改了 SQL 服务器引用,现在本节返回"外部组件已引发错误"。

我完全不知道从这里开始,因为即使将引用更改回来仍然会引发异常。我什至尝试运行今天早上早些时候有效的先前版本,但现在我得到了异常。

完整的异常详细信息:

System.Runtime.InteropServices.SEHException was unhandled
  HResult=-2147467259
  Message=External component has thrown an exception.
  Source=System.Windows.Forms
  ErrorCode=-2147467259
  StackTrace:
       at System.Windows.Forms.UnsafeNativeMethods.PrintDlgEx(PRINTDLGEX lppdex)
       at System.Windows.Forms.PrintDialog.ShowPrintDialog(IntPtr hwndOwner, PRINTDLGEX data)
       at System.Windows.Forms.PrintDialog.RunDialog(IntPtr hwndOwner)
       at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.CommonDialog.ShowDialog()
       at McBeathTraining.GapCheck.button1_Click(Object sender, EventArgs e) in C:'Users'Benjamin.Roffey'Documents'Visual Studio 2010'Projects'McBeathTraining'McBeathTraining'GapCheck.cs:line 191
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.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(IntPtr 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.RunDialog(Form form)
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.Form.ShowDialog()
       at McBeathTraining.Form1.GapCheck_Click(Object sender, EventArgs e) in C:'Users'Benjamin.Roffey'Documents'Visual Studio 2010'Projects'McBeathTraining'McBeathTraining'Form1.cs:line 96
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.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(IntPtr 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 McBeathTraining.Program.Main() in C:'Users'Benjamin.Roffey'Documents'Visual Studio 2010'Projects'McBeathTraining'McBeathTraining'Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

printDialog1.ShowDialog() == C# 中的 DialogResult.OK 正在工作,现在它已

假设您隐藏的代码实际上没有引发异常(特别是该对话框的打印事件),则此异常通常是由于单元线程问题。您需要使线程调用Print() STA