DataGrid Tabbing

D

Doug Bell

Hi
I have a DataGrid with some hidden columns and also some read Only and some
ComboBox Columns.

Sandard Tabbing through the Datagrid sees the focus go to the hidden columns
requiring further Tabbing to get to the desired column but it works fine
stopping correctly on the ComboBox column.

I have built a routine to test whether the use is Tabbing forward or back
(Shift Tab) by looking at the last location, not by trapping the Keys. If
tabbing (say forward) and it lands on a hidden (zero width) column or a Read
Only column, it sets the location explicitly to the next column and the
DataGrids CurrentCellChanged event fires to see if it needs to move further.

This works fine except when it gets to a ComboBox Column and there it opens
the ComboBox (correctly) but then moves on to the bext column (hidden) and
then on to the next column another Combo Box Column and stops.
Like wise if tabbing back it doesn't stop at the first Comb on the way back.

The code looks fine, I just can't see what is causing this behavior except I
found if I put a MsgBox in the code to halt it after it moves then it works
fine.

Also found that the code does not seem to flow as written. It steps through
correctly but when I use a series of Console.Writelines it looks a bit
strange.

Can anyone shed some light on this for me or suggest anything?

Here is my Tabbing code and the result of the Writelines:
______________________________________________________
Private iPrevCol As Integer

Private iPrevRow As Integer

____________________________________________________________

Private Sub GrdODs_CurrentCellChanged(ByVal sender As Object, _

ByVal e As System.EventArgs) _

Handles GrdODs.CurrentCellChanged

Console.WriteLine("Cell Changed -Previous Row/Col: " & iPrevRow & "/" &
iPrevCol)

TabThruGrid()

End Sub

____________________________________________

Private Sub TabThruGrid()

Dim dg As DataGrid = GrdODs

Dim cellItm As DataGridCell = dg.CurrentCell

Dim iCurrentRow As Integer = cellItm.RowNumber

Dim iCurrentCol As Integer = cellItm.ColumnNumber

Dim iLastRow, iLastCol As Integer

Dim fKeepTabbing As Boolean

Dim stTableName As String = dtOrdDetails.TableName.ToString

Dim iMove As Int16

Try

'fKeepTabbing = dg.TableStyles(stTableName). _

' GridColumnStyles(iCurrentColumn).Width = 0 Or _

' dg.TableStyles(stTableName). _

' GridColumnStyles(iCurrentColumn).ReadOnly()

'If fKeepTabbing Then

For iLastCol = _

dg.TableStyles(stTableName).GridColumnStyles.Count - 1 _

To 0 Step -1

'Find last visible Col

If dg.TableStyles(stTableName). _

GridColumnStyles(iLastCol).Width > 0 And _

Not dg.TableStyles(stTableName). _

GridColumnStyles(iLastCol).ReadOnly() Then

Exit For

End If

Next iLastCol

If iLastCol = 0 Then

Exit Sub

End If

iLastRow = dtOrdDetails.Rows.Count - 1

If (iCurrentCol > iPrevCol And iCurrentRow = iPrevRow) Or _

(iCurrentCol = 0 And iPrevCol = iLastCol And _

iCurrentRow = iPrevRow) Or _

(iCurrentCol = iLastCol And iCurrentRow > iPrevRow) Or _

(iCurrentCol = iLastCol And iPrevRow = iLastRow) Then

'Tab forward

iMove = 1

ElseIf (iCurrentCol < iPrevCol And iCurrentRow = iPrevRow) Or _

(iCurrentCol = 0 And iCurrentRow < iPrevRow) Or _

(iCurrentCol = 0 And iCurrentRow = 0 And _

iCurrentCol <> iPrevCol And iCurrentRow <> iPrevRow) Then

'Tab back

iMove = -1

Else

iMove = 0

End If

Console.WriteLine("Current Position (R/C)=" & iCurrentRow & "/" &
iCurrentCol)



