Reputation: 1061
I've done some googling and found this relevant here but its only for masktextbox how do I do it for textbox instead?
For instance, 24 hex byte entries xx xx xx xx xx xx xx xx xx xx xx xx where x is 0-9, a-f ,A-F.
After which, for every group of 2 hex value is converted to its decimal equivalent to be displayed into another textbox? How to do that?
Upvotes: 0
Views: 219
Reputation: 109567
This is a bit hacky, but it seems to work.
Add a TextChanged
and a KeyPress
handler for your text box as follows (this code assumes that the text box is called textBox1
- obviously you should substitute your own):
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));
}
As for your second question: That's a different question and therefore you should post a separate question for it.
Upvotes: 1