Skip to content

Commit 9887aa8

Browse files
authored
Merge pull request #16 from Xcone/develop
Left, Right and Enter behavior for WinForms and WPF
2 parents 4817eee + 40d1824 commit 9887aa8

File tree

2 files changed

+88
-61
lines changed

2 files changed

+88
-61
lines changed

ReoGrid/WPF/WPFControl.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -952,27 +952,42 @@ protected override void OnLostFocus(RoutedEventArgs e)
952952
}
953953
base.OnLostFocus(e);
954954
}
955+
955956
protected override void OnPreviewKeyDown(KeyEventArgs e)
956957
{
957958
var sheet = this.Owner.CurrentWorksheet;
958959

959960
// in single line text
960961
if (!TextWrap && Text.IndexOf('\n') == -1)
961962
{
963+
Action moveAction = null;
964+
962965
if (e.Key == Key.Up)
963966
{
964-
sheet.EndEdit(Text);
965-
sheet.MoveSelectionUp();
966-
e.Handled = true;
967+
moveAction = () => sheet.MoveSelectionUp();
967968
}
968969
else if (e.Key == Key.Down)
969970
{
970-
sheet.EndEdit(Text);
971-
sheet.MoveSelectionDown();
972-
e.Handled = true;
973-
}
971+
moveAction = () => sheet.MoveSelectionDown();
972+
}
973+
else if (e.Key == Key.Left && SelectionStart == 0)
974+
{
975+
moveAction = () => sheet.MoveSelectionLeft();
976+
}
977+
else if (e.Key == Key.Right && SelectionStart == Text.Length)
978+
{
979+
moveAction = () => sheet.MoveSelectionRight();
980+
}
981+
982+
if (moveAction != null)
983+
{
984+
sheet.EndEdit(Text);
985+
moveAction();
986+
e.Handled = true;
987+
}
974988
}
975989
}
990+
976991
protected override void OnKeyDown(KeyEventArgs e)
977992
{
978993
var sheet = this.Owner.CurrentWorksheet;

ReoGrid/WinForm/WinFormControl.cs

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*****************************************************************************
2-
*
2+
*
33
* ReoGrid - .NET Spreadsheet Control
4-
*
4+
*
55
* http://reogrid.net/
66
*
77
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright (c) 2012-2016 Jing <lujing at unvell.com>
1515
* Copyright (c) 2012-2016 unvell.com, all rights reserved.
16-
*
16+
*
1717
****************************************************************************/
1818

1919
#if WINFORM
@@ -263,15 +263,15 @@ public ReoGridControl()
263263
BackColor = SystemColors.Control,
264264
TabStop = false,
265265
});
266-
266+
267267
Controls.Add(bottomPanel);
268268

269269
#endregion // Bottom Panel
270-
270+
271271
this.InitControl();
272272

273273
ResumeLayout();
274-
274+
275275
//TODO: detect clipboard changes
276276
// need detect and remove the hightlight range when content has been removed from System Clipboard
277277
//ClipboardMonitor.Instance.ClipboardChanged += new EventHandler<ClipboardChangedEventArgs>(ClipboardMonitor_ClipboardChanged);
@@ -305,11 +305,11 @@ void canvasElements_VisibleChanged(object sender, EventArgs e)
305305
this.currentWorksheet.UpdateViewportControllBounds();
306306
}
307307
}
308-
308+
309309
/// <summary>
310310
/// Release resources used in this component.
311311
/// </summary>
312-
/// <param name="disposing">True to release both managed and unmanaged resources;
312+
/// <param name="disposing">True to release both managed and unmanaged resources;
313313
/// False to release only unmanaged resources.</param>
314314
protected override void Dispose(bool disposing)
315315
{
@@ -329,7 +329,7 @@ protected override void Dispose(bool disposing)
329329
if (builtInFullColSelectCursor != null) builtInFullColSelectCursor.Dispose();
330330
if (builtInFullRowSelectCursor != null) builtInFullRowSelectCursor.Dispose();
331331
}
332-
332+
333333
#endregion // Constructor
334334

