0

有没有办法在 Visual Basic中自定义MsgBox控件?

我经常使用它来提醒用户。但是它永远不会在屏幕上弹出;它只出现在底部任务栏上。它也总是有一个类似于“App_data_xxx”的标题。我可以以任何方式改善这一点吗?

我的搜索并没有提供太多帮助。

例如:

' Queries user number and password against the database and returns user
Dim matchingusers = From u In db.Users
                    Where username = u.Email And password = u.Password
                    Select u

' Checks if a valid user was returned
If matchingusers.Count = 0 Then
    MsgBox("Invalid user entered, try again")
Else
    SelectedDeveloper = 0
    ' Set the logged in user for use in the project
    GlobalVariables.LoggedInUser = matchingusers.First
4

3 回答 3

3

您可以使用该MessageBox函数及其许多变量。这是它可以做什么的示例:

 MessageBox.Show("The Displayed text in the messagebox", _
         "the text displayed in the title bar", MessageBoxButtons.YesNoCancel, _
             MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)

或者你仍然可以MsgBox使用它的变量,尽管它给你的选择更少。这是一个例子:

MsgBox("message text", MsgBoxStyle.Information, "title bar text")
于 2013-04-15T19:22:07.437 回答
2

您正在使用对MessageBox类的引用而不指定Method要调用的类。您不能创建一个实例,MessageBox因此不能将字符串作为参数传递来尝试创建一个。

用于MessageBox.Show(string messageText)显示MessageBox所需消息。

至于您的问题,您应该创建自己的MessageBox课程。使用此解决方案,您可以获得所需的所有选项,并且可以完全自定义它。调用会有所不同:

//C#
var myCustomMessageBox = new CustomMessageBox();
myCustomMessageBox.ShowDialog();

//Vb
Dim myCustomMessageBox As New CustomMessageBox()
myCustomMessageBox.ShowDialog()

ShowDialog()将用于创建消息框的效果。

于 2013-04-15T15:22:10.187 回答
0

在 Visual Basic 2008 中,我需要一个消息框,它只能在短时间内保持打开状态,并且它的打开时间是可变的。我还有一个问题,当使用扩展屏幕时,msgbox 出现在扩展屏幕(这是一种不同的形式)而不是主计算机屏幕上。为了克服这些问题,我使用主屏幕上的表单上的面板创建了一个自定义消息框。

调用消息面板 DoMessage("The message", Seconds, Buttons to show 1 = Ok only 2 = Yes(ok) and No, 3 = Yes, No and Cancel. 如果秒为 0 或未指定,则显示时间面板设置为长时间(10000 秒) 如果未指定要显示的按钮,则仅设置为确定按钮。如果同时指定了秒和按钮,则如果未单击任何按钮,则面板将在超时后隐藏。

如果单击 Ok 或 Yes,则响应为 1,如果单击 No,则返回 2,如果单击 Cancel,则返回 3 它被放入 DoMsgResp 中,因此您可以看到其中处理响应的内容。

通过调用 MakeMsgPanel() 打开表单时创建消息面板

Dim MessagePanel As New Panel    'The panel 
Dim MessageLabel As New Label    'The message 
Dim MsgYes As New Button          'Yes or OK button
Dim MsgNo As New Button          'no button
Dim MsgCcl As New Button         'Cancel button
Dim Sleepsecs As Integer          'How long panel shows for 
Dim DoMsgResp As Integer         'response 1, 2 or 3 depending which button clicked
Private Sub MakeMsgPanel()
    Me.Controls.Add(MessagePanel)
    Me.MessagePanel.Controls.Add(MessageLabel)
    Me.MessagePanel.Controls.Add(MsgYes)
    Me.MessagePanel.Controls.Add(MsgNo)
    Me.MessagePanel.Controls.Add(MsgCcl)
    MessagePanel.Location = New System.Drawing.Point(Me.Width / 2 - 200, Me.Height / 2 - 100)
    MessagePanel.BackColor = Color.PaleGreen
    MessageLabel.BackColor = Color.PeachPuff
    MessagePanel.BorderStyle = BorderStyle.FixedSingle
    MessageLabel.Font = New Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point)
    MessageLabel.AutoSize = True
    MessagePanel.AutoSize = True
    MessagePanel.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowOnly
    MessagePanel.Hide()
    MsgYes.Location = New System.Drawing.Point(205, 5)
    MsgNo.Location = New System.Drawing.Point(115, 5)
    MsgCcl.Location = New System.Drawing.Point(25, 5)
    MsgYes.Text = "Yes"
    MsgNo.Text = "No"
    MsgCcl.Text = "Cancel"
    AddHandler MsgYes.Click, AddressOf MsgYes_Click
    AddHandler MsgNo.Click, AddressOf MsgNo_Click
    AddHandler MsgCcl.Click, AddressOf MsgCcl_Click
