- void textBox1_TextChanged(object sender, EventArgs e)
- {
- int caret = textBox1.SelectionStart;
- bool atEnd = caret == textBox1.TextLength;
- textBox1.Text = sanitiseText(textBox1.Text);
- textBox1.SelectionLength = 0;
- textBox1.SelectionStart = atEnd ? textBox1.TextLength : caret;
- }
- void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!isHexDigit(e.KeyChar) && e.KeyChar != '\b')
- e.Handled = true;
- }
- string sanitiseText(string text)
- {
- char[] result = new char[text.Length*2];
- int n = 0;
- foreach (char c in text)
- {
- if ((n%3) == 2)
- result[n++] = ' ';
- if (isHexDigit(c))
- result[n++] = c;
- }
- return new string(result, 0, n);
- }
- bool isHexDigit(char c)
- {
- return "0123456789abcdef".Contains(char.ToLower(c));
- }
How to dynamically adjust the input length of a message?
Message Byte entries into textbox: xx xx xx xx xx xx xx xx xx xx xx xx ..... where x is 0-9, a-f in hex.
The code below was used as a sort of my goal keeper to allow only hex entry into the textbox but I want to further enhance the code below with the additional function mention above? Sorry if I couldn't explain it clearly enough due to my inadequate English command. Thanks
Bhuvanesh MohankumarPosted May 31, 2016, 3:02 AM