0

I have this problem with TextBox. User enters text/string in the TextBox and this string is exported as an value of attribute in the xml file. Is there some way how can I disable the user to enter (type+paste) forbidden (for xml format) characters, like the symbol for Euro €?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
user1885304
  • 55
  • 2
  • 6
  • Don't do this. It is annoying and often the user has no clue why he's not able to type the characters. Filter them out later, or better yet let them be there unless you absolutely have to – SoWhat Aug 13 '13 at 07:48
  • why would the euro symbol be forbidden in xml ? for a list of invalid xml chars see [here](http://stackoverflow.com/questions/730133/invalid-characters-in-xml). another option might be to wrap the user input as a cdata section ( `<[CDATA[...]]>` ). – collapsar Aug 13 '13 at 07:49
  • I think @Somesh is right. Ill filter out - or better replace (ampersand,...) - the characters later. And Im sorry collapsar for the bad example. You are right, Euro shouldnt be a problem in xml file, that was bad example. – user1885304 Aug 13 '13 at 14:35

5 Answers5

0

instead of restricting user you can filter out all the characters that you don't want before passing to your xml.

string textToPAss = ReplaceInvalidCharacters(yourTextbox.Text);


public string ReplaceInvalidCharacters(string toReplace)
{
   toReplace = toReplace.Replace("€","");
   //similarly do for all others unwanted characters
    return toReplace;
}
Ehsan
  • 31,833
  • 6
  • 56
  • 65
0

Handle the KeyPress event

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '€')
    e.Handled = true;

}
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
  • 1
    It would be nice for downvoters to provide a reason of their downvote. I accept critics, but i would like a reason, and if i am wrong i can fix it... – Giannis Paraskevopoulos Aug 13 '13 at 07:54
  • my guess would be damith, as a way to get his answer to the top. – Philip Gullick Aug 13 '13 at 08:18
  • 1
    I'm not the downvoter, but I don't think this is suitable. If a user right-clicks and pastes text into the textbox, containing the € character, this approach wont remove it. – Lars Kristensen Aug 13 '13 at 08:24
  • @LarsKristensen What you say is very true and is something i didn't think so. Also this is a constructive comment... – Giannis Paraskevopoulos Aug 13 '13 at 08:26
  • @jyparask, Thanks, just trying to help. Btw, shouldn't it be "e.Handled = false;" if the KeyChar matches, so it doesn't get added to the text? – Lars Kristensen Aug 13 '13 at 08:31
  • 1
    @LarsKristensen It should be set to true. It makes it think that the event has been handled so nothing happens afterwards and the character is not added. – Giannis Paraskevopoulos Aug 13 '13 at 08:36
  • @jyparask, I just tried it out and you are right. In one of my applications, I prevent deletion from a Grid by handling an event and setting e.Handled = false; - It is a grid from a third party vendor, so I guess they handle it backwards. Thanks for clearing it up! :) – Lars Kristensen Aug 13 '13 at 08:50
0

invalid XML characters has their valid XML equivalent. so if you can convert input to a valid XML then final result will be valid XML.

inputText = SecurityElement.Escape(inputText);

Ref :SecurityElement.Escape

Damith
  • 62,401
  • 13
  • 102
  • 153
  • he has got problem with value, and if he is passing that to a third party that doesn't accepts some values than your solution won't be helpful – Ehsan Aug 13 '13 at 07:53
  • This apparently does't escape the character they specifically asked for; although it needn't necessarily be for XML, still doesn't satisfy the requirements. – Grant Thomas Aug 13 '13 at 07:54
0

This will block the letter a from being typed into the textbox.

private void textBox1_TextChanged(object sender, EventArgs e)
{
    int pos = (sender as TextBox).Text.IndexOf("a");
    (sender as TextBox).Text = (sender as TextBox).Text.Replace("a", "");
    if (pos!= -1)
    (sender as TextBox).Select(pos, 0);
}

but as, Somesh Mukherjee, said rather Filter invalid chars out later. a simple String.Replace() will do the trick.

string.Empty
  • 10,393
  • 4
  • 39
  • 67
0

Here is an code example what you're looking for:

class MyTextBox : TextBox
{
    protected override void WndProc(ref Message m)
    {
        // Trap WM_PASTE:
        if (m.Msg == 0x302 && Clipboard.ContainsText())
        {
            var removedInvalid = from c in Clipboard.GetText()
                                 where XmlConvert.IsXmlChar(c)
                                 select c;

            SelectedText = new string(removedInvalid.ToArray());

            return;
        }

        base.WndProc(ref m);
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if (!XmlConvert.IsXmlChar(e.KeyChar))
            e.Handled = true;

        base.OnKeyPress(e);
    }
}

The above prevents the user to either stroke or paste invalid XML characters into the text box.

Ryszard Dżegan
  • 24,366
  • 6
  • 38
  • 56