1

私は数ヶ月間、JuceLibraryでC++に取り組んでいました。私は自分のプロジェクトで、テキストボックスのフォーマットがいくつかの機能を備えた16進値のみに変更されたコードを作成しました。

デモンストレーション:

12 ab 32 a5 64

カーソルが最後にあり、バックスペースを押し続けると、一般的なテキストボックスで発生するように、値が削除されます。

ここで、カーソルがa5の先頭にあり、「Deleteキー」を押すと、値は次のようになります。

12 ab 32 56 4

カーソルがa5の終わりにあり、「削除キー」を押しても何も起こりません。値を入力している間、スペースバーは間隔bwに2つの値を許可しないでください。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

これを試してください(TextChanged-TextBoxのイベント):

    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 に答える