Home > database >  how can display non printable character in textbox or textblock WPF
how can display non printable character in textbox or textblock WPF

Time:06-16

When using winform, I can add and display non printable character ASCII to textbox (etc: STX (\u0002), ETX (\u0003),.....). Now I change to WPF, I facing with issue can not display non printable character at textbox or textblock

I check in string it still exist "\u0002ABCDEF\u0003" but in textbox WPF only "ABCDEF", I try copy all data in textbox and paste to Notepad it show complete "\u0002ABCDEF\u0003"

Please help me this case, how can setting property or change variable of textbox for display non printable character ASCII

CodePudding user response:

WPF will not display these characters in a textbox or a textblock. You would need to write a converter that finds the character in the string which is to be presented by a binding and substitute.

Have a look here:

https://social.msdn.microsoft.com/Forums/en-US/970da1a8-232b-4984-b0e9-ec78cf5d9648/wpf-textbox-is-not-showing-special-characters-belie-ascii-code-7?forum=wpf

Hope that helps!

CodePudding user response:

Thank Mr Benporter !!! Your guidline support me have idea using dictionary for connvert non printable character to show text when receive data & hidden text (raw data) when send.

Screenshot sample

internal class NonPrintableCharacter
{
    Dictionary<string, string> characters = new Dictionary<string, string>();
    public string _str = string.Empty;
    public NonPrintableCharacter()
    {
        characters = new Dictionary<string, string>();
        Create_Dictionary();
    }
    public NonPrintableCharacter(string text)
    {
        _str = text;
        characters = new Dictionary<string, string>();
        Create_Dictionary();
    }


    private void Create_Dictionary()
    {
        characters.Add(Convert.ToChar(0).ToString(), "<NULL>");
        characters.Add(Convert.ToChar(1).ToString(), "<SOH>");
        characters.Add(Convert.ToChar(2).ToString(), "<STX>");
        characters.Add(Convert.ToChar(3).ToString(), "<ETX>");
        characters.Add(Convert.ToChar(4).ToString(), "<EOT>");
        characters.Add(Convert.ToChar(5).ToString(), "<ENQ>");
        characters.Add(Convert.ToChar(6).ToString(), "<ACK>");
        characters.Add(Convert.ToChar(7).ToString(), "<BELL>");
        characters.Add(Convert.ToChar(8).ToString(), "<BS>");
        characters.Add(Convert.ToChar(9).ToString(), "<TAB>");
        characters.Add(Convert.ToChar(10).ToString(), "<LF>");
        characters.Add(Convert.ToChar(11).ToString(), "<VT>");
        characters.Add(Convert.ToChar(12).ToString(), "<FF>");
        characters.Add(Convert.ToChar(13).ToString(), "<CR>");
        characters.Add(Convert.ToChar(14).ToString(), "<SO>");
        characters.Add(Convert.ToChar(15).ToString(), "<SI>");
        characters.Add(Convert.ToChar(16).ToString(), "<DLE>");
        characters.Add(Convert.ToChar(17).ToString(), "<DC1>");
        characters.Add(Convert.ToChar(18).ToString(), "<DC2>");
        characters.Add(Convert.ToChar(19).ToString(), "<DC3>");
        characters.Add(Convert.ToChar(20).ToString(), "<DC4>");
        characters.Add(Convert.ToChar(21).ToString(), "<NAK>");
        characters.Add(Convert.ToChar(22).ToString(), "<SYN>");
        characters.Add(Convert.ToChar(23).ToString(), "<ETB>");
        characters.Add(Convert.ToChar(24).ToString(), "<CAN>");
        characters.Add(Convert.ToChar(25).ToString(), "<EM>");
        characters.Add(Convert.ToChar(26).ToString(), "<SUB>");
        characters.Add(Convert.ToChar(27).ToString(), "<ESC>");
        characters.Add(Convert.ToChar(28).ToString(), "<FS>");
        characters.Add(Convert.ToChar(29).ToString(), "<GS>");
        characters.Add(Convert.ToChar(30).ToString(), "<RS>");
        characters.Add(Convert.ToChar(31).ToString(), "<US>");
    }

    public string ShowNonPrintCharacter()
    {
        foreach(KeyValuePair<string,string> item in characters)
        {
            if(_str.Contains(item.Key))
            {
                _str= _str.Replace(item.Key, item.Value);
            }
        }
        return _str;
    }
    public string HiddenNonPrintCharacter()
    {
        foreach (KeyValuePair<string, string> item in characters)
        {
            if (_str.Contains(item.Value))
            {
                _str = _str.Replace(item.Value, item.Key);
            }
        }
        return _str;
    }
}
  • Related