If iMove = 1 Then 'If Tabbing forward

Console.WriteLine("Tab was forward")

If iCurrentCol = iLastCol Then 'If at last visible Col

Console.WriteLine("Col = Last Col")

If iCurrentRow = iLastRow Then 'If at last Row

Console.WriteLine("Row = LastRow")

'Move to first Col and first Row

fKeepTabbing = dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).Width = 0 Or _

dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).ReadOnly()

If fKeepTabbing Then

Console.WriteLine("Keep Tabbing, hidden Col or Read Only")

Console.WriteLine("Move to 0/0")

dg.CurrentCell = New DataGridCell(0, 0)

ButtonSave.Focus()

Console.WriteLine("Moved: 0 / 0")



End If 'fKeepTabbing

Else 'intCurrentRow = intLastRow

Console.WriteLine("Not at Last Row")

'Not at last Row so move to next Row and to Col 0

fKeepTabbing = dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).Width = 0 Or _

dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).ReadOnly()

If fKeepTabbing Then

Console.WriteLine("Keep Tabbing, hidden Col or Read Only")

Console.WriteLine("Move to " & iCurrentRow + 1 & "/0")

dg.CurrentCell = _

New DataGridCell(iCurrentRow + 1, 0)

Console.WriteLine("Moved: iCurrentRow - 1 / 0")

End If 'fKeepTabbing

End If 'intCurrentRow = intLastRow

Else 'intCurrentRow = intLastRow

'Not at last Col

'So move to next Col

Console.WriteLine("Not at Last Col")

fKeepTabbing = dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).Width = 0 Or _

dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).ReadOnly()

If fKeepTabbing Then

Console.WriteLine("Keep Tabbing, hidden Col or Read Only")

Console.WriteLine("move to " & iCurrentRow & "/" & iCurrentCol + 1)

dg.CurrentCell = _

New DataGridCell(iCurrentRow, iCurrentCol + iMove)

Console.WriteLine("Moved: iCurrentRow - 1 / iLastColumn")

End If 'Not fKeepTabbing

End If 'intCurrentRow = intLastRow

ElseIf iMove = -1 Then 'If Tabbing back

Console.WriteLine("Move was backward")

If iCurrentCol = 0 Then 'If at 1st Col

Console.WriteLine("At 1st Col")

If iCurrentRow = 0 Then 'If at 1st Row

Console.WriteLine("At 1st Row")

'Move to Last Row

fKeepTabbing = dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).Width = 0 Or _

dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).ReadOnly()

If fKeepTabbing Then

Console.WriteLine("Keep Tabbing, hidden Col or Read Only")

Console.WriteLine("move to " & iLastRow & "/" & iLastCol)

dg.CurrentCell = _

New DataGridCell(iLastRow, iLastCol)

Console.WriteLine("Moved: iLastRow / iLastColumn")

End If 'fKeepTabbing

Else 'intCurrentRow = 0

'Not at 1st Row

Console.WriteLine("Not at 1st Row")

'Move back 1 Row to the last Col

fKeepTabbing = dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).Width = 0 Or _

dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).ReadOnly()

If fKeepTabbing Then

Console.WriteLine("Keep Tabbing, hidden Col or Read Only")

Console.WriteLine("move to " & iCurrentRow - 1 & "/" & iCurrentCol)

dg.CurrentCell = _

New DataGridCell(iCurrentRow - 1, iCurrentCol)

Console.WriteLine("Moved: iCurrentRow - 1 / iLastColumn")

End If 'fKeepTabbing

End If 'intCurrentRow = 0

Else 'intCurrentColumn = 0

Console.WriteLine("Not at 1st Col")

'Not at 1stCol

fKeepTabbing = dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).Width = 0 Or _

dg.TableStyles(stTableName). _

GridColumnStyles(iCurrentCol).ReadOnly()

If fKeepTabbing Then

Console.WriteLine("Keep Tabbing, hidden Col or Read Only")

