0

私は Visual Basic を初めて使用し、Tic Tac Toe の演習に行き詰まっています。これが私のコードです。

Public Class Form1
    Public Enum Buttons As Byte
        btn1 = 1
        btn2 = 2
        btn3 = 3
        btn4 = 4
        btn5 = 5
        btn6 = 6
        btn7 = 7
        btn8 = 8
        btn9 = 9
    End Enum

    Public Sub Computer()
        Dim RandomNumberGenerator As New Random
        Dim RandomNumber As Integer
        RandomNumber = RandomNumberGenerator.Next(1, 9)
        Dim RandomButton = CType(RandomNumber, Buttons)
        Do
            If RandomButton.Enabled = True Then
                RandomButton.Enabled = False
                RandomButton.Text = "O"
                RandomButton.Font = New Font("Consolas", 50, FontStyle.Bold)
                Exit Do
            Else
                RandomNumber = RandomNumberGenerator.Next(1, 9)
                Dim RandomButton = CType(RandomNumber, Buttons)
            End If
        Loop

私が抱えている問題は、if ループです。選択したランダムボタンが有効になっているかどうかを確認しようとしています。しかし、代わりに、Visual Basic は、「Enable は Tic_Tac_Toe.Fourm1.Buttons のメンバーではありません。列挙型を介してボタンを無効または有効にできる方法があるかどうか知りたかったのです。誰かがこれを理解するのを手伝ってくれますか?

4

1 に答える 1

1

First problem: Your enum (Buttons) doesn't have a member "Enabled". You are setting properties of a Button on an enum, which won't work.

You have to create an actual UI Button for each "RandomButton" and set the value, ID, and text, then add it to the form. At this point you could get/set those properties, including "Enabled".

If these buttons are all on the Form already and you're just trying to pick a random one, you can use Me.Controls.Find(ID)

于 2013-06-04T00:40:01.003 に答える