Skip to content

Instantly share code, notes, and snippets.

View Larry57's full-sized avatar

L4rry Larry57

View GitHub Profile
@Larry57
Larry57 / Zoom.cs
Created September 15, 2015 10:26
Calculate a zoom
// sourceSize = source control size
// targetSize = target control size
// center of the zoom
// zoom radius
static Rectangle Zoom(Size sourceSize, Size targetSize, Point center, int radius)
{
var f = Math.Min((double)targetSize.Width / ((double)radius * 2d), (double)targetSize.Height / ((double)radius * 2d));
var loc = new Point((int)((-center.X + radius) * f), (int)((-center.Y + radius) * f));
@Larry57
Larry57 / TeaFileBinarySearch.cs
Created December 13, 2013 21:15
This extension method performs a BinarySearch in a TeaFile file. http://discretelogics.com/teafiles
public static int BinarySearch<T, U>(this TeaFile<T> tf, U target, Func<T, U> indexer) where T : struct
{
var lo = 0;
var hi = (int)tf.Count - 1;
var comp = Comparer<U>.Default;
while(lo <= hi)
{
var median = lo + (hi - lo >> 1);
var num = comp.Compare(indexer(tf.Items[median]), target);
@Larry57
Larry57 / TreeViewExtensions.cs
Last active December 29, 2015 11:39
Save / Restore the TreeView nodes expansion state before and after a Refresh.
using System.Collections.Generic;
using System.Linq;
namespace System.Windows.Forms
{
internal static class TreeViewExtensions
{
internal static List<string> GetExpansionState(this TreeNodeCollection nodes)
{
return nodes.Descendants()
@Larry57
Larry57 / ZedGraphExtensions.cs
Last active December 28, 2015 15:49
ZedGraph extensions. - Independant Pan and Zoom Y axes - YAxis order change in GraphPane - GraphPane order change in MasterPane
public static class ZedGraphExtensions
{
/// <summary>
/// Change the Y axes order
/// </summary>
/// <param name="list">YAxisList</param>
/// <param name="pane">GraphPane</param>
/// <param name="index">index</param>
/// <param name="relativePos">relative position</param>
/// <returns>new index</returns>
@Larry57
Larry57 / GetUniqueKey.cs
Created October 17, 2013 07:41
Returns a new unique key from an existing IEnumerable<string>. The key is based on a prefix followed by a number. For example, in "Customer_0", "Customer_1", "Customer_4"..., GetUniqueKey will return "Customer_2".
public static class Extensions
{
public static string GetUniqueKey(this IEnumerable<string> keys, string prefix, StringComparison comparisonType = StringComparison.InvariantCultureIgnoreCase)
{
int count = 0;
while (keys.FirstOrDefault(k => k.StartsWith(prefix + count, comparisonType)) != null)
count++;
return prefix + count;
}
@Larry57
Larry57 / MultiSelectTreeView.cs
Created October 15, 2013 11:47
Multi select TreeView found on Code Project - http://www.codeproject.com/Articles/20581/Multiselect-Treeview-Implementation. This nice little piece of work requires some attention.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Graph.UI.Tools
{
public class MultiSelectTreeview : TreeView
{
#region Selected Node(s) Properties
@Larry57
Larry57 / arrangeItems.cs
Last active December 17, 2015 15:19
Arrange items in a panel like a thumbnails manager
public static void Arrange(Control container, int horizontalGap, int verticalGap, float aspectRatio)
{
var H = container.Height;
var W = container.Width;
var N = container.Controls.OfType<Control>().Count(c => c.Visible);
if (N == 0)
return;
var bestSizedItem = (
/// <summary>
/// Represents a wildcard running on the
/// <see cref="System.Text.RegularExpressions"/> engine.
/// </summary>
public class Wildcard : Regex
{
/// <summary>
/// Initializes a wildcard with the given search pattern.
/// </summary>
/// <param name="pattern">The wildcard pattern to match.</param>
@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
{
/* 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;