?
Solved

randomise TAB order VB.NET 2003/2010

Posted on 2011-03-11
12
Medium Priority
?
424 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 75

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 75

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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 420 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 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 80 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 86

Expert Comment

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

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 75

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

839 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