From 3dd0ab200858c6a066d2291ed4c66f20be584898 Mon Sep 17 00:00:00 2001
From: zeromus <zeromus@users.noreply.github.com>
Date: Tue, 20 Oct 2015 01:57:47 -0500
Subject: [PATCH] HexTextBox - try to preserve the current value when changing
 the hex size. This kind of thing is super annoying, revert it immediately if
 there's a problem. Fixes #525

---
 .../CustomControls/HexTextBox.cs              | 20 +++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs b/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs
index 5db3b08369..6074e8e33a 100644
--- a/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs
+++ b/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs
@@ -30,11 +30,27 @@ namespace BizHawk.Client.EmuHawk
 
 		public void SetHexProperties(long domainSize)
 		{
+			bool wasMaxSizeSet = _maxSize.HasValue;
+			int currMaxLength = MaxLength;
+
 			_maxSize = domainSize - 1;
+
 			MaxLength = _maxSize.Value.NumHexDigits();
 			_addressFormatStr = "{0:X" + MaxLength + "}";
-			
-			ResetText();
+
+			//try to preserve the old value, as best we can
+			if(!wasMaxSizeSet)
+				ResetText();
+			else if(_nullable)
+				Text = "";
+			else if (MaxLength != currMaxLength)
+			{
+				long? value = ToLong();
+				if (value.HasValue)
+					value = value.Value & ((1L << (MaxLength * 4)) - 1);
+				else value = 0;
+				Text = string.Format(_addressFormatStr, value.Value);
+			}
 		}
 
 		public long GetMax()