335335
#region Adapter
@@ -437,7 +437,7 @@ public void ChangeCursor(CursorStyle cursor)
437437
case CursorStyle.Selection: this.control.Cursor = this.control.internalCurrentCursor; break;
438438
case CursorStyle.Busy: this.control.Cursor = Cursors.WaitCursor; break;
439439
case CursorStyle.Hand: this.control.Cursor = Cursors.Hand; break;
440-
case CursorStyle.FullColumnSelect:
440+
case CursorStyle.FullColumnSelect:
441441
this.control.Cursor = this.control.FullColumnSelectionCursor!=null ?
442442
this.control.FullColumnSelectionCursor : this.control.builtInFullColSelectCursor;
443443
break;
@@ -457,7 +457,7 @@ public void ChangeCursor(CursorStyle cursor)
457457
case CursorStyle.Cross: this.control.Cursor = this.control.builtInCrossCursor; break;
458458
}
459459
}
460-
460+
461461
public void RestoreCursor()
462462
{
463463
if (this.oldCursor != null)
@@ -894,7 +894,7 @@ void sheetMenuItem_Click(object sender, EventArgs e)
894894
this.CurrentWorksheet = sheet;
895895
}
896896
}
897-
897+
898898
private void ShowSheetTabControl()
899899
{
900900
if (!this.bottomPanel.Visible)
@@ -1028,46 +1028,58 @@ protected override void OnCreateControl()
10281028
{
10291029
sf = new StringFormat(StringFormat.GenericDefault);
10301030
}
1031-
protected override void OnKeyDown(KeyEventArgs e)
1032-
{
1033-
var sheet = owner.currentWorksheet;
1034-
1035-
if (sheet.currentEditingCell != null && Visible)
1036-
{
1037-
bool isProcessed = false;
1038-
1039-
// in single line text
1040-
if (!TextWrap && Text.IndexOf('\n') == -1)
1041-
{
1042-
if (e.KeyCode == Keys.Up)
1043-
{
1044-
e.SuppressKeyPress = true;
1045-
sheet.EndEdit(Text);
1046-
sheet.MoveSelectionUp();
1047-
isProcessed = true;
1048-
}
1049-
else if (e.KeyCode == Keys.Down)
1050-
{
1051-
e.SuppressKeyPress = true;
1052-
sheet.EndEdit(Text);
1053-
sheet.MoveSelectionDown();
1054-
isProcessed = true;
1055-
}
1056-
}
1057-
1058-
if (!isProcessed)
1059-
{
1060-
if (!Toolkit.IsKeyDown(Win32.VKey.VK_CONTROL) && e.KeyCode == Keys.Enter)
1061-
{
1062-
e.SuppressKeyPress = true;
1063-
sheet.EndEdit(Text);
1064-
sheet.MoveSelectionForward();
1065-
}
1066-
}
1067-
}
1068-
}
1069-
1070-
protected override bool ProcessCmdKey(ref Message msg, System.Windows.Forms.Keys keyData)
1031+
protected override void OnKeyDown(KeyEventArgs e)
1032+
{
1033+
var sheet = owner.currentWorksheet;
1034+
1035+
if (sheet.currentEditingCell != null && Visible)
1036+
{
1037+
bool isProcessed = false;
1038+
1039+
// in single line text
1040+
if (!TextWrap && Text.IndexOf('\n') == -1)
1041+
{
1042+
isProcessed = true;
1043+
if (e.KeyCode == Keys.Up)
1044+
{
1045+
ProcessSelectionMoveKey(e, sheet, () => sheet.MoveSelectionUp());
1046+
}
1047+
else if (e.KeyCode == Keys.Down)
1048+
{
1049+
ProcessSelectionMoveKey(e, sheet, () => sheet.MoveSelectionDown());
1050+
}
1051+
else if (e.KeyCode == Keys.Left && SelectionStart == 0)
1052+
{
1053+
ProcessSelectionMoveKey(e, sheet, () => sheet.MoveSelectionLeft());
1054+
}
1055+
else if (e.KeyCode == Keys.Right && SelectionStart == Text.Length)
1056+
{
1057+
ProcessSelectionMoveKey(e, sheet, () => sheet.MoveSelectionRight());
1058+
}
1059+
else
1060+
{
1061+
isProcessed = false;
1062+
}
1063+
}
1064+
1065+
if (!isProcessed)
1066+
{
1067+
if (!Toolkit.IsKeyDown(Win32.VKey.VK_CONTROL) && e.KeyCode == Keys.Enter)
1068+
{
1069+
ProcessSelectionMoveKey(e, sheet, () => sheet.MoveSelectionForward());
1070+
}
1071+
}
1072+
}
1073+
}
1074+
1075+
private void ProcessSelectionMoveKey(KeyEventArgs e, Worksheet sheet, Action moveAction)
1076+
{
1077+
e.SuppressKeyPress = true;
1078+
sheet.EndEdit(Text);
1079+
moveAction();
1080+
}
1081+
1082+
protected override bool ProcessCmdKey(ref Message msg, System.Windows.Forms.Keys keyData)
10711083
{
10721084
var sheet = owner.currentWorksheet;
10731085

@@ -1300,7 +1312,7 @@ protected override void WndProc(ref Message m)
13001312
return;
13011313
}
13021314
else
1303-
// Chinese and Japanese IME will send this message
1315+
// Chinese and Japanese IME will send this message
13041316
// before start to accept user's input
13051317
if (m.Msg == (int)Win32.WMessages.WM_IME_STARTCOMPOSITION)
13061318
{
@@ -1336,7 +1348,7 @@ protected override void OnResize(EventArgs e)
13361348
{
13371349
this.currentWorksheet.UpdateViewportControllBounds();
13381350
}
1339-
1351+
13401352
base.OnResize(e);
13411353

13421354
if (this.sheetTab.Right > this.bottomPanel.Width - 40)

0 commit comments

Comments
 (0)