[英]How to customize a “MsgBox” control in Visual Basic
Is there a way to customize the MsgBox control in Visual Basic? 有没有一种方法可以在Visual Basic中自定义MsgBox控件?
I use it quite often to alert users. 我经常使用它来提醒用户。 However it never pops up on the screen; 但是,它永远不会在屏幕上弹出。 it just appears along the bottom task bar. 它只是出现在底部任务栏上。 It also always has a heading similar to "App_data_xxx". 它还始终具有类似于“ App_data_xxx”的标题。 Can I improve this in any way? 我可以以任何方式改进它吗?
My searches aren't throwing up much help. 我的搜索并没有带来太多帮助。
For example: 例如:
' 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
You are using a reference to the MessageBox
class without specifying the Method
you want to call. 您正在使用对MessageBox
类的引用,而未指定要调用的Method
。 You cannot create an instance of MessageBox
and therefore cannot pass a string as parameter to try and create one. 您无法创建MessageBox
的实例,因此无法将字符串作为参数传递来尝试创建一个。
Use MessageBox.Show(string messageText)
to display the MessageBox
with the desired message. 使用MessageBox.Show(string messageText)
显示带有所需消息的MessageBox
。
As for your question, you should create your own MessageBox
class. 至于您的问题,您应该创建自己的MessageBox
类。 With this solution, you get all the options you want and can customize it completely. 使用此解决方案,您可以获得所需的所有选项,并且可以对其进行完全自定义。 The call will be a little different : 通话会有所不同:
//C#
var myCustomMessageBox = new CustomMessageBox();
myCustomMessageBox.ShowDialog();
//Vb
Dim myCustomMessageBox As New CustomMessageBox()
myCustomMessageBox.ShowDialog()
The ShowDialog() will be used to create the effect of a messagebox. ShowDialog()将用于创建消息框的效果。
You can use the MessageBox
function and its many variables. 您可以使用MessageBox
函数及其许多变量。 Here's an example of what it can do: 这是它可以做什么的示例:
MessageBox.Show("The Displayed text in the messagebox", _
"the text displayed in the title bar", MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)
Or you can still use MsgBox
and use its variables, though it gives you fewer options. 或者,您仍然可以使用MsgBox
并使用其变量,尽管它提供的选项较少。 Here's an example: 这是一个例子:
MsgBox("message text", MsgBoxStyle.Information, "title bar text")
In Visual Basic 2008 I had a requirement for a message box that would only stay on for short times and that the time it was on to be variable. 在Visual Basic 2008中,我需要一个消息框,该消息框只能保留很短的时间,并且该消息框的时间是可变的。 I also had the problem that when using extended screen that the msgbox showed up on the extended screen (which was a different form) instead of on the main computer screen. 我还有一个问题,当使用扩展屏幕时,msgbox会显示在扩展屏幕(这是另一种形式)上,而不是显示在主计算机屏幕上。 To overcome these problems I created a custom Message Box using a panel on the form I had on the main screen. 为了克服这些问题,我使用主屏幕上窗体上的面板创建了一个自定义消息框。
To call the message panel DoMessage("The message", Seconds, Buttons to show 1 = Ok only 2 = Yes(ok) and No, 3 = Yes, No and Cancel. If seconds is 0 or not specified then the time to show the panel is set to a long time (10000 seconds) If Buttons to show is not specified it is set to Ok button only. If seconds and buttons are both specified, If no button is clicked the panel will just hide after the timeout. 调用消息面板DoMessage(“ The message”,Seconds,Buttons以显示1 =仅确定2 =是(确定)和否,3 =是,否和取消。如果秒为0或未指定,则显示时间面板设置为较长时间(10000秒),如果未指定要显示的按钮,则仅设置为“确定”按钮;如果同时指定了秒和按钮,则如果未单击任何按钮,则该面板将在超时后隐藏。
The responses are 1 if Ok or Yes is clicked, 2 if No is clicked, 3 if Cancel is clicked It is put into DoMsgResp so you see what is in it to handle the response. 如果单击“确定”或“是”,则响应为1;如果单击“否”,则为2;如果单击“取消”,则为3。将其放入DoMsgResp中,以便您查看其中包含的内容来处理响应。
Create the message panel when opening the form by calling MakeMsgPanel() 通过调用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.