2017-07-26 64 views
-1
public static void AlphabeticPasting(object sender, DataObjectPastingEventArgs e) 
{ 
    if (e.DataObject.GetDataPresent(typeof(string))) 
    { 
     var text = e.DataObject.GetData(typeof(string)) as string; 
     if (!Regex.IsMatch(text, (Properties.Resources.Alpha))) 
     { 
      //replace and recopy 
      var trimmed = Regex.Replace(text,Properties.Resources.AlphaPasting, string.Empty); 
      e.CancelCommand(); 
      Clipboard.SetData(DataFormats.Text, trimmed); 
      ApplicationCommands.Paste.Execute(trimmed, e.Source as FrameworkElement); 

     } 
    } 
} 

我想限制數字部分在文本框中粘貼, 我打電話文本框,Properties.Resources.Alphabet和Properties.Resources的DataObject.Pasting屬性裏面這個方法。 AlphabetPasting是正則表達式的一部分,我在資源文件中提到了正則表達式。System.StackOverflowException在Windows動態鏈接庫

Alpha = ^[[A-Za-z-~`[email protected]#$%^&*()_+=|{}':;.,<>/\\]?]*$ and AlphaPsting = [^[A-Za-z-~`[email protected]#$%^&*()_+=|{}':;.,<>/\\]?]+ 

我只需要從資源文件中取正則表達式。

我得到System.StackOverFlowException如果我試圖通過文本框中的任何東西。請幫我解決這個問題。

+0

如果您正在處理alphabeticPasting的事件,那麼當您執行ApplicationCommand.Paste.Execute時,是不是再次調用它?這可能會導致它... – iMortalitySX

回答

0

我看起來整個方法被稱爲Paste事件的結果。如果情況並非如此,那麼就忽略這個答案。 ApplicationCommands.Paste.Execute()將觸發事件,將您的代碼放入無限循環。

而不是使用ApplicationCommands.Paste.Execute(),嘗試將發件人對象轉換爲文本框,然後相應地設置文本屬性。爲了使它工作,你可能需要跟蹤更多的信息(光標位置,長度等),但是你不應該陷入循環。

+0

亞工作正常,感謝Mark Bonafe .. –