我本无心

instinct

博客园 首页 新随笔 联系 订阅 管理
昨天装配脑袋在交流会上讲了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() 
    }

}

posted on 2005-01-02 11:00  我本无心  阅读(1971)  评论(3编辑  收藏  举报