Đây là mã nguồn cho Msgbox. Như bạn có thể thấy nó không làm bất cứ điều gì đặc biệt thú vị trước khi gọi MessageBox.Show.
<MethodImpl(MethodImplOptions.NoInlining), HostProtection(SecurityAction.LinkDemand, Resources:=HostProtectionResource.UI)> _
Public Shared Function MsgBox(ByVal Prompt As Object, ByVal Optional Buttons As MsgBoxStyle = 0, ByVal Optional Title As Object = new Object()) As MsgBoxResult
Dim owner As IWin32Window = Nothing
Dim text As String = Nothing
Dim titleFromAssembly As String
Dim vBHost As IVbHost = HostServices.VBHost
If (Not vBHost Is Nothing) Then
owner = vBHost.GetParentWindow
End If
If ((((Buttons And 15) > MsgBoxStyle.RetryCancel) OrElse ((Buttons And 240) > MsgBoxStyle.Information)) OrElse ((Buttons And &HF00) > MsgBoxStyle.DefaultButton3)) Then
Buttons = MsgBoxStyle.OkOnly
End If
Try
If (Not Prompt Is Nothing) Then
[text] = CStr(Conversions.ChangeType(Prompt, GetType(String)))
End If
Catch exception As StackOverflowException
Throw exception
Catch exception2 As OutOfMemoryException
Throw exception2
Catch exception3 As ThreadAbortException
Throw exception3
Catch exception9 As Exception
Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", New String() { "Prompt", "String" }))
End Try
Try
If (Title Is Nothing) Then
If (vBHost Is Nothing) Then
titleFromAssembly = Interaction.GetTitleFromAssembly(Assembly.GetCallingAssembly)
Else
titleFromAssembly = vBHost.GetWindowTitle
End If
Else
titleFromAssembly = Conversions.ToString(Title)
End If
Catch exception4 As StackOverflowException
Throw exception4
Catch exception5 As OutOfMemoryException
Throw exception5
Catch exception6 As ThreadAbortException
Throw exception6
Catch exception13 As Exception
Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", New String() { "Title", "String" }))
End Try
Return DirectCast(MessageBox.Show(owner, [text], titleFromAssembly, (DirectCast(Buttons, MessageBoxButtons) And DirectCast(15, MessageBoxButtons)), (DirectCast(Buttons, MessageBoxIcon) And DirectCast(240, MessageBoxIcon)), (DirectCast(Buttons, MessageBoxDefaultButton) And DirectCast(&HF00, MessageBoxDefaultButton)), (DirectCast(Buttons, MessageBoxOptions) And DirectCast(-4096, MessageBoxOptions))), MsgBoxResult)
End Function
Nguồn
2012-01-10 07:46:36
+1 Mặc dù nó chủ yếu tồn tại * cho khả năng tương thích ngược * với mã VB6 đang hoạt động hiện tại. – MarkJ
Tôi nhận thấy rằng tôi không thể gọi MessageBox từ thư viện không phải GUI. Có vẻ như tôi sẽ cần phải tham khảo/nhập khẩu System.Windows.Forms để sử dụng nó trong thư viện, nhưng điều đó sẽ làm hỏng lý do tôi đang sử dụng một thư viện. MsgBox hoạt động tốt (để chuyển tiếp thông tin đến ứng dụng GUI cha), do đó, có ít nhất một sự khác biệt. –