Advertisement
ivandrofly

SubtitleEdit: Get context menu using ExpressionTree

Nov 27th, 2017
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1.         private static Action<object, string> TokenProvider;
  2.  
  3.         private static Action<object, string> BuildTokenProvider(object control)
  4.         {
  5.             if (TokenProvider != null)
  6.             {
  7.                 // NOTE: once this is contructed, if you try to invoke it using other type of control it will throw exception
  8.                 return TokenProvider;
  9.             }
  10.             Type ctrlType = control.GetType();
  11.             var objParam = Expression.Parameter(typeof(object), "control");
  12.             var objValueParam = Expression.Parameter(typeof(string), "value");
  13. // convert the given object to control type to allow accesing "SelectedText" property
  14.             var convertExpr = Expression.Convert(objParam, ctrlType);
  15.             var propExp = Expression.Property(convertExpr, "SelectedText");
  16.             var binaryExp = Expression.Assign(propExp, objValueParam);
  17.             TokenProvider = Expression.Lambda<Action<object, string>>(binaryExp, objParam, objValueParam).Compile();
  18.             return TokenProvider;
  19.         }
  20.  
  21.         public static ContextMenu GetRegExContextMenu(object control)
  22.         {
  23.             TokenProvider = TokenProvider ?? BuildTokenProvider(control);
  24.             var cm = new ContextMenu();
  25.             var l = Configuration.Settings.Language.RegularExpressionContextMenu;
  26.             cm.MenuItems.Add(l.WordBoundary, delegate { TokenProvider(control, "\\b"); });
  27.             cm.MenuItems.Add(l.NonWordBoundary, delegate { TokenProvider(control, "\\B"); });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement