无法将C ++中的TextBox_TextChanged()转换为C#

时间:2012-09-28 11:11:27

标签: c# c++ wpf textbox

我在Juce Library工作了几个月。我在我的项目中编写了一个代码,其中文本框的格式被修改为仅具有少量特征的十六进制值:

Demonstratation:

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#中相同代码的一般实现。

请帮助!!!

2 个答案:

答案 0 :(得分: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);
            }
        }
    }
}

编辑(仅接受数字,ControlKeys或a-f):

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

答案 1 :(得分: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);
    }
}
}
}