您的位置首页生活百科

在C#中,发生异常时MessageBox.Show(ex.Message)

在C#中,发生异常时MessageBox.Show(ex.Message)

这个很简单哦,可以自己写一个窗体来代替Messagebox:1 新建一个窗体2 拖一个timer3 放一个ok按钮,一个取消按钮4 重点是timer的运行代码-----------主要代码有:Public Class OpDialog#Region "字段" '''

''' 这个是设置显示多少秒,然后会自动关闭的 ''' ''' ''' ''' Public Property DisplaySeconds As Integer Get Return OpDialog._seconds End Get Set(ByVal value As Integer) If value >= 2 Then _seconds = value End If End Set End Property Private Shared _seconds As Integer = 0#End Region'设置ok和取消按钮的事件,就可以手动点击 Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click Me.DialogResult = System.Windows.Forms.DialogResult.OK Me.Close() End Sub Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click Me.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.Close() End Sub Private Sub OpDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblMsg.Text = My.MySettings.Default.OpDialogSeconds.ToString() _seconds = My.MySettings.Default.OpDialogSeconds Me.Timer1.Enabled = True Me.Timer1.Interval = 1000 End Sub'-------------这里是timer的运行代码,很简单吧,每tick一次把时间减去1秒 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If _seconds <= 0 Then Me.Timer1.Enabled = False CType(Me.ActiveControl, Button).PerformClick() End If _seconds -= 1 lblMsg.Text = (_seconds).ToString() End SubEnd Class