Solved

randomise TAB order VB.NET 2003/2010

Posted on 2011-03-11
12
417 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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 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 75

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: ForceDirectory plain function 7 42
Help with error in Query 2 31
MailAddress in vb 4 30
Toolbar to format all windows forms controls at design time needed 5 16
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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