1

When copy-pasting an IP address from a share-point site, I can see that the actual value that gets pasted in the text-box is: 123.123.123.123​U+200B, which then returns false when attempting to IPAddress.TryParse(...).

I'm rather new in WPF world, and with the help of some SO posts I have so far managed to resolve this by having the PasteHandler in my code-behind class:

    private static void TextPasteHandler(object sender, DataObjectPastingEventArgs e)
    {
        if (!e.DataObject.GetDataPresent(typeof(string))) return;
        
        var pastedText = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;

        DataObject dObj = new DataObject();

        dObj.SetData(DataFormats.Text, Regex.Replace(pastedText, @"[^\u0020-\u007E]", string.Empty));

        e.DataObject = dObj;
    }

However, I was wondering if there's a more standard WPF way to handle cases like this?

RollerMobster
  • 864
  • 1
  • 10
  • 28
  • 1
    do you care about how the pasted string looks in textbox, or only care about successful parsing of ip? – Lei Yang Mar 24 '22 at 12:42
  • @LeiYang I do care how it looks in the textbox, it should contain only the IP address in the shape of 123.123.123.123, and nothing more. – RollerMobster Mar 24 '22 at 13:12
  • 1
    then you just regex extract ip, instead of replace. also [possible help](https://stackoverflow.com/a/15259355/1518100) – Lei Yang Mar 24 '22 at 13:15

0 Answers0