Is it Microsoft bug or mine?

Posts   
 
    
Developer
User
Posts: 58
Joined: 05-May-2004
# Posted on: 12-Apr-2005 08:46:37   

Hi,

UserControl has a bug to process Tab key. I need to bypass Tab keys for ReadOnly textboxes (There is a complex usercontrol with a grid, but I make a sample to make sure usercontrol has a issue.)

Sample Project Download: http://203.26.24.80/kakaiya/vbnet1.zip

It works great with TextBox on the left side, but it doesn't work with right side UserControl contains textbox. User needs to press Tab key twice in some cases.

I am using Visual Studio 2003 [Framework 1.1 + SP1].

if anyone knows solution/alternative, please let me know.

Thanks in advance.

Regards,

Developer

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 12-Apr-2005 12:54:25   

Hi,

Normally, controls do not receive dialog characters, Tab, Return, Esc, arrows key.

To receive this dialog character do the following:

Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean

    If keyData = Windows.Forms.Keys.Tab Then

        ' do your processing here
        Return True
    Else
        Return False
    End If

End Function

Developer
User
Posts: 58
Joined: 05-May-2004
# Posted on: 12-Apr-2005 13:59:59   

Hi,

Thanks for your suggestion. I have already implemented ProcessDialogKey function into my complex UserControl with a grid. However I removed those codes & grids from the sample project to make it simple. simple_smile

http://203.26.24.80/kakaiya/vbnet1.zip

I really appreciate, if you can download and play around to implement ProcessDialogKey function into sample project and let me know if it works! It doesn't work on my PC cry

Thanks again. Any other ideas/suggestion?

Kind Regards.

Developer

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 12-Apr-2005 16:21:01   

Hi,

It seems that you want to avoid the usercontrol to receive focus when it is in ReadOnly mode and the Tab key is pressed.

In my usercontrols, I have done the following:


Private _nextControl As Control
Private _readOnly As Boolean = False

Public Property [ReadOnly]() As Boolean
        Get
            Return _readOnly
        End Get
        Set(ByVal Value As Boolean)

            _readOnly = Value
            Me.txtCodigo.ReadOnly = Value
        
            If Value Then
                Me.FocusNextControl()
            End If
        End Set
End Property

 <Browsable(False)> _
 Public Property NextControl() As Control
        Get
            Return _nextControl
        End Get
        Set(ByVal Value As Control)
            _nextControl = Value
        End Set
 End Property


Protected Sub FocusNextControl()
        If Not Me._nextControl Is Nothing Then
            Me._nextControl.Focus()
        End If
End Sub

Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        If Me._readOnly Then
            Me.FocusNextControl()
        Else
            MyBase.OnEnter(e)
        End If
End Sub



Then I set myusercontrol's NextControl property in the load event of the form where I use myusercontrol.

Developer
User
Posts: 58
Joined: 05-May-2004
# Posted on: 13-Apr-2005 03:53:57   

Hi Rogelio,

Thanks for your help.

Rogelio wrote:


Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        If Me._readOnly Then
            Me.FocusNextControl()
        Else
            MyBase.OnEnter(e)
        End If
End Sub

Then I set myusercontrol's NextControl property in the load event of the form where I use myusercontrol.

If I add OnEnter event for usercontrol, it does not get fired sometimes, due to focus is not moving to next UserControl (user needs to press Tab key twice to move focus???).

As it doesn't fire the focus event, your code does not work. Also, you might notice that form creates dynamic controls, so it might be difficult to track NextControl property in my complex usercontrol.


        Private Sub PanelEditor_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Enter
            Debug.WriteLine("PanelEditor_Enter")
        End Sub

To make Tab key work properly, If I change

 Inherits System.Windows.Forms.UserControl

to

 Inherits System.Windows.Forms.Panel

It works!!! but it's not a correct way, it does not have couple of events (Load) and nice designer.

Any other ideas/suggestion?

Kind Regards.

Developer

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 13-Apr-2005 05:06:10   

Hi,

Try overriding the Sub OnEnter, as my sample code.

Developer
User
Posts: 58
Joined: 05-May-2004
# Posted on: 13-Apr-2005 05:44:17   

Hi,

I override OnEnter as you specified, but it doesn't work (same behaviour as enter event). frowning

This behaviour (twice Tab keys) only happens, when previous usercontrol is readonly and by-pass it's focus to next control using SelectNextControl.

I just notice that if I mouse click on Readonly usercontrol, it set focus to next control and I need to press Tab twice to go out. If I mouse click on non-Readonly usercontrol, it set focus and I need to press Tab once to go out. Do you think internally it doesn't bypass it's focus to next control ??


    Public Overloads Function SelectNextControl(ByVal ctl As Windows.Forms.Control) As Boolean
        If Not ctl Is Nothing Then
            Return MyBase.SelectNextControl(ctl, True, True, True, True)
        End If
    End Function

Also, How come it's working with inherits Panel instead of Usercontrol? I really stuck on this UserControl issue.

Could you please download sample http://203.26.24.80/kakaiya/vbnet1.zip and send me updated copy at mitalkakaiya@hotmail.com ?

I do really appreciate your help. smile

Regards, Developer