diff UI/EquipmentAmountControl.cs @ 114:727c1b0e49a6

Fixes #115: Typing number for equipment amount doesn't update Okay button * Remove KeyDown capture code (AcceptButton functionality captures the key press first) * Add ProcessDialogKey method that uses ValueChanged helper class to determine whether the control changes when we force an update New functionality is that the values can be typed and changed by pressing Enter, then a second Enter will accept the changes and close the dialog. If no changes were made then pressing Enter once accepts changes and closes the dialog.
author IBBoard <dev@ibboard.co.uk>
date Thu, 24 Dec 2009 12:04:32 +0000
parents c1a3993297b1
children e00332dc1618
line wrap: on
line diff
--- a/UI/EquipmentAmountControl.cs	Thu Dec 24 10:52:03 2009 +0000
+++ b/UI/EquipmentAmountControl.cs	Thu Dec 24 12:04:32 2009 +0000
@@ -10,6 +10,7 @@
 using IBBoard.Lang;
 using IBBoard.Limits;
 using IBBoard.Windows.Forms.I18N;
+using IBBoard.Windows.Forms.Util;
 using IBBoard.WarFoundry.API;
 using IBBoard.WarFoundry.API.Objects;
 using IBBoard.WarFoundry.API.Util;
@@ -171,16 +172,6 @@
 			OnValueChanged();
 		}
 
-		private void percentage_KeyDown(object sender, KeyEventArgs e)
-		{
-			Console.WriteLine("Keycode: "+e.KeyCode);
-
-			if (e.KeyCode == Keys.Enter)
-			{
-				PerformPercentageValueChanged();
-			}
-		}
-
 		private void SetNumericValueFromPercentage()
 		{
 			numeric.ValueChanged -= numeric_ValueChanged;
@@ -346,5 +337,38 @@
 		{
 			OnValueChanged();
 		}
+
+		protected override bool ProcessDialogKey(Keys keyData)
+		{
+			bool processed = false;
+
+			if (keyData == Keys.Enter)
+			{
+				if (numeric.Focused)
+				{
+					processed = ForceUpDownControlUpdate(numeric);
+				}
+				else if (percentage.Focused)
+				{
+					processed = ForceUpDownControlUpdate(percentage);
+				}
+			}
+
+			if (!processed)
+			{
+				processed = base.ProcessDialogKey(keyData);
+			}
+
+			return processed;
+		}
+
+		private bool ForceUpDownControlUpdate(NumericUpDown control)
+		{
+			ControlValueChangedChecker checker = new ControlValueChangedChecker();
+			control.ValueChanged += checker.ValueChanged;
+			decimal val = control.Value;
+			control.ValueChanged -= checker.ValueChanged;
+			return checker.valueChanged;
+		}
 	}
 }