TextBox should display text in hexadecimal in a specific format

Posted by Owais Wani on Stack Overflow See other posts from Stack Overflow or by Owais Wani
Published on 2012-09-27T10:22:29Z Indexed on 2012/09/28 9:37 UTC
Read the original article Hit count: 342

Filed under:
|
|
|

I have a textbox in my xaml file which is editable. Now according to my project requirements content in textbox should only be 0-9 and a-f (hexadecimal values) and textbox should take the input based on hexadecimal values.

Demonstratation:

12 ab 32 a5 64

Now if my cursor is at the end and i go on pressing backspace, it shud remove the values as it happens in a general text box.

Now If my cursor is at the beginning of a5, and i press "delete key", the value should become like:

12 ab 32 56 4

If my cursor is at the end of a5 and i press the 'delete key" nothing should happen.

I had done this successful in my C++ application as follows:

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

}

where m_texti2cWrite is the name given to textbox. How can i implement the same case in my wpf application which is MVVM based. I have a textbox which shud take inputs as I said above. please help!!!

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf