Anyone know how we can cycle through the controls on a form starting left to right ?

dominico

Well-known member
Joined
Mar 9, 2005
Messages
57
Programming Experience
3-5
RESOLVED. Thanks to "jmcilhinney"

Please help me how to cycle through the controls below starting from left to right and then continue to the next row until the end of the form.

The problem is that the controls are not focused in the order from left to right. It starts anywhere on the form. I would like to start with the most left and then one by one toward the right.

Here is my code so far:
Note: I only want to read textboxes and comboboxes.

For I = 1 ToMe.Controls.Count - 1 ' cycle through the controls on the form.
' select only textboxes and comboboxes controls to extract data

If ((TypeOfMe.Controls(I) Is TextBox) Or (TypeOfMe.Controls(I) Is ComboBox)) And (Me.Controls(I).Text <> "") Then

.....

End If


Next I

Thank you everyone,

Dominico
 
Last edited:
jmcilhinney said:
Let me get straight exactly what it is that you are trying to accomplish. Are you trying to allow the user to Tab to the next control by pressing a button? If this is the case, here's my advice:

Declare a variable of type Control at class level.
VB.NET:
Private lastFocusedControl As Control
Create an event handler for the LostFocus event and have it handle that event for every control on your form. You can specifically add each control to the Handles clause or you can write a procedure to find every control and add the event handler to them.
VB.NET:
Private Sub Button1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
	Me.lastFocusedControl = DirectCast(sender, Control)
End Sub
Notice that the event handler assigns the control that just lost focus to the lastFocusedControl variable.

In the Click event handler for your button, refocus the control that just lost focus and then send the TAB key press.
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	If Not Me.lastFocusedControl Is Nothing Then
		Me.lastFocusedControl.Focus()
		SendKeys.Send("{TAB}")
	End If
End Sub

Edit:
Obviously the event handler serves no purpose for controls that cannot be focused, like labels. You can exclude them if you like, and you should for "correctness", but including them will not hurt.


