Skip to content

Instantly share code, notes, and snippets.

View Larry57's full-sized avatar

L4rry Larry57

View GitHub Profile
@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)
@Larry57
Larry57 / gist:4202478
Created December 4, 2012 10:27
Hide horizontal scrollbar in a listview
[DllImport("user32")]
private static extern bool ShowScrollBar(IntPtr hwnd, int wBar, bool bShow);
void HideHorizontalScrollBar(ListView lv)
{
ShowScrollBar(lv.Handle, 0, false);
}
@Larry57
Larry57 / ObjectExtensions.cs
Last active October 21, 2015 06:03
Get and Set properties values using a path with dots ("user.address.street"...)
using System.Reflection;
namespace System
{
internal static class ObjectExtensions
{
public static PropertyInfo GetPropertyInfo(this Type type, string name)
{
string[] bits = name.Split('.');
for (int i = 0; i < bits.Length - 1; i++)
@Larry57
Larry57 / NameOf.cs
Last active November 19, 2015 09:57
Return the full path of a property name
// Inspired by http://stackoverflow.com/q/301809/24472
public static string Property<TProp>(Expression<Func<T, TProp>> expression)
{
var s = expression.Body.ToString();
var p = s.Remove(0, s.IndexOf('.') + 1);
return p;
}
// Example:
@Larry57
Larry57 / truncateMs.cs
Created January 18, 2013 10:02
Truncate milliseconds in a DateTime
DateTime truncateMs(DateTime date)
{
return new DateTime((date.Ticks / TimeSpan.TicksPerSecond) * TimeSpan.TicksPerSecond);
}
@Larry57
Larry57 / GenericMinMax
Created January 18, 2013 10:07
Generic Min Max functions
static T Max<T>(T x, T y)
{
return (Comparer<T>.Default.Compare(x, y) > 0) ? x : y;
}
static T Min<T>(T x, T y)
{
return (Comparer<T>.Default.Compare(x, y) > 0) ? y : x;
}
@Larry57
Larry57 / environment.cs
Created January 25, 2013 08:39
Get Environment variable, Create it if it does not exist.
static string GetEnvironment(string environmentVariableName, string defaultValue)
{
var Parameter = Environment.GetEnvironmentVariable(environmentVariableName, EnvironmentVariableTarget.Machine);
if (!string.IsNullOrEmpty(Parameter))
return Parameter;
Environment.SetEnvironmentVariable(environmentVariableName, defaultValue, EnvironmentVariableTarget.Machine);
return defaultValue;
}
@Larry57
Larry57 / GetDataGridViewHeight.cs
Last active December 12, 2015 05:28
Returns a DataGridView height to resize it correctly
int GetDataGridViewHeight(DataGridView dataGridView)
{
var sum = dataGridView.ColumnHeadersVisible ? dataGridView.ColumnHeadersHeight : 0 +
dataGridView.Rows.OfType<DataGridViewRow>().Where(r => r.Visible).Sum(r => r.Height);
return sum;
}
@Larry57
Larry57 / EventMask.cs
Last active December 13, 2015 21:39
Special class to handle Events Mask nicely
// Usage :
textBoxChangedEventMask = new EventMask(
() => { textBox1.TextChanged += new EventHandler(textBox1_TextChanged); },
() => { textBox1.TextChanged -= new EventHandler(textBox1_TextChanged); }
);
//in the event code:
try
{