ComboBox以隨機亂數選定時不被變更的方法?

Ack Serkal
最近在做一個程式,其中有一個功能是讓使用者用下拉式選單提前指定好Combobox中的項目,再將其他使用者未決定項目的Combobox以隨機亂數作呈現。
起初的預想是:Combobox被使用者選定→Combobox.Enabled = False→按下"隨機"Button→符合"Combobox.Enabled = True"的Combobox跑隨機亂數,
但是實際執行後發現,即便Combobox.Enabled已經確定為False了,在跑隨機亂數的過程中,已選定的結果依舊會被隨機亂數產生出的結果覆蓋掉,想問各位大大有沒有辦法可以解決這個問題呢?

下列是程式的部分擷取。本人大約在前年年中時開始學習VB,所以下列程式如果有什麼拙劣之處還請見諒:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        For f = 0 To 35
            repeat(f) = 0
        Next f


        For j = 1 To num
            Randomize()
            Dim rnd As New Random
            If CType(Me.Controls("ComboBox" & j), ComboBox).Enabled = True Then
                count = 0
                x = 0
                Do Until count >= num

                    temp = rnd.Next(0, num)
                    If repeat(temp) = 0 Then

                        repeat(temp) = 1
                        prize(count) = temp
                        count += 1
                        x += 1
                        CType(Me.Controls("comboBox" & x), ComboBox).Text = strTemp(temp)

                    End If
                Loop
            End If
            Exit For
        Next j
    End Sub

P陳
Enable 是讓使用者不能用鍵盤 或 滑鼠 修改
但還是可以用 程式修改 其內容的 的
HB
for ref only

Public Class Form1
    Dim strS As String = ""
    Dim arrV() As String = {"AA", "FF", "CC", "123D", "WeRR", "QQQ", "236R"}

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.Items.AddRange(arrV)
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RandomizeArray(arrV)

        ComboBox1.Items.Clear()
        ComboBox1.Items.AddRange(arrV)
        ComboBox1.SelectedIndex = ComboBox1.Items.IndexOf(strS)
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        strS = ComboBox1.Text
    End Sub

    Private Sub RandomizeArray(ByVal arrValue() As String)
        Dim max_index As Integer = arrValue.Length - 1
        Dim rnd As New Random
        For i As Integer = 0 To max_index - 1
            Dim j As Integer = rnd.Next(i, max_index + 1)

            Dim temp As String = arrValue(i)
            arrValue(i) = arrValue(j)
            arrValue(j) = temp
        Next i
    End Sub

回到頂部