ho hebe

ho hebe

  • NA
  • 1
  • 502

How to dynamically adjust the input length of a message?

May 30 2016 10:09 PM
  1. void textBox1_TextChanged(object sender, EventArgs e)  
  2. {  
  3.     int caret = textBox1.SelectionStart;  
  4.     bool atEnd = caret == textBox1.TextLength;  
  5.     textBox1.Text = sanitiseText(textBox1.Text);  
  6.     textBox1.SelectionLength = 0;  
  7.     textBox1.SelectionStart = atEnd ? textBox1.TextLength : caret;  
  8. }  
  9.   
  10. void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
  11. {  
  12.     if (!isHexDigit(e.KeyChar) && e.KeyChar != '\b')  
  13.         e.Handled = true;  
  14. }  
  15.   
  16. string sanitiseText(string text)  
  17. {  
  18.     char[] result = new char[text.Length*2];  
  19.   
  20.     int n = 0;  
  21.   
  22.     foreach (char c in text)  
  23.     {  
  24.         if ((n%3) == 2)  
  25.             result[n++] = ' ';  
  26.   
  27.         if (isHexDigit(c))  
  28.             result[n++] = c;  
  29.     }  
  30.   
  31.     return new string(result, 0,  n);  
  32. }  
  33.   
  34. bool isHexDigit(char c)  
  35. {  
  36.     return "0123456789abcdef".Contains(char.ToLower(c));  
  37. }  
Assuming my second byte in hex is a representation of the message length, how can I dynamically adjust the length of the message the user can key into the textbox based on the hex value of the second byte?

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




Answers (1)