End Sub


Private Sub MsgYes_Click()
    DoMsgResp = 1
    Sleepsecs = 0
End Sub


Private Sub MsgNo_Click()
    DoMsgResp = 2
    Sleepsecs = 0
End Sub
Private Sub MsgCcl_Click()
    DoMsgResp = 3
    Sleepsecs = 0
End Sub


  Private Sub DoMessage(ByVal Msg As String, Optional ByVal Secs As Integer = 0, _
           Optional ByVal Btns As Integer = 0)
    'Information messages that can be timed
    Dim TheHeight As Integer
    Dim TheWidth As Integer
    Dim Labelx As Integer
    Dim Labely As Integer
    DoMsgResp = 0
    MessageLabel.Text = Msg
    If MessageLabel.Height < 90 Then
        TheHeight = 100
        Labely = (100 - MessageLabel.Height) / 2
    Else
        TheHeight = MessageLabel.Height + 10
        Labely = 5
    End If
    If MessageLabel.Width < 140 Then
        TheWidth = 150
        Labelx = (150 - MessageLabel.Width) / 2
    Else
        TheWidth = MessageLabel.Width + 10
        Labelx = 5
    End If
    MessagePanel.Size = New System.Drawing.Size(TheWidth, TheHeight)
    MessageLabel.Location = New System.Drawing.Point(Labelx, Labely)
    MessageLabel.Show()
    MessagePanel.Show()
    MessagePanel.BringToFront()
    MsgYes.BringToFront()
    MsgNo.BringToFront()
    MsgCcl.BringToFront()
    MessagePanel.Focus()
    If Btns = 0 Or Btns > 3 Then Btns = 1 'Make ok button if none specified or number too high
    If Btns = 1 Then
        MsgYes.Text = "Ok"
        MsgNo.Hide()
        MsgCcl.Hide()
    Else    'is 2 or 3 
        MsgYes.Text = "Yes"
        MsgNo.Show()
        If Btns = 2 Then MsgCcl.Hide() Else MsgCcl.Show()
    End If
    If Secs = 0 Then Secs = 10000 'make a long time
    If Secs > 0 Then
        Sleepsecs = Secs * 2
        Do Until Sleepsecs < 1
            Threading.Thread.Sleep(500)
            Application.DoEvents()
            Application.RaiseIdle(New System.EventArgs)
            Sleepsecs = Sleepsecs - 1
        Loop
    End If
    MessagePanel.Hide()
End Sub


 Private Sub ButtonTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTest.Click
            DoMessage("This is To see what happens with my message" & vbCrLf & _
              "see if it works good", 0, 3)
    If DoMsgResp = 1 Then
        MsgBox("Ok was hit")
    End If
    If DoMsgResp = 2 Then
        MsgBox("No was hit")
    End If
    If DoMsgResp = 3 Then
        MsgBox("Cancel was hit")
    End If
End Sub
于 2014-07-04T07:41:56.650 回答