Thank you jmcilhinney.
I apologize for not being clear. What I want to do is to collect the values of the fields on the screenform ONE BY ONE when I click on the button. For instance, I have 5 fields on the form. starting from left to right field#1, field#2, field#3, field#4, field#5. And these fields are either textboxes or comboboxes. There are label fields as well to label those 5 fields above.
So, whenever I click on the button, PROGRAMATICALLY -- I want the SCRIPT to start collecting the values IN THE RIGHT ORDER starting with field#1 and then field#2..and so on to the last field which is field#5. I want to be able to have it JUMP in the RIGHT ORDER -- NOT collecting values from field#4 and then jump to field#2 OR START WITH FIELD#3 AND THEN JUMP BACK TO FIELD#1.
I have already set the tabindex property of each field starting 1(of field#1) from the most left to 5 (of field#5) on the right.
I hope I have made it clear for you. I really don't know how I can explain it any clearer. My english sucks I know.

Please help me. Please create a few fields on a form and test them out first because I am not sure there is a way to accomplish this task.
I will post this issue as resolved when we need to. I promise.

Dominico
 
If you don't actually want the focus to move from control to control then forget about SendKeys. As I said, it simply simulates a key press. It doesn't actually help you here unless you want to move the focus and then check which control has the focus. You need to use the method I suggested in post #4. I had intended that you would write the GetNextControl function yourself. As it turns out, the Control class already has a GetNextControl method that does exactly what you want. Check it out in the help.
 
Here's an idea....
Create a collection object (or a hashtable, doesn't really matter)
Loop through all controls on the form....
Check the TabIndex of the control.... if it is anything but 0, then add (option a) the control to the collection, using the TabIndex as the index for the item. Or (option b) add the text of the text box to the collection, again using the tab orderas the index value.
Once all controls are done, loop through the collection and get your values - in order.

Tg
 
TechGnome said:
Here's an idea....
Create a collection object (or a hashtable, doesn't really matter)
Loop through all controls on the form....
Check the TabIndex of the control.... if it is anything but 0, then add (option a) the control to the collection, using the TabIndex as the index for the item. Or (option b) add the text of the text box to the collection, again using the tab orderas the index value.
Once all controls are done, loop through the collection and get your values - in order.

Tg

Hi TechGnome,

Thank you for your help. I wish I knew how to do what you suggested. I don't know how to create a script for that. Could you send me an example or a website that can show me how to write it ?

Many thanks in advance.

dominico
 
jmcilhinney said:
If you don't actually want the focus to move from control to control then forget about SendKeys. As I said, it simply simulates a key press. It doesn't actually help you here unless you want to move the focus and then check which control has the focus. You need to use the method I suggested in post #4. I had intended that you would write the GetNextControl function yourself. As it turns out, the Control class already has a GetNextControl method that does exactly what you want. Check it out in the help.


Hi jmcilhinney,

I am using your code in post #4. It looks promising. However, it still doesn't pickup the values in the right order. Plus, the "do loop until currentcontrol.equal=firstcontrol" statement crashes on me all the time. Could you use that code and run it on a form with 4 fields on it ? It takes only a few minutes to create this test. Please check it out. I am desperate for your help.

Best,

dominico
 
I created a form with TextBox1, TextBox2, TextBox3, TextBox4 and Button1 in that tab order. The following code does what you want:
VB.NET:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim currentControl As Control = Me.TextBox1

		Do
			MessageBox.Show(currentControl.Name)
			currentControl = Me.GetNextControl(currentControl, True)
		Loop Until currentControl Is Nothing
	End Sub
Note that GetNextControl returns a null reference when it reaches the end of the tab order. It does not start again from the beginning, which is why the loop runs until the currentControl variable is Nothing. All you have to know is which is the first control in the tab order and assign it to currentControl at the start.

Also, the code calls Me.GetNextControl because it returns only children, grandchildren, etc. of the control on which it was called. This leads me to the fact that this code would work for nested controls as well, e.g. controls on Panels and in GroupBoxes.
 
Hi, take a look at the attached project.
Btw, it seems like you couldn't make it work cus you missed part that you should add handler in load event ... :)

Say this will be a handler

PHP:
Private Sub Enter_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
 
Dim xControl As Control 
 
For Each xControl In Me.Controls
 
If xControl.Name.StartsWith("Text") Then
 
If e.KeyCode = Keys.Enter Then
 
xControl.Text = "something"
 
SendKeys.Send("{TAB}")
 
End If
 
End If
 
Next
 
End Sub


in load event you need to add this in order to make it functional:

PHP:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
Dim xControl As Control 
 
For Each xControl In Me.Controls
 
AddHandler xControl.KeyDown, AddressOf Enter_KeyDown
 
Next
 
End Sub


Cheers ;)
 

Attachments

  • TabOrder.zip
    24.1 KB · Views: 25
Sorry Gentlemen,

I have been so busy working two jobs with many problems on hand. Didn't have time for testing. I really don't know what to say. you two are angels. I am going to test out both of your scripts. Please bear with me. I will post the results asap.

love you guys,

dominico
 
Jmcilhinney, Kulrom,

YOU GUYS DID IT!!!!

I use Jmcilhinney's code and it works. I am so happy, gentlemen. As for Kulrom's code, I am sure it will work too.

I live in San Diego. If you two happen to visit then please email me two days ahead. Allow me to do something in return to thank both of you for your greatness.

I will email you both my phone number.

dominico
 
Glad to help, but I would keep my phone number to myself if I was you. If we all offered so much each time someone helped us there'd be no time left for programming! Such enthusiastic gratitude is more than enough thanks, but... you did say the cheque was in the mail, right? ;)
 
jmcilhinney said:
Glad to help, but I would keep my phone number to myself if I was you. If we all offered so much each time someone helped us there'd be no time left for programming! Such enthusiastic gratitude is more than enough thanks, but... you did say the cheque was in the mail, right? ;)


jmcilhinney,

I am really grateful. Please stay in touch. I am really poor now but not for long. If you need money later on, email me. I can help out for sure. I am serious. Give me 5 more months.

Cheers,
 
I'm serious too. Save your money for an MCAD certification. It's a much more worthy cause, I assure you. Believe me, in five months time you'll be on to far bigger challenges than this.
 
jmcilhinney said:
I'm serious too. Save your money for an MCAD certification. It's a much more worthy cause, I assure you. Believe me, in five months time you'll be on to far bigger challenges than this.


MCAD is ? I am working two jobs now. I only have free time on weekends. I wish I knew more in VB.Net like you and Kulrom. It is okay after 5 months. Maybe I visit you in downunder. I like your country. I always want to go and visit it. Nice people there.

dominico
 
MCAD = Microsoft Certified Applications Developer

I've been developing for .NET for just over a year. I had been working as a C++ developer since 2000 though, so I wasn't completely inexperienced. Read, observe, experiment, practice, use the help system. Any word I see that I'm not sure of, the first thing I do is a help search.

Anyway, enough off-topic stuff. See you next thread. :)
 
jmcilhinney said:
MCAD = Microsoft Certified Applications Developer

I've been developing for .NET for just over a year. I had been working as a C++ developer since 2000 though, so I wasn't completely inexperienced. Read, observe, experiment, practice, use the help system. Any word I see that I'm not sure of, the first thing I do is a help search.

Anyway, enough off-topic stuff. See you next thread. :)


I have been learning .net for about 3 months through a book I bought and msdn that comes with vb.net. I will keep reading and learning it everyday. Maybe someday I can be productive like you in helping others.

Thanks for the advice. Take care my friend.

dominico
 
Back
Top