如何根据键入文本框的第二个字节动态调整消息的长度? [英] How to dynamically adjust the length of a message based on the second byte keyed into the textbox?

查看:59
本文介绍了如何根据键入文本框的第二个字节动态调整消息的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的第二个字节以十六进制表示消息长度,当用户根据第二个字节的十六进制值键入文本框时,如何动态调整消息的长度?例如,我希望TextBox在达到长度限制时停止接受输入。那么,如果第二个字节是FF(254),那么TextBox中的字符总数是254 ... 127个十六进制对?



消息字节条目进入文本框:xx xx xx xx xx xx xx xx xx xx xx xx .....其中x为0-9,af为十六进制。



下面的代码被用作我的守门员的一种,只允许十六进制进入文本框,但我想通过编程方式进一步增强下面的代码?很抱歉,由于我的英语水平不足,我无法解释清楚。谢谢



我尝试过:



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:

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));
}

推荐答案

您的方法几乎可以正常工作,但您还需要检查文本框的当前字符数。您可以将参数 sender 打包为 TextBox 并检查 TexBoxBase.TextLength 。如果该字符不是退格键,并且您已经有最大字符数,请指定 e.Handled = true; 这就是全部。



但是,绝对没有必要做那样的事情。它已经为你完成了。您只需要为属性指定最大字符数 TextBoxBase.MaxLength

TextBoxBase.MaxLength属性(System.Windows.Forms) [ ^ ]。



问题解决了。



现在,与您无关的建议功能,仅限于代码风格,可维护性和提问方式。首先,由于显而易见的原因,永远不要使用自动生成的名称,如 textBox1_KeyPress 。不应该永久使用。它们违反(良好)命名约定。您将获得Intellisense以重命名它们。名称应该在语义上合理。而且,即使名称暗示这是一个事件处理程序,也没有证据表明它是。这只是一种方法。这是因为在某个事件实例上执行了运算符+ =的处理程序。因此,您需要显示它。由于多种原因,我避免通过设计师添加任何事件处理程序,并强烈建议每个人。
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.


这篇关于如何根据键入文本框的第二个字节动态调整消息的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