1

几个月来,我一直在使用 Juce Library 研究 C++。我在我的项目中编写了一个代码,其中文本框的格式被修改为仅具有很少功能的十六进制值:

演示:

12 ab 32 a5 64

现在,如果我的光标在末尾并且我继续按退格键,它将删除值,因为它发生在一般文本框中。

现在如果我的光标在 a5 的开头,并且我按下“删除键”,值应该变成这样:

12 ab 32 56 4

如果我的光标位于 a5 的末尾并且我按下“删除键”,则不会发生任何事情。在输入值时,空格键不应该让间距 bw 两个值。应该只允许输入 af 和 0-9。

此处的 C++ 代码:

void CMSP430CommPanel::textEditorTextChanged (TextEditor& editor)
{

if(&editor == m_texti2cWrite)
{       
int count = 0;
int location;

String text1 = m_texti2cWrite->getText();
String text = m_texti2cWrite->getText().removeCharacters(" ");
String hexString = String::empty;   
int countCaret = m_texti2cWrite->getCaretPosition();

    for(int i=0; i < text.length(); i++)
    {               
        hexString = hexString + String (&text[i], 1);
        if((i+1) % 2 == 0)
        {
            if(i != text.length()-1)
            {
                hexString = hexString + T(" "); 
                count ++;               
            }
        }
        count ++;
    }           

    m_texti2cWrite->setText(hexString,false);

    if(text1.length() == m_texti2cWrite->getCaretPosition())
    {
        m_texti2cWrite->setCaretPosition(count);
    }
    else
    {
        m_texti2cWrite->setCaretPosition(countCaret);
    }
}
}

我希望同样的事情在我的 WPF 应用程序中工作。让我们说 C# 中相同代码的一般实现。

请帮忙!!!

4

2 回答 2

1

试试这个(TextBox 的 TextChanged-Event):

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox m_texti2cWrite = (TextBox)sender;
        int count = 0;

        string text1 = m_texti2cWrite.Text;
        string text = m_texti2cWrite.Text.Replace(" ", string.Empty);
        string hexString = string.Empty;
        int countCaret = e.Changes.ToList()[0].Offset;

        for (int i = 0; i < text.Length; i++)
        {
            hexString += text[i];
            if ((i + 1) % 2 == 0)
            {
                if (i != text.Length - 1)
                {
                    hexString = hexString + " ";
                    count++;
                }
            }
            count++;
        }

        m_texti2cWrite.Text = hexString;
        if (text1.Length == countCaret)
        {
            m_texti2cWrite.Select(count, 0);
        }
        else
        {
            if (e.Changes.ToList()[0].RemovedLength == 0)
            {
                m_texti2cWrite.Select(countCaret + 1, 0);
                if (string.IsNullOrWhiteSpace(hexString.Substring(countCaret, 1)))
                    m_texti2cWrite.Select(countCaret + 2, 0);
            }
            else
            {
                m_texti2cWrite.Select(countCaret, 0);
                if (string.IsNullOrWhiteSpace(hexString.Substring(countCaret, 1)))
                    m_texti2cWrite.Select(countCaret + 1, 0);
            }
        }
    }
}

编辑(仅接受 Digits、ControlKeys 或 af):

  1. 添加这个方法:

    private Boolean IsTextAllowed(String text)
    {
        string acceptedChars = "ABCDEFabcdef";
        foreach (Char c in text.ToCharArray())
        {
            if (Char.IsDigit(c) || Char.IsControl(c) || acceptedChars.Contains(c)) continue;
            else return false;
        }
        return true;
    }
    
  2. 将 TextBox_PreviewTextInput-Event 添加到您的 TextBox

    private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !IsTextAllowed(e.Text);
    }
    
于 2012-09-28T11:35:48.277 回答
0
public class CMSP430CommPanel
{
    //C++ TO C# CONVERTER WARNING: The original C++ declaration of the following method implementation was not found:
    public void textEditorTextChanged(TextEditor editor)
    {

if (editor == m_texti2cWrite)
{
int count = 0;
int location;

string text1 = m_texti2cWrite.getText();
string text = m_texti2cWrite.getText().removeCharacters(" ");
string hexString = string.empty;
int countCaret = m_texti2cWrite.getCaretPosition();

    for (int i = 0; i < text.Length; i++)
    {
        hexString = hexString + (string)(text[i], 1);
        if ((i + 1) % 2 == 0)
        {
            if (i != text.Length - 1)
            {
                hexString = hexString + T(" ");
                count++;
            }
        }
        count++;
    }

    m_texti2cWrite.setText(hexString,false);

    if (text1.Length == m_texti2cWrite.getCaretPosition())
    {
        m_texti2cWrite.setCaretPosition(count);
    }
    else
    {
        m_texti2cWrite.setCaretPosition(countCaret);
    }
}
}
}
于 2012-09-28T11:21:26.990 回答