Leg*_*mbe 9 vb.net visual-studio-2010
以下两个之间有区别吗?
msgbox()
messagebox.show()
Run Code Online (Sandbox Code Playgroud)
一些教程使用msgbox(),有些使用另一个,messagebox.show()---我看到两者都可以有一个可编辑的样式,但我想知道:为什么有两个?
是否适应年龄较大的程序员(在旧版本的Visual Basic上学习过)?
那么在这种情况下,我应该在Visual Basic 2010(Visual Studio 2010)中使用哪一个?
Ode*_*ded 10
MsgBox()是一样的Messagebox.Show().
它适用于习惯它的VB6程序员.
没有关于使用哪一个的规则,但由于MsgBox只是最终委托MessageBox,我个人会直接使用MessageBox.
这是Msgbox的源代码。如您所见,在调用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
Run Code Online (Sandbox Code Playgroud)
当您尝试将图标与不同按钮混合使用时会有所不同。MsgBox 具有预定义的样式(可能有一种方法可以创建新样式)。
例如:
MsgBox("Do you wish to save changes?", MsgBoxStyle.YesNoCancel, "Save Changes")
Run Code Online (Sandbox Code Playgroud)

^ 这将显示一个带有是、否和取消按钮的框,没有图标。
MsgBox("Do you wish to save changes?", MsgBoxStyle.Question, "Save Changes")
Run Code Online (Sandbox Code Playgroud)

^ 这将显示一个带有问号图标的框,但只有一个确定按钮。
MessageBox.Show("Do you wish to save changes?", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
Run Code Online (Sandbox Code Playgroud)

^ 这将显示一个带有是、否和取消按钮以及一个问号图标的框。
如您所见,使用 MessageBox.Show 可以让您拥有带有任何图标的任何按钮。
| 归档时间: |
|
| 查看次数: |
29363 次 |
| 最近记录: |