Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Assuming my second byte in hex is a representation of the message length, how can I dynamically adjust the length of the message while the user key into the textbox based on the hex value of the second byte? For instance I want the TextBox to stop accepting input when the length limit is reached. So, if the second byte is FF (254), then the total number of characters in the TextBox is 254 ... 127 hex-pairs?

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 complementary function mentioned above programmatically? Sorry if I couldn't explain it clearly enough due to my inadequate English command. Thanks

What I have tried:

C#
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));
}
Posted
Updated 5-Jun-16 18:31pm
v4
Comments
BillWoodruff 2-Jun-16 5:40am    
Do you mean that you want the TextBox to stop accepting input when the length limit is reached ? So, if the second byte is FF (254), then the total number of characters in the TextBox is 254 ... 127 hex-pairs ?
imso 2-Jun-16 21:32pm    
Yes you nailed what I'm trying to express! Thank You
Sergey Alexandrovich Kryukov 5-Jun-16 20:17pm    
TextBox? Which one? Full type name, please. This is important: Different types "TextBox" for different frameworks/libraries have somewhat different features and set of public members.

Note that each time you ask a question on UI, you have to tag your application type and UI framework/library used.

—SA
imso 5-Jun-16 21:17pm    
Oh sorry about my inadequacy in posting as I'm still somewhat newbie in learning programming C#. I'm just using a very generic textbox Class named textbox1, commonly used so I'm not sure of other types or library if it exists?
Sergey Alexandrovich Kryukov 5-Jun-16 21:41pm    
Sorry, there is no such thing as "very generic TextBox". I don't know what you consider "commonly used". To me, they are all equally "common".

Please, full type name. If you do .NET development, you should know what a type is. A type has a name.
Example: "System.Windows.Forms.TextBox"...

—SA

1 solution

Your approach would almost work, but you also need to check up present number of characters of the text box. You can type-case the parameter sender to TextBox and checkup TexBoxBase.TextLength. If the character is not backspace, and you have maximum number of characters already, assign e.Handled = true; that's all.

However, there is absolutely no need to do anything like that. It is already done for you. All you need is to assign the maximum number of characters to the property TextBoxBase.MaxLength:
TextBoxBase.MaxLength Property (System.Windows.Forms)[^].

Problem solved.

Now, the advice not related to your functionality, only to the code style, maintainability and the way you ask questions. First, never use auto-generated names like textBox1_KeyPress, by obvious reasons. The are not supposed to be used permanently. They violate (good) naming conventions. You are given Intellisense to rename them. The names should be semantically sensible. Moreover, even if the name suggests that this is an event handler, there is no evidence that it is. This is just a method. It because a handler if the operator += is performed on some event instance. Therefore, you need to show it. By a number of reasons, I avoid adding any event handlers via the designer and strongly advise the same to everyone.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900