【问题标题】:Buttons in message box in Visual Basic .NETVisual Basic .NET 中消息框中的按钮
【发布时间】:2012-04-11 23:35:58
【问题描述】:

我正在编写一个井字游戏程序,并且我已经设置了棋盘,并且该游戏适用于大多数人。决定谁赢。我现在要做的是在确定获胜者后,会弹出一个消息框并显示谁获胜。我希望消息框包含两个按钮。一个带有文字“Ok for new game”的按钮和带有文字“Cancel to exit”的第二个按钮。我正在使用 Visual Basic 2010 Express。

这是我的代码:

Public Class Form1
    Private turn As Integer = 1
    Private play() As String = {"O", "X"}
    Private board(2, 2) As String

    Private Structure arrayIndex
        Dim x As Integer
        Dim y As Integer
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For x As Integer = 1 To 9
            Dim b As New Button With { _
                .Width = 80, _
                .Height = 80, _
                .Text = "", _
                .Location = New Point(60 + (((x - 1) Mod 3) * 80), 60 + (((x - 1) \ 3) * 80)), _
                .Tag = New arrayIndex With {.x = (x - 1) Mod 3, .y = (x - 1) \ 3}}
            Me.Controls.Add(b)
            AddHandler b.Click, AddressOf buttons_click

        Next
        Me.SetClientSizeCore(360, 360)


    End Sub

    Private Sub buttons_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If DirectCast(sender, Button).Text <> "" Then Return
        DirectCast(sender, Button).Text = play(turn Mod 2)
        Dim index As arrayIndex = DirectCast(DirectCast(sender, Button).Tag, arrayIndex)
        board(index.x, index.y) = play(turn Mod 2)
        turn += 1
        winner()
    End Sub

    Private Sub winner()
        Dim rows(7) As String

        rows(0) = board(0, 0) & board(1, 0) & board(2, 0)
        rows(1) = board(0, 1) & board(1, 1) & board(2, 1)
        rows(2) = board(0, 2) & board(1, 2) & board(2, 2)
        rows(3) = board(0, 0) & board(0, 1) & board(0, 2)
        rows(4) = board(1, 0) & board(1, 1) & board(1, 2)
        rows(5) = board(2, 0) & board(2, 1) & board(2, 2)
        rows(6) = board(0, 0) & board(1, 1) & board(2, 2)
        rows(7) = board(2, 0) & board(1, 1) & board(0, 2)

        For x As Integer = 0 To 7
            If rows(x).Length = 3 AndAlso (rows(x)(0) = rows(x)(1) AndAlso rows(x)(0) = rows(x)(2)) Then
                MessageBox.Show(rows(x)(0) & "'s winsssss!", "We have a winner!", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
            If DialogResult.OK Then
            turn = 1
            ReDim board(2, 2)
            For Each ctrl As Control In Controls
            ctrl.Text = ""
            Next
            Return
            Else
               Me.Close()


            End If
    End Sub
End Class

【问题讨论】:

  • 代码提示:在 VB 2010 中大部分时间都不需要_ 行延续。
  • 好的,我只是决定将它们包括在内,以防万一。谢谢你的提示。您对如何解决我的问题有什么想法吗?
  • 当然 - MsgBox 不可能。您需要再创建一个 Form - 但这不会太耗时。
  • 我可以使用 MsgBoxStyle 吗?当点击确定时,我可以这样创建板子,当点击取消时,关闭程序?
  • 当然你可以使用MsgBoxStyle(虽然我建议你实际使用本机.NET MessageBox.Show)但是你不会得到你的自定义按钮文本。

标签: vb.net


【解决方案1】:

最简单的方法是创建一个新的Form,它的作用是MessageBox。有很多API 可以自定义MessageBox,但会给您额外的时间。所以这就是你要做的。

  • 新建表单
  • 将 MaximizeButton、MinimizeButton 和 ShowOnTitleBar 设置为 False
  • FormBorderStyle 设置为 FixedDialog,使其不会太大
  • 在上面添加两个按钮(Ok for new gameCancel to exit
  • StartUpPosition 设置为CenterForm

将此代码添加到On for New Game Button点击事件

DialogResult = DialogResult.Yes

将此代码添加到Cancel to exit点击事件

DialogResult = DialogResult.No

游戏结束后,尝试调用你创建的新表单。

Dim xForm as new frmAsk ' Assuming that frmAsk is the name of your new form
If xForm.showDialog = DialogResult.Yes Then
    ' New game here
Else
    ' exit game
End If

希望对你有所帮助。

【讨论】:

  • 我会在我创建的第一个表单中添加最后一段代码吗?还是进入新的形式?
  • 只有dialogresult 会添加到新表单中。 Dim xForm... 将添加到第一个表单中。
  • 对于你的例子,新的表单名称 frmAsk,它是说 frmAsk 没有被声明
  • 实际上,没关系,这确实有效。我没有在我的项目中添加新表单。
  • 打平的时候你能帮我吗?
【解决方案2】:

你为什么不试试这个:

If Msgbox("Your text here") <> MsgboxResult.Yes then
    Exit
Else
    Your code there to continue the game.
End if

希望这会有所帮助。

【讨论】:

  • 感谢您的意见和帮助。但是如果打成平手,你认为你能帮我写代码吗?我知道我必须创建一个布尔变量来查看空间是否已填充。但不确定如何实际编码。
【解决方案3】:

试试这个

Select Case msgbox("You Win! Do you want to play another game?",vbYesNo)
    case vbYes
        'add code to start a new game
    case vbNo
        'add code to exit your app
        Unload Me
End Select

【讨论】:

  • 我现在已经开始工作了。但我正在尝试编写平局时的代码。我似乎无法真正弄清楚那部分。我不知道为什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 2012-04-23
相关资源
最近更新 更多