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

标签 c# c++ wpf textbox

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

演示:

12 ab 32 a5 64

现在,如果我的光标位于末尾并且我继续按退格键,它会像在一般文本框中发生的那样删除值。

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

12 ab 32 56 4

如果我的光标在 a5 的末尾并且我按下“删除键”,则不会发生任何事情。在输入值时,空格键不应让两个值之间有间距。只应允许输入 a-f 和 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# 中相同代码的一般实现。

请帮忙!!!

最佳答案

试试这个(TextBox 的 TextChanged 事件):

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

编辑(只接受数字、控制键或 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 事件添加到您的文本框

    private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !IsTextAllowed(e.Text);
    }
    

关于c# - 无法将 C++ 中的 TextBox_TextChanged() 转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12638894/

相关文章:

c# - MySql 数据库中的 EF 重命名列

c# - 在带有 continue 的循环中使用

c# - 如何从现有列表中获取与关键字匹配的项目列表并忽略大小写(不区分大小写)?

c# - 为什么C#编译这行代码不报错

c++ - 是否应允许模板模板参数接收从先前参数解析的参数?

c++ fstream不打印像ρ和Δ这样的符号来正确归档

c# - 最大化/最小化时,如何调整 TextBox 的大小以适应窗口大小?

wpf - 在什么情况下 ICommand CanExecute 可能不会更新?

c++ - Qt Creator 中“未找到有效工具包”

c# - 将 DataTable 导出为 CSV 时出现逗号问题