Focus problem

S

Simone

Hello.

I have a form with a TabControl inside. In the first TabPage i have 4
TextBox. In the second TabPage i ha a Grid.
The problem is this:
I'm in the first TabPage on the second TextBox field with the cursor. Then i
tap with the pen on the second TabPage and tap on the grid. When i return on
the first TabPage with the pen i would return on the second TextBox with the
focus (cursor).

I'm in trouble with that..

Can anyone help me?

Thanks.
 
C

chris.m.oswald

I'm not sure I totally understood what you said, but it sounds like you
want to remember the last focus position on your previous tab page.

You can catch the TabControl SelectedIndexChanged Event and determine
what page is being selected. Based on the page being selected you can
set focus to the last control

You will have to catch the lostfocus event for each control on the
first tab page and store it in a form level variable. Perhaps called
_tabPageOneControl. Each time the focus is lost you store the
variable.

Here's the code.

Dim _tabPageOneControl As Control
Dim _tabPageTwoControl As Control

Private Sub ControlPage1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.LostFocus, TextBox2.LostFocus
_tabPageOneControl = CType(sender, Control)
End Sub

Private Sub ControlPage2_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox3.LostFocus
_tabPageTwoControl = CType(sender, Control)
End Sub

Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
If (TabControl1.SelectedIndex = 0) Then
If (Not _tabPageOneControl Is Nothing) Then
_tabPageOneControl.Focus()
ElseIf (TabControl1.SelectedIndex = 1) Then
If (Not _tabPageTwoControl Is Nothing) Then
_tabPageTwoControl.Focus()
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top