<pre> ExceptionDialog.ShowBugWindowOnError()</pre>
<pre class="alt"> <span class="kwrd">End</span> Sub</pre>
<pre><span class="kwrd">Imports</span> System.Threading</pre>
<pre class="alt"><span class="kwrd">Imports</span> System.Text</pre>
<pre> </pre>
<pre class="alt"><span class="kwrd">Public</span> <span class="kwrd">Class</span> ExceptionDialog</pre>
<pre> </pre>
<pre class="alt"><span class="preproc">#Region</span> <span class="str">"Const"</span></pre>
<pre> <span class="kwrd">Const</span> OpenDetailButtonText <span class="kwrd">As</span> <span class="kwrd">String</span> = <span class="str">"v 詳細資料"</span></pre>
<pre class="alt"> <span class="kwrd">Const</span> CloseDetailButtonText <span class="kwrd">As</span> <span class="kwrd">String</span> = <span class="str">"^ 詳細資料"</span></pre>
<pre><span class="preproc">#End Region</span></pre>
<pre class="alt"> </pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre><span class="preproc">#Region</span> <span class="str">"Var"</span></pre>
<pre class="alt"> <span class="kwrd">Private</span> _isDetailOpened <span class="kwrd">As</span> <span class="kwrd">Boolean</span></pre>
<pre><span class="preproc">#End Region</span></pre>
<pre class="alt"> </pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre><span class="preproc">#Region</span> <span class="str">"Public Shared Method"</span></pre>
<pre class="alt"> </pre>
<pre> <span class="rem">'***************************************************************************</span></pre>
<pre class="alt"> <span class="rem">'Author: Larry Nung</span></pre>
<pre> <span class="rem">'Date: 2009/4/9</span></pre>
<pre class="alt"> <span class="rem">'Purpose: </span></pre>
<pre> <span class="rem">'Memo: </span></pre>
<pre class="alt"> <span class="rem">'***************************************************************************</span></pre>
<pre> <span class="rem">''' <summary></span></pre>
<pre class="alt"> <span class="rem">''' Shows the bug window on error.</span></pre>
<pre> <span class="rem">''' </summary></span></pre>
<pre class="alt"> <span class="rem">''' <remarks></remarks></span></pre>
<pre> <span class="kwrd">Public</span> <span class="kwrd">Shared</span> <span class="kwrd">Sub</span> ShowBugWindowOnError()</pre>
<pre class="alt"> <span class="kwrd">AddHandler</span> Application.ThreadException, <span class="kwrd">AddressOf</span> OnErrorOccur</pre>
<pre> <span class="kwrd">End</span> <span class="kwrd">Sub</span></pre>
<pre class="alt"><span class="preproc">#End Region</span></pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre><span class="preproc">#Region</span> <span class="str">"Protected Shared Method"</span></pre>
<pre class="alt"> </pre>
<pre> <span class="rem">'***************************************************************************</span></pre>
<pre class="alt"> <span class="rem">'Author: Larry Nung</span></pre>
<pre> <span class="rem">'Date: 2009/4/9</span></pre>
<pre class="alt"> <span class="rem">'Purpose: </span></pre>
<pre> <span class="rem">'Memo: </span></pre>
<pre class="alt"> <span class="rem">'***************************************************************************</span></pre>
<pre> <span class="rem">''' <summary></span></pre>
<pre class="alt"> <span class="rem">''' Called when [error occur].</span></pre>
<pre> <span class="rem">''' </summary></span></pre>
<pre class="alt"> <span class="rem">''' <param name="sender">The sender.</param></span></pre>
<pre> <span class="rem">''' <param name="e">The <see cref="System.Threading.ThreadExceptionEventArgs" /> instance containing the event data.</param></span></pre>
<pre class="alt"> <span class="rem">''' <remarks></remarks></span></pre>
<pre> <span class="kwrd">Protected</span> <span class="kwrd">Shared</span> <span class="kwrd">Sub</span> OnErrorOccur(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> <span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> ThreadExceptionEventArgs)</pre>
<pre class="alt"> <span class="kwrd">Dim</span> errorDlg <span class="kwrd">As</span> <span class="kwrd">New</span> ExceptionDialog</pre>
<pre> errorDlg.DetailErrorMsg_TextBox.Text = GetDetailErrorMsg(e.Exception)</pre>
<pre class="alt"> errorDlg.ShowDialog()</pre>
<pre> <span class="kwrd">End</span> <span class="kwrd">Sub</span></pre>
<pre class="alt"><span class="preproc">#End Region</span></pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre><span class="preproc">#Region</span> <span class="str">"Private Shared Method"</span></pre>
<pre class="alt"> </pre>
<pre> <span class="rem">'***************************************************************************</span></pre>
<pre class="alt"> <span class="rem">'Author: Larry Nung</span></pre>
<pre> <span class="rem">'Date: 2009/4/9</span></pre>
<pre class="alt"> <span class="rem">'Purpose: </span></pre>
<pre> <span class="rem">'Memo: </span></pre>
<pre class="alt"> <span class="rem">'***************************************************************************</span></pre>
<pre> <span class="rem">''' <summary></span></pre>
<pre class="alt"> <span class="rem">''' Gets the detail error MSG.</span></pre>
<pre> <span class="rem">''' </summary></span></pre>
<pre class="alt"> <span class="rem">''' <returns></returns></span></pre>
<pre> <span class="rem">''' <remarks></remarks></span></pre>
<pre class="alt"> <span class="kwrd">Private</span> <span class="kwrd">Shared</span> <span class="kwrd">Function</span> GetDetailErrorMsg(<span class="kwrd">ByVal</span> e <span class="kwrd">As</span> Exception) <span class="kwrd">As</span> <span class="kwrd">String</span></pre>
<pre> <span class="kwrd">Dim</span> str <span class="kwrd">As</span> <span class="kwrd">New</span> StringBuilder</pre>
<pre class="alt"> str.AppendLine(<span class="kwrd">String</span>.Format(<span class="str">"Source: {0}"</span>, e.Source))</pre>
<pre> str.AppendLine(<span class="kwrd">String</span>.Format(<span class="str">"Message: {0}"</span>, e.Message))</pre>
<pre class="alt"> str.AppendLine(<span class="kwrd">String</span>.Format(<span class="str">"TargetSite: {0}"</span>, e.TargetSite))</pre>
<pre> str.AppendLine(<span class="str">""</span>)</pre>
<pre class="alt"> str.AppendLine(<span class="str">"StackTrace: "</span>)</pre>
<pre> str.AppendLine(e.StackTrace)</pre>
<pre class="alt"> <span class="kwrd">Return</span> str.ToString</pre>
<pre> <span class="kwrd">End</span> <span class="kwrd">Function</span></pre>
<pre class="alt"><span class="preproc">#End Region</span></pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre> </pre>
<pre class="alt"><span class="preproc">#Region</span> <span class="str">"Event Process"</span></pre>
<pre> <span class="kwrd">Private</span> <span class="kwrd">Sub</span> OK_Button_Click(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> System.<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.EventArgs) <span class="kwrd">Handles</span> OK_Button.Click</pre>
<pre class="alt"> <span class="kwrd">Me</span>.DialogResult = System.Windows.Forms.DialogResult.OK</pre>
<pre> <span class="kwrd">Me</span>.Close()</pre>
<pre class="alt"> <span class="kwrd">End</span> <span class="kwrd">Sub</span></pre>
<pre> </pre>
<pre class="alt"> <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Cancel_Button_Click(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> System.<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.EventArgs) <span class="kwrd">Handles</span> Cancel_Button.Click</pre>
<pre> <span class="kwrd">Me</span>.DialogResult = System.Windows.Forms.DialogResult.Cancel</pre>
<pre class="alt"> Application.<span class="kwrd">Exit</span>()</pre>
<pre> <span class="kwrd">End</span> <span class="kwrd">Sub</span></pre>
<pre class="alt"> </pre>
<pre> <span class="kwrd">Private</span> <span class="kwrd">Sub</span> Detail_Button_Click(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> System.<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.EventArgs) <span class="kwrd">Handles</span> Detail_Button.Click</pre>
<pre class="alt"> _isDetailOpened = <span class="kwrd">Not</span> _isDetailOpened</pre>
<pre> Detail_Button.Text = <span class="kwrd">If</span>(_isDetailOpened, CloseDetailButtonText, OpenDetailButtonText)</pre>
<pre class="alt"> <span class="kwrd">Me</span>.Height = <span class="kwrd">If</span>(_isDetailOpened, <span class="kwrd">Me</span>.DetailErrorMsg_TextBox.Bottom, <span class="kwrd">Me</span>.DetailErrorMsg_TextBox.Top) + 32</pre>
<pre> <span class="kwrd">End</span> <span class="kwrd">Sub</span></pre>
<pre class="alt"> </pre>
<pre> <span class="kwrd">Private</span> <span class="kwrd">Sub</span> ExceptionDialog_Load(<span class="kwrd">ByVal</span> sender <span class="kwrd">As</span> System.<span class="kwrd">Object</span>, <span class="kwrd">ByVal</span> e <span class="kwrd">As</span> System.EventArgs) <span class="kwrd">Handles</span> <span class="kwrd">MyBase</span>.Load</pre>
<pre class="alt"> <span class="kwrd">Me</span>.Height = <span class="kwrd">Me</span>.DetailErrorMsg_TextBox.Top + 32</pre>
<pre> <span class="kwrd">Me</span>.ErrorIcon_Label.Image = SystemIcons.<span class="kwrd">Error</span>.ToBitmap</pre>
<pre class="alt"> <span class="kwrd">End</span> <span class="kwrd">Sub</span></pre>
<pre><span class="preproc">#End Region</span></pre>
<pre class="alt"> </pre>
<pre><span class="kwrd">End</span> <span class="kwrd">Class</span></pre>
<pre><span class="kwrd">using</span> System.Collections.Generic;</pre>
<pre class="alt"><span class="kwrd">using</span> System.ComponentModel;</pre>
<pre><span class="kwrd">using</span> System.Data;</pre>
<pre class="alt"><span class="kwrd">using</span> System.Drawing;</pre>
<pre><span class="kwrd">using</span> System.Linq;</pre>
<pre class="alt"><span class="kwrd">using</span> System.Text;</pre>
<pre><span class="kwrd">using</span> System.Windows.Forms;</pre>
<pre class="alt"> </pre>
<pre> </pre>
<pre class="alt"><span class="kwrd">namespace</span> ExceptionDlgTest</pre>
<pre>{</pre>
<pre class="alt"> <span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> ExceptionDlg : Form</pre>
<pre> {</pre>
<pre class="alt"> </pre>
<pre> <span class="preproc">#region</span> Const</pre>
<pre class="alt"> <span class="kwrd">const</span> <span class="kwrd">string</span> OpenDetailButtonText = <span class="str">"v 詳細資料"</span>;</pre>
<pre> <span class="kwrd">const</span> <span class="kwrd">string</span> CloseDetailButtonText = <span class="str">"^ 詳細資料"</span>;</pre>
<pre class="alt"> <span class="preproc">#endregion</span></pre>
<pre> </pre>
<pre class="alt"> <span class="preproc">#region</span> Var</pre>
<pre> <span class="kwrd">private</span> Boolean _isDetailOpened;</pre>
<pre class="alt"> <span class="preproc">#endregion</span></pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre> <span class="preproc">#region</span> Construction</pre>
<pre class="alt"> <span class="kwrd">public</span> ExceptionDlg()</pre>
<pre> {</pre>
<pre class="alt"> InitializeComponent();</pre>
<pre> }</pre>
<pre class="alt"> <span class="preproc">#endregion</span></pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre> </pre>
<pre class="alt"> <span class="preproc">#region</span> Public Shared Method</pre>
<pre> </pre>
<pre class="alt"> <span class="rem">//***************************************************************************</span></pre>
<pre> <span class="rem">//Author: Larry Nung</span></pre>
<pre class="alt"> <span class="rem">//Date: 2009/4/9</span></pre>
<pre> <span class="rem">//Purpose: </span></pre>
<pre class="alt"> <span class="rem">//Memo: </span></pre>
<pre> <span class="rem">//***************************************************************************</span></pre>
<pre class="alt"> <span class="rem">/// <summary></span></pre>
<pre> <span class="rem">/// </span></pre>
<pre class="alt"> <span class="rem">/// </summary></span></pre>
<pre> <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ShowBugWindowOnError()</pre>
<pre class="alt"> {</pre>
<pre> Application.ThreadException += <span class="kwrd">new</span> System.Threading.ThreadExceptionEventHandler(OnErrorOccur);</pre>
<pre class="alt"> } </pre>
<pre> <span class="preproc">#endregion</span></pre>
<pre class="alt"> </pre>
<pre> </pre>
<pre class="alt"> <span class="preproc">#region</span> Protected Shared Method</pre>
<pre> <span class="kwrd">protected</span> <span class="kwrd">static</span> <span class="kwrd">void</span> OnErrorOccur(<span class="kwrd">object</span> sender, System.Threading.ThreadExceptionEventArgs e)</pre>
<pre class="alt"> {</pre>
<pre> ExceptionDlg errorDlg = <span class="kwrd">new</span> ExceptionDlg();</pre>
<pre class="alt"> errorDlg.DetailErrorMsg_TextBox.Text = GetDetailErrorMsg(e.Exception);</pre>
<pre> errorDlg.ShowDialog();</pre>
<pre class="alt"> }</pre>
<pre> <span class="preproc">#endregion</span></pre>
<pre class="alt"> </pre>
<pre> </pre>
<pre class="alt"> <span class="preproc">#region</span> Private Shared Method</pre>
<pre> <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">string</span> GetDetailErrorMsg(Exception e)</pre>
<pre class="alt"> {</pre>
<pre> StringBuilder str = <span class="kwrd">new</span> StringBuilder();</pre>
<pre class="alt"> str.AppendLine(String.Format(<span class="str">"Source: {0}"</span>, e.Source));</pre>
<pre> str.AppendLine(String.Format(<span class="str">"Message: {0}"</span>, e.Message));</pre>
<pre class="alt"> str.AppendLine(String.Format(<span class="str">"TargetSite: {0}"</span>, e.TargetSite));</pre>
<pre> str.AppendLine(<span class="str">""</span>);</pre>
<pre class="alt"> str.AppendLine(<span class="str">"StackTrace: "</span>);</pre>
<pre> str.AppendLine(e.StackTrace);</pre>
<pre class="alt"> <span class="kwrd">return</span> str.ToString();</pre>
<pre> }</pre>
<pre class="alt"> <span class="preproc">#endregion</span></pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre> <span class="preproc">#region</span> Event Process</pre>
<pre class="alt"> <span class="kwrd">private</span> <span class="kwrd">void</span> Cancel_Button_Click(<span class="kwrd">object</span> sender, EventArgs e)</pre>
<pre> {</pre>
<pre class="alt"> Application.Exit();</pre>
<pre> }</pre>
<pre class="alt"> </pre>
<pre> <span class="kwrd">private</span> <span class="kwrd">void</span> OK_Button_Click(<span class="kwrd">object</span> sender, EventArgs e)</pre>
<pre class="alt"> {</pre>
<pre> <span class="kwrd">this</span>.Close();</pre>
<pre class="alt"> }</pre>
<pre> </pre>
<pre class="alt"> <span class="kwrd">private</span> <span class="kwrd">void</span> Detail_Button_Click(<span class="kwrd">object</span> sender, EventArgs e)</pre>
<pre> {</pre>
<pre class="alt"> _isDetailOpened = !_isDetailOpened;</pre>
<pre> Detail_Button.Text = _isDetailOpened ? CloseDetailButtonText : OpenDetailButtonText;</pre>
<pre class="alt"> <span class="kwrd">this</span>.Height = _isDetailOpened ? <span class="kwrd">this</span>.DetailErrorMsg_TextBox.Bottom : <span class="kwrd">this</span>.DetailErrorMsg_TextBox.Top + 32;</pre>
<pre> }</pre>
<pre class="alt"> </pre>
<pre> <span class="kwrd">private</span> <span class="kwrd">void</span> ExceptionDlg_Load(<span class="kwrd">object</span> sender, EventArgs e)</pre>
<pre class="alt"> {</pre>
<pre> <span class="kwrd">this</span>.Height = <span class="kwrd">this</span>.DetailErrorMsg_TextBox.Top + 32;</pre>
<pre class="alt"> <span class="kwrd">this</span>.ErrorIcon_Label.Image = SystemIcons.Error.ToBitmap();</pre>
<pre> }</pre>
<pre class="alt"> <span class="preproc">#endregion</span></pre>
<pre> </pre>
<pre class="alt"> </pre>
<pre> }</pre>
<pre class="alt">}</pre>
Private Sub Application_ThreadException(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
...
Dim exceptionDlg As New ThreadExceptionDialog(e.Exception)
exceptionDlg.ShowDialog()
End Sub</pre></div>