PictureBoxes ในอาร์เรย์

เอาล่ะ ฉันมีปัญหาสองประการกับรหัสนี้:

  1. ฉันมีกล่องรูปภาพ 8 กล่อง ฉันกำลังพยายามตรวจสอบตัวเลขที่สร้างแบบสุ่ม เพื่อดูว่าตรงกับตัวเลขที่อยู่ท้ายกล่องรูปภาพหรือไม่ (เช่น ถ้าตัวเลขสุ่ม = 8 ค่าคงที่อื่นจะถูกใส่ลงในกล่องรูปภาพ) 8 และไม่มีอย่างอื่น แต่ถ้ายังไม่มีบางอย่างอยู่ในนั้น)

    ฉันทำมันโดยเพียงแค่ใช้เช็คทุกครั้งเป็นคำสั่ง if เดียว แต่... 180 ถ้าคำสั่งในภายหลัง... นั่นมากเกินไปสำหรับการเขียนโค้ด

    ฉันกำลังพยายาม (อย่างที่คุณเห็น) รันมันผ่านหลาย ๆ ลูป ส่วนหลักที่ฉันมีปัญหาคือการกำหนดกล่องรูปภาพเฉพาะในอาเรย์ด้วยรูปภาพจากอาเรย์ ฉันสามารถรับรูปภาพโดยใช้ imgName และ imgPictures(i) โดยใช้ในรูปแบบ PictureBox1.Image = imgPictures(i) แต่ไม่สามารถแทนที่ PictureBox1.Image ด้วย picBoxes().Image ได้

  2. การใช้ MsgBoxes ที่ฉันทิ้งไว้ในโค้ด ลูปและคำสั่ง if เรียกใช้ "Debug1" และ "Debug2" แต่ไม่มีคำสั่งอื่นๆ... ทำไมจึงเป็นเช่นนี้

ฉันจะทิ้งรหัสไว้และดูว่าพวกคุณจะทำอะไรได้บ้าง

Dim i As Integer = 1
Dim x As Integer = 1
Dim rndnumber As Integer = mathsclass.get_randomnumber()
Dim imgPictures(20) As Image
Dim picBoxes(8) As PictureBox
picBoxes = New PictureBox() {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8}
Dim imgName As String = ("_" & i)
imgPictures(0) = My.Resources.ResourceManager.GetObject(imgName)
picBoxes(x).Image = imgPictures(i)

While (i <= 20)
    MsgBox("Debug1")
    rndnumber = mathsclass.get_randomnumber()
    imgName = ("_" & i)
    imgPictures(i) = My.Resources.ResourceManager.GetObject(imgName)
    x = 0
    While (x < 8)
        MsgBox("Debug2")
        If ((randomnumber = i) & (randomposition = x)) Then
            MsgBox("Debug3")
            picBoxes(x).Image = imgPictures(i)
        Else
            While (rndnumber = randomnumber)
                MsgBox("Debug4")
                rndnumber = mathsclass.get_randomnumber()
            End While
            MsgBox("Debug5")
            If ((randomnumber <> rndnumber) & (randomposition <> x)) Then
                MsgBox("Debug6")
                imgName = ("_" & rndnumber)
                imgPictures(i) = My.Resources.ResourceManager.GetObject(imgName)
                picBoxes(x).Image = imgPictures(i)
            End If
            MsgBox("Debug7")
        End If
        MsgBox("Debug8")
        x += 1
    End While
    MsgBox("Debug9")
    i += 1
End While

person Breandán Fawcett    schedule 11.12.2014    source แหล่งที่มา
comment
คุณต้องชัดเจนมากขึ้นอีกหน่อยว่าคุณใช้ภาษาอะไร แต่ฉันให้ +1 สำหรับคำถามที่น่าสนใจ สำหรับ VB6 การดีบักโค้ดนั้นเป็นเรื่องง่าย เพียงกด F8 ใน IDE แล้วคุณจะเริ่มก้าวผ่านโค้ด คุณสามารถแสดงตัวแปรและดูว่าเกิดอะไรขึ้น   -  person Kevin    schedule 13.12.2014


คำตอบ (1)


ฉันไม่แน่ใจจริงๆว่าคุณพยายามใช้ภาษาใดที่นี่ ฉันหมายถึงไม่ว่าจะเป็น VB.Net หรือคลาส VB6! ฉันคิดว่ามันคือ VisualBasic.Net (VB.Net)

