Skip to content

Instantly share code, notes, and snippets.

View Larry57's full-sized avatar

L4rry Larry57

View GitHub Profile
@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 / 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
{
@Larry57
Larry57 / SuspendDrawing.cs
Last active December 14, 2015 00:09
Suspend control drawing (useful for DataGridView)
// Credits: http://stackoverflow.com/a/778133/24472
[DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private const int WM_SETREDRAW = 0xB;
public static void SuspendDrawing(this Control target)
{
SendMessage(target.Handle, WM_SETREDRAW, 0, 0);
}
@Larry57
Larry57 / KeyboardHook.cs
Created April 11, 2013 18:08
A global Keyboard Hook that works in WPF and is also high CPU load proof. Credits: http://blogs.vertigo.com/personal/ralph/Blog/Lists/Posts/Post.aspx?ID=8
public class KeyboardHook
{
#region pinvoke details
private enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
/* This package contains a powerful, declarative command-line parsing system in a single .cs file.
* You can include this in any project with almost zero footprint and very easy/readable usage,
* as shown below. More switch types, including generic ones are coming soon. Visit the project
* page for a sample of how to use this handy package. Part of Code Blocks (http://codeblocks.codeplex.com)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@Larry57
Larry57 / WcfExtensions.cs
Created April 18, 2013 06:41
Extensions for Wcf to dispose a Client properly
/// <summary>
/// http://www.codeproject.com/Tips/197531/Do-not-use-using-for-WCF-Clients
/// </summary>
public static class WcfExtensions
{
public static void Using<T>(this T client, Action<T> work)
where T : ICommunicationObject
{
try
{