using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Media; using System.Text; using System.Threading.Tasks; namespace W4Gui.Components { public class HexInputBox : TextBox { string lastText = ""; public HexInputBox() { } public byte[] Value { get { return convertHexStringToByteArray(this.Text.Replace(" ", "")); } set { this.Text = BitConverter.ToString(value).Replace('-', ' '); } } private byte[] convertHexStringToByteArray(string hexString) { if (hexString.Length % 2 != 0) hexString += "0"; byte[] data = new byte[hexString.Length / 2]; for (int index = 0; index < data.Length; index++) { string byteValue = hexString.Substring(index * 2, 2); data[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture); } return data; } private bool checkChars(KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Insert) return false; else if (Char.IsAsciiHexDigit(e.KeyChar)) return true; else if (e.KeyChar == '\b') return true; else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0) return true; return false; } protected override void OnTextChanged(EventArgs e) { int cacheSelectionStart = this.SelectionStart; try { string newText = this.Text; this.Text = BitConverter.ToString(convertHexStringToByteArray(newText.Replace(" ", ""))).Replace('-', ' '); this.lastText = this.Text; } catch { this.Text = lastText; this.SelectionStart = cacheSelectionStart; } base.OnTextChanged(e); } protected override void OnKeyDown(KeyEventArgs e) { if(e.KeyCode == Keys.Right) { int cacheSelectionStart = this.SelectionStart; cacheSelectionStart++; if (cacheSelectionStart < this.TextLength) { if (this.TextLength > 0 && this.Text[cacheSelectionStart] == ' ') cacheSelectionStart++; this.SelectionStart = cacheSelectionStart; } else { this.SelectionStart = this.TextLength; } e.SuppressKeyPress = true; } else if (e.KeyCode == Keys.Left) { int cacheSelectionStart = this.SelectionStart; cacheSelectionStart--; if (cacheSelectionStart > 0) { if (this.TextLength > 0 && this.Text[cacheSelectionStart] == ' ') cacheSelectionStart--; this.SelectionStart = cacheSelectionStart; } else { this.SelectionStart = 0; } e.SuppressKeyPress = true; } else if (e.KeyCode == Keys.Delete) { int cacheSelectionStart = this.SelectionStart; StringBuilder text = new StringBuilder(this.Text); if (cacheSelectionStart < text.Length) { if (cacheSelectionStart != 0) { if (text[cacheSelectionStart] != ' ') cacheSelectionStart--; if (text[cacheSelectionStart] == ' ') { text.Remove(cacheSelectionStart, (text.Length >= 3) ? 3 : 2); } else { cacheSelectionStart++; } } else { if (text.Length > 0) { text.Remove(0, (text.Length >= 3) ? 3 : 2); } } } this.Text = text.ToString(); this.SelectionStart = cacheSelectionStart; e.SuppressKeyPress = true; } base.OnKeyDown(e); } protected override void OnKeyPress(KeyPressEventArgs e) { if (!checkChars(e)) { e.Handled = true; SystemSounds.Beep.Play(); base.OnKeyPress(e); return; } char toWrite = (e.KeyChar).ToString().ToUpper().First(); if (!Char.IsAsciiHexDigit(toWrite)) { if (toWrite == '\b') { int cacheSelectionStart = this.SelectionStart; StringBuilder text = new StringBuilder(this.Text); if (cacheSelectionStart < text.Length) { if (text[cacheSelectionStart] != ' ') cacheSelectionStart--; if (text[cacheSelectionStart] == ' ') { if(cacheSelectionStart > 3) { text.Remove(cacheSelectionStart - 3, 3); cacheSelectionStart -= 3; } else { text.Remove(cacheSelectionStart - 2, 2); cacheSelectionStart -= 2; } } else { cacheSelectionStart++; } } else { text.Remove(text.Length - 3, 3); cacheSelectionStart -= 3; } this.Text = text.ToString(); this.SelectionStart = cacheSelectionStart; e.Handled = true; } base.OnKeyPress(e); return; } else { int cacheSelectionStart = this.SelectionStart; StringBuilder text = new StringBuilder(this.Text); if (cacheSelectionStart < text.Length) { if (text[cacheSelectionStart] == ' ') cacheSelectionStart++; text[cacheSelectionStart] = toWrite; cacheSelectionStart++; } else { if (cacheSelectionStart != 0) { text.Append(" "); cacheSelectionStart++; } text.Append(toWrite + "0"); cacheSelectionStart++; } this.Text = text.ToString(); this.SelectionStart = cacheSelectionStart; e.Handled = true; base.OnKeyPress(e); return; } } } }