StrataFrame Forum

Ultrawingrid - handling keypress and setting focus to row on enter

http://forum.strataframe.net/Topic27195.aspx

By Charles R Hankey - 5/20/2010



Two Ultrawingrid issues :



What is the best way to cause the focus to go to the first column of the first row when the grid is entered via tabbing from the previous control. i.e. so you can immediately start typing or tabbing through the gird.



How does one handle the keypress so that a ctrl-Tab will exit the grid and move to the next control in the TOController tab order.?



Thanks



( if you know the answer and could email it to me I'd appreciate it as i may need it before the forum goes down for maintenance)


By Ivan George Borges - 5/20/2010

Hiya.

This is to Exit and Set Focus to next control. I haven't been able to catch Ctrl+Tab. If you manage it, please let me know:

    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.E AndAlso e.Control Then
            SetFocusToNextControl(True)
        End If

        MyBase.OnKeyDown(e)
    End Sub

And this is to enter in Edit mode:

    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        Me.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.ActivateCell)
        Me.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.EnterEditMode)

        MyBase.OnEnter(e)
    End Sub

Hope it helps. Wink

By Charles R Hankey - 5/20/2010

Super ! Thanks Ivan. I'll try these tonight.



SetFocustonextControl(true) is really new to me but i thought there had to be something like that available.
By Charles R Hankey - 5/20/2010

Sorry. I'm being a little stupid. i tried putting this in my subclass



Imports Infragistics

Imports Infragistics.Shared

Imports System.Windows.Forms



Public Class uWingrid

Inherits Infragistics.Win.UltraWinGrid.UltraGrid



Public Sub New()





Me.DisplayLayout.CaptionVisible = Win.DefaultableBoolean.False

Me.DisplayLayout.GroupByBox.Hidden = True

Me.DisplayLayout.Override.AllowAddNew = Infragistics.Win.UltraWinGrid.AllowAddNew.FixedAddRowOnBottom

Me.DisplayLayout.Override.AllowDelete = Infragistics.Win.DefaultableBoolean.[True]

Me.DisplayLayout.Override.AllowRowSummaries = Infragistics.Win.UltraWinGrid.AllowRowSummaries.[False]

Me.DisplayLayout.Override.AllowUpdate = Infragistics.Win.DefaultableBoolean.[True]





End Sub

Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)

If e.KeyCode = Keys.E AndAlso e.Control Then

SetFocusToNextControl(True)

End If

MyBase.OnKeyDown(e)

End Sub



Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)

Me.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.ActivateCell)

Me.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.EnterEditMode)



MyBase.OnEnter(e)

End Sub





End Class





The two sub don't seem to be event handlers and they don't appear to be being called.



If I have a form with 13 ultragrids, where would I put the subs ? Or f I wanted this behavior for a single grid? It looks like the code should be in a subclass, but I think I'm missing something about subclassing the grid ....?



I guess my brain isn't working today.
By Charles R Hankey - 5/20/2010

My apologies. I should have studied your excellent Ultragrid sample app before posting. I am going to study that, fix my subclass and try it again. I think I am starting to understand.



Thanks for the sample !


By Ivan George Borges - 5/21/2010

Hi Charles.

SetFocustonextControl(true) is an UltraGrid method, so it will work fine inside an inherited UltraGrid. But then, you want to use Ctrl+Tab to exit the UltraGrid, which I agree is the easier thing for the user to understand, but I wasn't able to catch the keydown Ctrl+Tab from inside the UltraGrid.

In order to do that, I used my BaseForm and added the following code:

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, _
                                               ByVal keyData As System.Windows.Forms.Keys) As Boolean
        '-- check for unique keystrokes
        Select Case keyData
            Case Keys.Control Or Keys.Tab
                '-- created to be able to tab out of a Grid control
                Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
        End Select

        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

This worked fine for me, since all forms will tab to next control once the user types Ctrl+Tab.

Regarding the Subs, they are overriding the grid's OnKeyDown (which we don't need anymore since we are testing the ProcessCmdKey in our base form) and OnEnter subs. That is why we call the MyBase.OnEnter(e) (remember DODEFAULT()? Smile)

Thanks for the question, I wanted to write these features for myself as well. BigGrin

By Ivan George Borges - 5/21/2010

By the way, I have moved the OnEnter code to the OnGotFocus:

    Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
        Me.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.ActivateCell)
        Me.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.EnterEditMode)

        MyBase.OnGotFocus(e)
    End Sub

By Charles R Hankey - 5/26/2010

Hi Ivan



I've been working a lot using your ultraWingrid subclassing example.



First, in order to exit the grid using Ctrl - Tab :



If e.KeyCode = Windows.Forms.Keys.Tab AndAlso e.Control Then

SetFocusToNextControl(True)

End If



If e.KeyCode = Windows.Forms.Keys.Tab AndAlso e.Control AndAlso e.Shift Then



SetFocusToNextControl(False)

End If



If e.KeyCode = windows.Forms.Keys.Delete andalso e.Control then

Me.currentActiveRow.Delete()

End If

MyBase.OnKeyDown(e)




But the weird thing I have discovered is that if you use a SF TabOrderController on the form the tab in the grid will move to the next control (just as ctrl-tab does in the example above) and not tab you through the grid and into add record at the end.



I've made minor mods to your sample to demonstrate both of these things. As delivered, you'll see tabbing from country into the grid and then tabbing moves through the grid until you exit with either Ctrl-Tab or Ctrl-Shift-Tab.



But now drop a SF TabOrderController on the form, set the properties for the three controls and notice tabbing when in the grid moves your to postal code.



Obviously, what we need is a way to tell the TabOrderController to ignore the Tab if the container is an ultrawingrid.



Any ideas?




By Ivan George Borges - 5/26/2010

Hi Charles.

I have been fighting the UltraGrid for days now! w00t

Getting it to do exactly what I want has been a challenge, but so far I am managing to do it. "Someone" has always told me how working with grids was a pain in the neck...

I will have a look at your sample and see if I can come back with some idea. Wink

By Ivan George Borges - 5/26/2010

Here you are, Charles.

Basicaly, I dropped a GroupBox on the form and moved the grid inside it. Then I rearranged the form Tab order, and set the GroupBox "TabOrder on TabOrderController1" to 2, or whatever gets in the middle of the other 2 controls before and afterhand. Next, I added code to the GroupBox_Enter event to move the focus to the grid:

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                Handles GroupBox1.Enter
        '-- move focus to grid
        Me.grdCustomers.Focus()
    End Sub

And, for the Grid "TabOrder on TabOrderController1", I set -1, so it will tab inside it freely.

Hope it helps. Wink

By Charles R Hankey - 5/26/2010

Clever idea. I'll have a look at it tonight.



Obrigado again ! Smile
By Charles R Hankey - 5/27/2010

Ivan, thank you so much for the groupbox trick to get the TabOrderController to work with Ultrawingrid. Works beutifully and is very easy to implement. The ctrl-tab and shift-ctrl-tab still work as designed.



For benefit of lurkers ( and me when I forget what I did ) :



When using a TabControl and multiple pages, you can tab from last control on page1 to first control on page2 by using the Leave event of the page1 control to set the SelectedTab() of the TabControl to page2 and then, in the Enter of page2 set the focus to the next control in the tab sequence.
By Ivan George Borges - 5/27/2010

Glad it helped, Charles! Wink