Solved

randomise TAB order VB.NET 2003/2010

Posted on 2011-03-11
12
412 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:RobertoFreemano
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35111492
Is this for VB or Delphi?
0
 
LVL 7

Expert Comment

by:foobarr
ID: 35112091
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
 

Author Comment

by:RobertoFreemano
ID: 35112157
Hi kaufmed,

VB.NET 2003/2010

Thanks,
Roberto
0
 

Author Comment

by:RobertoFreemano
ID: 35112190
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35112205
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
 

Author Comment

by:RobertoFreemano
ID: 35112225
Thanks kaufmed,

I'm not sure how to proceed. :(

Roberto
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 105 total points
ID: 35112231
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
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 20 total points
ID: 35112340
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35112383
I see I misread the question!  =)
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35112415
Heheh. Don't worry, we won't hold it against you  ; )
0
 

Author Closing Comment

by:RobertoFreemano
ID: 35112543
Thanks Idle_MInd, you solution worked.

Kaufmed, I couldn't get your solution to work, but thanks anyway.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35115596
I guess you didn't misread after all Idle....   everyone else did!

Good show old bean   :  )
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now