randomise TAB order VB.NET 2003/2010

Hi Experts,

I wish to find a solution to randomly change tab order on form load. (WinForm)... I have searched the net but only found issues regarding the problems within 2003 edition...

I have no code examples - sorry :(

Thanks,
Roberto
RobertoFreemanoAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
You could do:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tabs As New ArrayList
        For Each tp As TabPage In TabControl1.TabPages
            tabs.Add(tp)
        Next
        TabControl1.TabPages.Clear()

        Dim i As Integer
        Dim R As New Random
        While tabs.Count > 0
            i = R.Next(0, tabs.Count)
            TabControl1.TabPages.Add(tabs(i))
            tabs.RemoveAt(i)
        End While
    End Sub

Open in new window

0
 
käµfm³d 👽Commented:
Is this for VB or Delphi?
0
 
foobarrCommented:
on page load you could go

dim ctrl as control
dim rando as new random()

for each ctrl in frmName.controls

ctrl.TabIndex = rando.Next()


next
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
RobertoFreemanoAuthor Commented:
Hi kaufmed,

VB.NET 2003/2010

Thanks,
Roberto
0
 
RobertoFreemanoAuthor Commented:
Hi foobarr,

I get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred in certstar.exe
Additional information: Object reference not set to an instance of an object.


on  ctrl.TabIndex = rando.Next()
0
 
käµfm³d 👽Commented:
Then foobarr's example should suit you. The only thing you may have issue with is controls which contain other controls (GroupBox, Panel, etc.). In that case, you may consider having a recursive function to grab all controls. You could also use a Linq-based approach to grabbing all controls.
0
 
RobertoFreemanoAuthor Commented:
Thanks kaufmed,

I'm not sure how to proceed. :(

Roberto
0
 
käµfm³d 👽Connect With a Mentor Commented:
Something like this:
Public Class Form1
    Private rand As New Random(DateTime.Now.TimeOfDay.Milliseconds)

    Public Sub RecurseChildren(ByRef ctl As Control)
        For Each child As Control In ctl.Controls
            RecurseChildren(child)
        Next

        ctl.TabIndex = rand.Next(1, 1000)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RecurseChildren(Me)
    End Sub
End Class

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I see I misread the question!  =)
0
 
käµfm³d 👽Commented:
Heheh. Don't worry, we won't hold it against you  ; )
0
 
RobertoFreemanoAuthor Commented:
Thanks Idle_MInd, you solution worked.

Kaufmed, I couldn't get your solution to work, but thanks anyway.
0
 
käµfm³d 👽Commented:
I guess you didn't misread after all Idle....   everyone else did!

Good show old bean   :  )
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.