Skip to content

Instantly share code, notes, and snippets.

View Larry57's full-sized avatar

L4rry Larry57

View GitHub Profile
@Larry57
Larry57 / EventArgsGeneric.cs
Created June 3, 2011 13:33
Generic version of EventArgs
using System;
namespace iTracker.UI.Forms.Tools
{
public class EventArgs<T> : EventArgs
{
public T Content
{
get;
set;
@Larry57
Larry57 / treehelper.cs
Created June 3, 2011 13:39
Ancestors and Descendants for Tree handling
using System;
using System.Collections.Generic;
public static class Extensions
{
static public IEnumerable<T> Descendants<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> descendBy)
{
foreach (T value in source)
{
yield return value;
@Larry57
Larry57 / DbHelper.cs
Created June 6, 2011 07:51
Minimal Db Helper
namespace DbHelper
{
public class OleDbHelper : DbHelper<OleDbConnection>
{
public static DbParameter CreateParameter(string name, object value)
{
return new OleDbParameter(name, value);
}
}
@Larry57
Larry57 / DataReaderLinq.cs
Last active April 10, 2020 19:29
Linq enumerate a DataReader
/* How to use with a DataReader: */
public void Test()
{
var Res =
from Dr in MyDataReader.Enumerate()
select new {
ID = (Guid)Dr["ID"],
Description = Dr["Desc"] as string
};
@Larry57
Larry57 / StringTruncate.cs
Created October 18, 2012 06:26
Truncate a string too long for display. So, "A too long string" become "A too long s..." if limit is 12 chars.
public static string Truncate(this string myString, int limit, string symbol)
{
if (myString == null)
return null;
if (limit < 0)
throw new ArgumentOutOfRangeException("limit", limit, "must be 0 or greater");
if (symbol == null)
throw new ArgumentNullException("symbol must not be null");
@Larry57
Larry57 / DuckTryParse.cs
Last active October 11, 2015 19:48
Universal TryParse
public class DuckTryParse
{
public static bool TryParse<duck>(string stringToParse, out duck value)
{
var method = MethodCache<duck>.TryParse;
if (method == null || !method(stringToParse, out value))
{
value = default(duck);
return false;
@Larry57
Larry57 / DesignMode.cs
Created October 18, 2012 06:36
Unflawed Design Mode
new bool DesignMode 
{
get
{
return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
}
}
@Larry57
Larry57 / WindowsFormsHost.xaml
Created October 18, 2012 06:37
Magic xaml tip to make a WindowsFormsHost resize correctly when the application is embedded in a XBAP.
<WindowsFormsHost Margin="0" x:Name="winFormHost" FontSize="11.33" FontFamily="Microsoft Sans Serif" />
@Larry57
Larry57 / Clamp.cs
Created October 18, 2012 13:32
Clamp function
// Credits: http://stackoverflow.com/questions/2683442/where-can-i-find-the-clamp-function-in-net
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if(val.CompareTo(max) > 0) return max;
else return val;
}
@Larry57
Larry57 / program.cs
Created November 26, 2012 10:46
Force a single instance of an application by remote desktop session
static class Program
{
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)