v1.06 — Fix product key paste across all 5 fields
- Use DataObject.Pasting handler to intercept paste before MaxLength truncation - Full key paste (e.g. XXXXX-XXXXX-XXXXX-XXXXX-XXXXX) distributes across all boxes - Works when pasting into any of the 5 fields Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -27,6 +27,40 @@ namespace InstaSoftOfficeTool.Pages
|
||||
_keyBoxes[i].Text = parts[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Intercept paste on all boxes
|
||||
foreach (var box in _keyBoxes)
|
||||
{
|
||||
DataObject.AddPastingHandler(box, OnPaste);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPaste(object sender, DataObjectPastingEventArgs e)
|
||||
{
|
||||
if (!e.DataObject.GetDataPresent(typeof(string))) return;
|
||||
|
||||
var pasted = (string)e.DataObject.GetData(typeof(string));
|
||||
var allAlphaNum = Regex.Replace(pasted, "[^A-Za-z0-9]", "");
|
||||
|
||||
// Only intercept if it looks like a full key (more than 5 chars)
|
||||
if (allAlphaNum.Length <= 5) return;
|
||||
|
||||
e.CancelCommand(); // prevent default paste
|
||||
|
||||
_suppressAutoTab = true;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
int start = i * 5;
|
||||
if (start < allAlphaNum.Length)
|
||||
{
|
||||
int len = System.Math.Min(5, allAlphaNum.Length - start);
|
||||
_keyBoxes[i].Text = allAlphaNum.Substring(start, len).ToUpper();
|
||||
}
|
||||
}
|
||||
_suppressAutoTab = false;
|
||||
_keyBoxes[4].Focus();
|
||||
_keyBoxes[4].CaretIndex = _keyBoxes[4].Text.Length;
|
||||
ValidationMessage.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private void KeyBox_TextChanged(object sender, TextChangedEventArgs e)
|
||||
@@ -36,28 +70,6 @@ namespace InstaSoftOfficeTool.Pages
|
||||
var tb = (TextBox)sender;
|
||||
var raw = tb.Text;
|
||||
|
||||
// Detect full key paste (contains dash or longer than 5 chars with alphanumerics)
|
||||
var allAlphaNum = Regex.Replace(raw, "[^A-Za-z0-9]", "");
|
||||
if (allAlphaNum.Length > 5 && tb == _keyBoxes[0])
|
||||
{
|
||||
// Full key pasted — distribute across all boxes
|
||||
_suppressAutoTab = true;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
int start = i * 5;
|
||||
if (start < allAlphaNum.Length)
|
||||
{
|
||||
int len = System.Math.Min(5, allAlphaNum.Length - start);
|
||||
_keyBoxes[i].Text = allAlphaNum.Substring(start, len).ToUpper();
|
||||
}
|
||||
}
|
||||
_suppressAutoTab = false;
|
||||
_keyBoxes[4].Focus();
|
||||
_keyBoxes[4].CaretIndex = _keyBoxes[4].Text.Length;
|
||||
ValidationMessage.Visibility = Visibility.Collapsed;
|
||||
return;
|
||||
}
|
||||
|
||||
// Clean non-alphanumeric chars
|
||||
var cleaned = Regex.Replace(raw, "[^A-Za-z0-9]", "");
|
||||
if (cleaned != raw)
|
||||
|
||||
Reference in New Issue
Block a user