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

2025-01-05 07:29:21
推荐回答(5个)
回答1:

这个很简单哦,可以自己写一个窗体来代替Messagebox:
1 新建一个窗体
2 拖一个timer
3 放一个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 Sub

End Class

回答2:

自己写一个类似的form,load后计时并调close关闭。

回答3:

重载 ,自定义关闭方式.

回答4:

自动关闭?那你就不要弹出不行嘛?

回答5:

点确定