Thursday, November 4, 2010

MaskEdit Code

If no MaskEdit found do the following rough code

private void ultraTextEditor1_KeyUp(object sender, KeyEventArgs e)
{

UltraTextEditor te = (UltraTextEditor)sender;
var q = from _ in
new List() {
Keys.D0, Keys.D1, Keys.D2,Keys.D3, Keys.D4, Keys.D5,
Keys.D6, Keys.D7, Keys.D8,Keys.D9,
Keys.NumPad0, Keys.NumPad1, Keys.NumPad2,Keys.NumPad3, Keys.NumPad4, Keys.NumPad5,
Keys.NumPad6, Keys.NumPad7, Keys.NumPad8,Keys.NumPad9,
Keys.Delete, Keys.Decimal,Keys.Left, Keys.Right,Keys.OemPeriod,Keys.Back,Keys.Home,
}
where e.KeyCode == _
select _;
e.SuppressKeyPress = q.Count() == 0;
if ((e.KeyCode == Keys.Decimal || e.KeyCode == Keys.OemPeriod) && te.Text.Count(s => s == '.') == 1)
{
e.SuppressKeyPress = true;
}

var q1 = from _ in
new List() {
Keys.Left, Keys.Right,Keys.Back,Keys.Home,
}
where e.KeyCode == _
select _;
if (q1.Count() > 0) return;

if (te.Text.Length > 0)
{
string t = te.Text.Replace(",", "").Replace("..", ".");
double Data = Convert.ToDouble(t);
int i = (int)Math.Floor(Data);
string sInt = string.Format("{0:#,#}", i);
string dec = +t.IndexOf('.') >= 0 ? t.Substring(t.IndexOf('.')) : "";
te.Text = "";
te.AppendText(sInt + dec);
}
}

Monday, November 1, 2010

Dynamically Create Event Handler

 
public static class UIExtensioins
{
public class Inclusion
{
public string ComponentTypeFullName { get; set; }
public string EventName { get; set; }
public string[] SenderPropertyNames { get; set; }
public string[] EventArgsPropertyNames { get; set; }
}

public static void AttachLoggerToUIEvent(this Form f, IContainer components,List inclusionList)
{
foreach (Inclusion i in inclusionList)
{
foreach (Component c in FindComponent(f, i.ComponentTypeFullName, components))
{
AttacheEventHandler(c, i);
}
}
}

public static IEnumerable FindComponent(Control ownerControl,string typeFullName, IContainer containter)
{
bool topComponentFound = false;
foreach (Component c1 in containter.Components)
if (c1.GetType().FullName == typeFullName)
{
topComponentFound = true;
yield return c1;
}
if (topComponentFound) yield break;

foreach (Control c1 in ownerControl.Controls)
{
if (c1.GetType().FullName == typeFullName) yield return c1;
else foreach (Control c2 in FindComponent(c1, typeFullName, containter))
if (c2.GetType().FullName == typeFullName) yield return c2;
}
}


public static void Log(Inclusion i, object sender, T args) where T : EventArgs
{
string logMsg="";
StringBuilder sb = new StringBuilder();

try
{
foreach (string s in i.SenderPropertyNames)
{
if (s != null)
{
PropertyInfo pi = sender.GetType().GetProperty(s);
object v = pi.GetValue(sender, null);
sb.Append(s + "=" + v + ";");
}
}
foreach (string s in i.EventArgsPropertyNames)
{
if (s != null)
{
string[] parts = s.Split('.');
PropertyInfo pi;
object v = args;
for (int k = 0; k < parts.Length; k++)
{
pi = v.GetType().GetProperty(parts[k]);
v = pi.GetValue(v, null);
}
sb.Append(s + "=" + v + ";");
}
}
string Name = "";
Control c = sender as Control;
if (c != null) Name = c.Parent.Name + ":" + c.Name;
logMsg = Name + " " + i.EventName + " " + sb.ToString();
}
catch (Exception ex)
{
logMsg = ex.Message + " " + ex.InnerException.Message + " " + ex.StackTrace;
}

finally
{
Action act = delegate
{
Console.WriteLine(logMsg);
ILog LOG = LogManager.GetLogger(typeof(TradeEntryMainForm));
LOG.Info(logMsg);
};
act.BeginInvoke(ar => { try { act.EndInvoke(ar); } catch { } }, null);
}

}

private static void AttacheEventHandler(Component c, Inclusion i)
{
foreach (EventInfo ei in c.GetType().GetEvents())
{
if (c.GetType().FullName == i.ComponentTypeFullName && ei.Name == i.EventName)
{
Type eventArgsType = ei.EventHandlerType.GetMethod("Invoke").GetParameters()[1].ParameterType;
MethodInfo mi = typeof(UIExtensioins).GetMethod("Log",
BindingFlags.Static | BindingFlags.Public).MakeGenericMethod(eventArgsType
);
ei.AddEventHandler(c,
Delegate.CreateDelegate(ei.EventHandlerType, i, mi));
}
}
}
}

App Setting Json serialization


public class Inclusion
{
public string ComponentTypeFullName { get; set; }
public string EventName { get; set; }
public string[] SenderPropertyNames { get; set; }
public string[] EventArgsPropertyNames { get; set; }
}

static List li = new List()
{
new Inclusion(){
ComponentTypeFullName="Infragistics.Win.Misc.UltraButton",
EventName="Click",
SenderPropertyNames=new string[]{null},
EventArgsPropertyNames=new string[]{null}},
new Inclusion() {
ComponentTypeFullName="Infragistics.Win.UltraWinEditors.UltraComboEditor",
EventName="SelectionChanged",
SenderPropertyNames=new string[]{"Text"},
EventArgsPropertyNames=new string[]{null}},
new Inclusion() {
ComponentTypeFullName="Infragistics.Win.UltraWinToolbars.UltraToolbarsManager",
EventName="ToolClick",
SenderPropertyNames=new string[]{null},
EventArgsPropertyNames=new string[]{"Tool.Key"}},

};

//Read
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List));
string cd = Settings.Default.Inclusion;
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(cd));

List li2 =(List) ser.ReadObject(ms);
ms.Close();

//write to see the format
Type t = li.GetType();
DataContractJsonSerializer ser2 = new DataContractJsonSerializer(t);
FileStream fs = new FileStream(@"c:\3.txt", FileMode.CreateNew);
ser.WriteObject(fs, li);
fs.Flush();
fs.Close();