Console.WriteLine("Move to " & iCurrentRow & "/" & iCurrentCol + iMove)

dg.CurrentCell = _

New DataGridCell(iCurrentRow, iCurrentCol + iMove)

Console.WriteLine("Moved: iCurrentRow / iCurrentColumn + (iMove):" & iMove)

End If 'fKeepTabbing

End If 'intCurrentColumn = 0

End If 'If iMove = 1 ie If tabbing

'Else 'fKeepTabbing

'Console.WriteLine("-Col:" & iCurrentColumn & " -" & Now & ": -Return ")

'dacSetFormCntls()

'End If 'fKeepTabbing ie If Cell Hidden

TextBoxCol.Text = iPrevRow & "; " & iPrevCol

Catch

dg.CurrentCell = New DataGridCell(0, 0)

Console.WriteLine("Error: -Col:" & iCurrentCol & " -" & Now)

Console.WriteLine(" Move to 0/0")

End Try

' Set Previous Row and Col

cellItm = dg.CurrentCell

iPrevRow = cellItm.RowNumber

iPrevCol = cellItm.ColumnNumber

Console.WriteLine("Prev Row/Col =" & iPrevRow & "/" & iPrevCol)

End Sub
 
C

ClayB

Not sure how you are putting the combobox in your datagrid, but try
this if possible. Derive the ComboBox class you are using, override
its WndProc method and ignore the keyup. This is how the first VB
sample link in this FAQ entry avoid the same problem you are
describing. Without this code, as you tab through the grid, on keydown
your tab into the combobox cell, and on keyup you tab out of the
cell.

http://www.syncfusion.com/faq/windowsforms/search/480.aspx


Public Class NoKeyUpCombo
Inherits ComboBox
Private WM_KEYUP As Integer = &H101


Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
If m.Msg = WM_KEYUP Then
'ignore keyup to avoid problem with tabbing &
dropdownlist;
Return
End If
MyBase.WndProc(m)
End Sub 'WndProc
End Class 'NoKeyUpCombo

================
Clay Burch
Syncfusion, Inc.
 
D

Doug Bell

Hi Thanks Clay,

I am, as you suggested deriving the ComboBox and consuming the KeyUp and if
I Tab through the DataGrid without any code to Tab through the Hidden and
Read Only Cells it Tabs correctly into the Combo Box.

With the code to Tab past the Hidden and Read Only Cells, which works
perfectly on Text Box Cells, it Tabs in and then out again.

If I put a MsgBox in the code or even if I step through the code it works
perfectly but when I let it run unabated it tabs in and out again.

Doug
 
D

Doug Bell

Thanks Clay,
Your sample works very well.
I used your sample sub
Sub dataGrid1_CurrentCellChanged

in my project and was still seeing the same problem.

I will look into my DataGridCombo code and ite inheritance.

Again Thanks


Doug
 
D

Doug Bell

Clay,
I think I may have found where the problem is coming from.
My ComboBoxes ComboBoxStyle = DropDown not DropDownList.

When I changed your Style to DropDown I saw the problem on your Sample as
well.

I need to have them as dropdown so that the User can type the value into the
combo to select the value.
Actually they will be Locations so
2BMovd;A01;A02;A03;AFloor;A04; B01;B02 etc

They don't want to select with the mouse or type "A" repeatedly to cylce
through all the "A" locations. So I had to set up the Combo as a DropDown
and then the Tabbing problem occurs.

If you have any suggestions to resolve it while keeping the DropDown Style I
would really appreciate it.
 
C

ClayB

I was able to get the sample working by using PreProcessMessage to
ignore the KEYUP instead of WndProc.

Public Overrides Function PreProcessMessage(ByRef msg As
System.Windows.Forms.Message) As Boolean
If msg.Msg = WM_KEYUP Then
Return True
End If
Return MyBase.PreProcessMessage(msg)
End Function

===================
Clay Burch
Syncfusion, Inc.
 

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