andrew4582

ControlTagExtentionsWinForms

Nov 1st, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1.     public static class ControlTagExtentions {
  2.  
  3.         public static T GetTagItem<T>(this Control c,string key) {
  4.             Dictionary<string,object> tagDict = null;
  5.  
  6.             if(c.Tag == null) {
  7.                 tagDict = new Dictionary<string,object>();
  8.                 c.Tag = tagDict;
  9.             }
  10.             else {
  11.                 tagDict = c.Tag as Dictionary<string,object>;
  12.                 if(tagDict == null)
  13.                     return default(T);
  14.             }
  15.             if(!tagDict.ContainsKey(key))
  16.                 return default(T);
  17.  
  18.             return (T)tagDict[key];
  19.         }
  20.  
  21.         public static void SetTagItem(this Control c,string key,object value) {
  22.             Dictionary<string,object> tagDict = null;
  23.             if(c.Tag == null) {
  24.                 tagDict = new Dictionary<string,object>();
  25.                 c.Tag = tagDict;
  26.             }
  27.             else {
  28.                 tagDict = c.Tag as Dictionary<string,object>;
  29.                 if(tagDict == null) {
  30.                     tagDict = new Dictionary<string,object>();
  31.                     c.Tag = tagDict;
  32.                 }
  33.             }
  34.             tagDict[key] = value;
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment