昨天装配脑袋在交流会上讲了2005的新功能可以友好定义错误提示,我们也可以在vs2002/3中实现(所有代码参考sharpdevelop)http://www.icsharpcode.net
1.定义一个静态函数
static void ShowErrorBox(object sender, ThreadExceptionEventArgs eargs)

{
DialogResult result = new ExceptionBox(eargs.Exception).ShowDialog();
switch (result)

{
case DialogResult.Ignore:
break;
case DialogResult.Abort:
Application.Exit();
break;
case DialogResult.Yes:
break;
}
}
2.程序启动之前:
Application.ThreadException += new ThreadExceptionEventHandler(ShowErrorBox);
3.最后是ExceptionBox的定义
// project created on 2/6/2003 at 11:10 AM
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Resources;
using System.Reflection;
using System.Drawing;
namespace Auspe.Report.Common.Gui.Dialogs


{
public class ExceptionBox : System.Windows.Forms.Form

{
private System.Windows.Forms.TextBox exceptionTextBox;
private System.Windows.Forms.CheckBox copyErrorCheckBox;
private System.Windows.Forms.CheckBox includeSysInfoCheckBox;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button continueButton;
private System.Windows.Forms.Button reportButton;
Exception exceptionThrown;
public ExceptionBox(Exception e)

{
this.exceptionThrown = e;
InitializeComponent();
exceptionTextBox.Text = e.ToString();
ResourceManager resources = new ResourceManager("BitmapResources", Assembly.GetEntryAssembly());
}
string getClipboardString()

{
string str = "";

if (includeSysInfoCheckBox.Checked)
{
str = ".NET Version : " + Environment.Version.ToString() + Environment.NewLine;
str += "OS Version : " + Environment.OSVersion.ToString() + Environment.NewLine;
str += "Boot Mode : " + SystemInformation.BootMode + Environment.NewLine;
str += "Working Set Memory : " + (Environment.WorkingSet / 1024) + "kb" + Environment.NewLine + Environment.NewLine;
Version v = Assembly.GetEntryAssembly().GetName().Version;
str += "SharpDevelop Version : " + v.Major + "." + v.Minor + "." + v.Revision + "." + v.Build + Environment.NewLine;
}
str += "Exception thrown: " + Environment.NewLine;
str += exceptionThrown.ToString();
return str;
}
void CopyInfoToClipboard()

{

if (copyErrorCheckBox.Checked)
{

try
{
Clipboard.SetDataObject(new DataObject(System.Windows.Forms.DataFormats.Text, getClipboardString()), true);

} catch (Exception)
{}
}
}
// This method is used in the forms designer.
// Change this method on you own risk
void buttonClick(object sender, System.EventArgs e)

{
CopyInfoToClipboard();
// open IE via process.start to our bug reporting forum
Process.Start("http://yourreporturl/");
}
void continueButtonClick(object sender, System.EventArgs e)

{
// CopyInfoToClipboard();
DialogResult = System.Windows.Forms.DialogResult.Ignore;
}

void InitializeComponent()
{
this.reportButton = new System.Windows.Forms.Button();
this.continueButton = new System.Windows.Forms.Button();
this.label = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.includeSysInfoCheckBox = new System.Windows.Forms.CheckBox();
this.copyErrorCheckBox = new System.Windows.Forms.CheckBox();
this.exceptionTextBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// reportButton
//
this.reportButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.reportButton.Location = new System.Drawing.Point(16, 424);
this.reportButton.Name = "reportButton";
this.reportButton.Size = new System.Drawing.Size(260, 23);
this.reportButton.TabIndex = 4;
this.reportButton.Text = "报送错误信息";
this.reportButton.Click += new System.EventHandler(this.buttonClick);
//
// continueButton
//
this.continueButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.continueButton.Location = new System.Drawing.Point(464, 424);
this.continueButton.Name = "continueButton";
this.continueButton.Size = new System.Drawing.Size(90, 23);
this.continueButton.TabIndex = 5;
this.continueButton.Text = "继续";
this.continueButton.Click += new System.EventHandler(this.continueButtonClick);
//
// label
//
this.label.Location = new System.Drawing.Point(16, 8);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(538, 48);
this.label.TabIndex = 6;
this.label.Text = "在使用过程中出现了一个意外的错误,您可以把此错误提交给我们开发组以帮助改进。";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(538, 80);
this.label2.TabIndex = 8;
this.label2.Text = "如何有效地发送错误:我们建立了一个论坛,请登陆论坛提交错误";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 152);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(538, 23);
this.label3.TabIndex = 9;
this.label3.Text = "非常感谢您支持我们的工作!";
//
// includeSysInfoCheckBox
//
this.includeSysInfoCheckBox.Checked = true;
this.includeSysInfoCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
this.includeSysInfoCheckBox.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.includeSysInfoCheckBox.Location = new System.Drawing.Point(16, 392);
this.includeSysInfoCheckBox.Name = "includeSysInfoCheckBox";
this.includeSysInfoCheckBox.Size = new System.Drawing.Size(528, 24);
this.includeSysInfoCheckBox.TabIndex = 3;
this.includeSysInfoCheckBox.Text = "包括系统信息(操作系统版本,.Net版本)";
//
// copyErrorCheckBox
//
this.copyErrorCheckBox.Checked = true;
this.copyErrorCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
this.copyErrorCheckBox.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.copyErrorCheckBox.Location = new System.Drawing.Point(16, 368);
this.copyErrorCheckBox.Name = "copyErrorCheckBox";
this.copyErrorCheckBox.Size = new System.Drawing.Size(528, 24);
this.copyErrorCheckBox.TabIndex = 2;
this.copyErrorCheckBox.Text = "将错误信息复制到剪切板";
//
// exceptionTextBox
//
this.exceptionTextBox.Location = new System.Drawing.Point(16, 176);
this.exceptionTextBox.Multiline = true;
this.exceptionTextBox.Name = "exceptionTextBox";
this.exceptionTextBox.ReadOnly = true;
this.exceptionTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.exceptionTextBox.Size = new System.Drawing.Size(538, 184);
this.exceptionTextBox.TabIndex = 1;
this.exceptionTextBox.Text = "textBoxExceptionText";
//
// ExceptionBox
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(570, 463);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label);
this.Controls.Add(this.continueButton);
this.Controls.Add(this.reportButton);
this.Controls.Add(this.includeSysInfoCheckBox);
this.Controls.Add(this.copyErrorCheckBox);
this.Controls.Add(this.exceptionTextBox);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ExceptionBox";
this.Text = "Unhandled exception has occured";
this.ResumeLayout(false);
}
}
}
1.定义一个静态函数
2.程序启动之前:
3.最后是ExceptionBox的定义