ไม่แน่ใจว่าฉันเข้าใจคุณชัดเจนหรือไม่ ดังนั้นฉันจึงให้โค้ดตัวอย่างเล็กๆ น้อยๆ แก่คุณเพื่อให้คุณลองใช้ ความคิดเห็นในนั้นค่อนข้างจะอธิบายได้ว่าฉันเชื่อ

ขั้นแรกให้สร้างโปรเจ็กต์ว่างใหม่ เพิ่มกล่องข้อความ 5 กล่องลงไป (ไม่จำเป็นต้องเปลี่ยนชื่อ เพียงลากและวางกล่องข้อความ 5 หมายเลข) เพิ่มปุ่มแล้วดับเบิลคลิกแล้ววางโค้ดที่ฉันแสดงด้านล่าง:

    Dim rand As New Random          '~~~ for creating the random numer
    Dim intTotal As Integer = 5     '~~~ I have 5 textboxes with the names ending by number from 1 to 5
    Dim intUpdated As Integer = 0   '~~~ to store the total number of updated textboxes

    '~~~ first of all we are looping through the textboxes(am just showing how you can fetch them via it's name. There are other ways to iterate through the controls though!)
    For i As Integer = 1 To intTotal
        Dim tbox As TextBox = DirectCast(Me.Controls.Item("TextBox" & i.ToString()), TextBox)   '~~~ here, we are fetching the TextBox via it's "Name" property, by appending the number at the end of "TextBox". Because in my form, I have "TextBox1", "TextBox2", "TextBox3", "TextBox4" and "TextBox5"

        tbox.Tag = "untouched"  '~~~ just setting it's "Tag" property to some text so that we could later use it evaluate whether we have touched this textbox via the random number

        '-------------------------------------------------
        'if you want to do something else like loading up default text or something, you can do it here too
        '-------------------------------------------------
    Next

    '~~~ the randomly choosing part..
    Dim j As Integer = 1
    Do While intUpdated < intTotal  '~~~ loop until we have taken into consideration all the textboxes via random number
        Dim r As Integer = rand.Next(1, intTotal + 1) '~~~ create a random number between 1 and 5 (both inclusive)

        '~~~ fetch that TextBox via it's name. Note that, am appending the random number to the name "TextBox". For eg, if the random number generated was "2", it would access "TextBox2"
        Dim tbox As TextBox = DirectCast(Me.Controls.Item("TextBox" & r.ToString()), TextBox)

        '~~~ since we have textbox now, we are checking whether we have accessed it earlier via random number
        If tbox.Tag.ToString() = "untouched" Then   '~~~ if not accessed before...
            tbox.Tag = "touched"    '~~~ mark the textbox as accessed via random number
            intUpdated += 1         '~~~ increment the counter variable that we are using for storing the number of textboxes we accessed

            '-------------------------------------------------
            '~~~ do whatever you want on it here.. Right now, am just inserting some text on it to show the order in which the textboxes are accessed via our random number
            tbox.Text = "updated" & j.ToString
            j += 1
            '-------------------------------------------------
        End If
    Loop

หลังจากดำเนินการแล้ว ฉันหวังว่าคุณจะสามารถเข้าใจแนวคิดนี้ได้ หากต้องการเข้าถึงชื่อการควบคุมที่อยู่ในแบบฟอร์มของคุณ คุณสามารถเข้าถึงได้ดังนี้: Me.Controls.Item("control_name_goes_here") ดังนั้นคุณจึงสามารถเพิ่มตัวเลขต่อท้ายสตริงได้เมื่อคุณกล่าวถึงชื่อของตัวควบคุม ฉันได้ใช้สิ่งนี้ในโค้ดด้านบน ดูมันสิ

BTW คุณกำลังใช้ & ซึ่งอยู่ใน VB จะทำหน้าที่เหมือนกับตัวดำเนินการต่อข้อมูล! กล่าวคือ Dim a As String = "Akhilesh" & "B Chandran" สิ่งนี้จะเก็บ Akhilesh B Chandran ไว้ในตัวแปรนั้น a สำหรับการดำเนินการ AND คุณต้องใช้ AND ใน C, C++, C# ฯลฯ จะเป็น && (โปรดทราบว่ามีอักขระเครื่องหมายและสองตัว)

นอกจากนี้ ใน VB.Net คุณควรใช้ MessageBox.Show() แทนฟังก์ชัน MsgBox() นั่นเป็นวิธีที่ดีกว่า

หวังว่านี่จะช่วยได้

person Akhilesh B Chandran    schedule 12.12.2014