[Gambas-user] NoTabFocus glitch when pressing shift (usercontrols)
Bruce Steers
bsteers4 at gmail.com
Tue Mar 9 14:03:01 CET 2021
hehe , I wasn't going to say anything about the first example but i was
thinking "well how are you handling .Enabled and things then?"
;)
I got my routine tiny.
It's lazy i know and more memory hungry as it simply runs through
Me.Window.Controls and builds a list of all the focusable controls
and selects prev/next or first/last . so if there are lots of controls it
might be an issue. I plan to do that a bit better like you have :)
But like i said, the finding the prev/next control was not my problem.
turns out setting .Focus on an object hides .NoTabFocus from the native Tab
routine.
Now i have made it not set .Focus if .NotabFocus the Tab / Shift+Tab
buttons work completely as expected without me adding them to my key
handler. i have just the arrow keys in there now.
Cheers me old mucka :)
BruceS
Public Sub NextFocus(Optional bReverse As Boolean)
Dim ctList As New Control[], m, c As Integer, cName As String
' make a list of all the enabled/visible/focusable controls
For Each ct As Control In Me.Window.Controls
cName = Object.Class(ct).Parent.Name
If cName <> "Control" And cName <> "UserControl" Then Continue
If (ct.NoTabFocus) Or (Not ct.Enabled) Or (Not ct.Visible) Then Continue
ctList.Add(ct)
If ct = Me Then m = c ' m is Index of this control
Inc c
Next
' select prev/next or first/last
If Not bReverse Then
ctList[IIf(m = c - 1, 0, m + 1)].SetFocus
Else
ctList[IIf(m = 0, c - 1, m - 1)].SetFocus
Endif
End
On Tue, 9 Mar 2021 at 07:59, Bruce <adamnt42 at gmail.com> wrote:
> On 9/3/21 10:12 am, Bruce wrote:
> > On 9/3/21 7:22 am, Bruce Steers wrote:
> >> This is weird.
> >>
> >> I I have a form full of custom usercontrols and have set NoTabFocus on
> >> one.
> >>
> >> If i let the GUIs handle Tab focus then they get selected despite the
> >> NoTabFocus flag.
> >>
> >> So i have written my own focus routine and grab the key events
> >>
> >> using up and down arrow and tab works fine and skips the NoTabFocus
> >> object.
> >>
> >> Using Shift + Tab selects it :(
> >>
> >> Using Shift and Tab uses the exact same command as Up arrow but up arrow
> >> works.
> >> I am calling Stop Event to cancel Tab and Shift+Tab press anywhere else.
> >>
> >> Odd how Tab works but shift + Tab does not.
> >>
> >> Project is here...
> >> https://gitlab.com/bsteers4/ncheckbox
> >>
> >> BruceS
> >>
> >>
> >>
> >> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
> >>
> > Wade through the following and see if you can find something in it. It
> > works here for all our custom controls.
> >
> > '' [EVT] Handler for the navigation keys
> > Public Sub SELF_KeyPress()
> >
> > Dim hTarget As Control
> >
> > ' 1) Make [Enter] emulate a [Tab]
> > If Key.Code = Key.Return Then
> > ' a) figure out where to start looking
> > If Key.Shift Then
> > If Me.Previous Then
> > hTarget = Me.Previous
> > Else
> > hTarget = Me.Parent.Children[Me.Parent.Children.Count - 1]
> > Endif
> > Else
> > If Me.Next Then
> > hTarget = Me.Next
> > Else
> > hTarget = Me.Parent.Children[0]
> > Endif
> > Endif
> > ' b) find the next viable control
> > While hTarget.NoTabFocus
> > If Key.Shift Then
> > hTarget = hTarget.Previous
> > If Not hTarget Then hTarget =
> > Me.Parent.Children[Me.Parent.Children.Count - 1]
> > Else
> > hTarget = hTarget.Next
> > If Not hTarget Then hTarget = Me.Parent.Children[0]
> > Debug hTarget.Name
> > Endif
> > Wend
> > ' c) go there
> > hTarget.SetFocus
> >
> > Endif
> >
> > rgrds
> > bruce
>
> Sorry - that was totally the wrong routine.
> Look through tis one.
>
> '' Resolves the previous "real" control to tab to (Possibly no longer
> required)
> Public Function PrevActiveControl(cControl As Control) As Control
>
> Dim tControl As Control
> Dim tContainer As Container
> ' Dim isContainer As Boolean
>
> If IsNull(cControl.Previous) Then
> ' Print "No Prev for " & cControl.Name
> If Not IsNull(cControl.Parent) Then
> ' Print "Has parent so try " & cControl.Parent.Name
> Return PrevActiveControl(cControl.Parent)
> Else
> ' Print "No parent (EOF?) so try " &
> cControl.Window.Children[cControl.Window.Children.Count - 1].Name
> tControl =
> cControl.Window.Children[cControl.Window.Children.Count - 1]
> Endif
> Else
> ' Print "Got a target " & cControl.Previous.Name
> tControl = cControl.Previous
> Endif
>
> While tControl Is Container
> ' Print "Target is a container, so try last child " &
> tContainer.Children[tContainer.Children.Count - 1].Name
> tContainer = tControl
> If tContainer.Children.Count > 0 Then
> tControl = tContainer.Children[tContainer.Children.Count - 1]
> Else
> Return PrevActiveControl(tControl)
> Endif
> Wend
>
> If Not tControl.Enabled Then Return PrevActiveControl(tControl)
> If Not tControl.Visible Then Return PrevActiveControl(tControl)
> If tControl Is PictureBox Then Return PrevActiveControl(tControl)
> If tControl Is Label Then Return PrevActiveControl(tControl)
>
> ' Print "Found next control " & tControl.Name
> Return tControl
>
> End
>
> It is called from the control _KeyPress() handler as per the following
> example.
>
> If Key.Code = Key.Tab Then
> Stop Event
> InnerSpinBox_Activate
> If Key.Shift Then
> MUtil.PrevActiveControl(Me).SetFocus
> Else
> MUtil.NextActiveControl(Me).SetFocus
> Endif
> Endif
>
>
> rdrds
> bruce
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210309/2601701d/attachment-0001.htm>
More information about the User
mailing list