Skip to content

Instantly share code, notes, and snippets.

@LGM-AdrianHum
LGM-AdrianHum / UriToCachedImageConverter.cs
Last active October 27, 2015 03:17
Allows you to bind a property in the form of a uri to be bound to an image control, and then cache the image locally on a folder/file location. You will need to ensure that you change the local cache folder.
using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;
namespace MyNamespace{
public sealed class UriToCachedImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
@LGM-AdrianHum
LGM-AdrianHum / TextSearchFilter.cs
Created October 27, 2015 03:18
Bind a TextBox to filter a listbox...
public class TextSearchFilter {
public TextSearchFilter(ICollectionView filteredView, TextBox textBox) {
string filterText = "";
filteredView.Filter = delegate(object obj) {
if (String.IsNullOrEmpty(filterText)) return true;
string str = obj as string;
if (String.IsNullOrEmpty(str)) return false;
@LGM-AdrianHum
LGM-AdrianHum / NaturalStringComparer.cs
Created October 27, 2015 03:20
Allows you to sort an array of strings in a natural order where the numeric component of the string is of random length.
void Main(){
new[] {"a4","a3","a2","a10","b5","b4","b400","1","C1d","c1d2"}.OrderBy(x => x, new NaturalStringComparer()).Dump();
}
public class NaturalStringComparer : IComparer<string>{
private static readonly Regex _re = new Regex(@"(?<=\D)(?=\d)|(?<=\d)(?=\D)", RegexOptions.Compiled);
public int Compare(string x, string y)
{
x = x.ToLower();
@LGM-AdrianHum
LGM-AdrianHum / Readme.MD
Last active October 29, 2015 23:23
A simple ValueConverter that can change visibility when a text value that is bound to it is null or empty

#Using a ValueConverter to make a control visible when there is string content.

Sometimes it is useful to have a control completely disappear when it or another value has no text value. In this case, it is handy to be able to bind the control to the string value, and trigger a visibility change when it has not value. You can also achieve this by using a data trigger on a style.

@LGM-AdrianHum
LGM-AdrianHum / KatTorrentViewModel.cs
Last active October 31, 2015 08:30
Simple Class To Get A Query From kat.cr and process it to a result set.
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
@LGM-AdrianHum
LGM-AdrianHum / FizzBuzz.cs
Last active November 4, 2015 03:27
A LINQ/Tuple<> solution to FizzBuzz
void DoFizzBuzz()
{
var combinations = new List<Tuple<int, string>>
{
new Tuple<int, string> (3, "Fizz"),
new Tuple<int, string> (5, "Buzz"),
};
Func<int, int, bool> isMatch = (i, comb) => i % comb == 0;
@LGM-AdrianHum
LGM-AdrianHum / MakeXmlRpcCallTheHardWay.rc
Created November 5, 2015 04:46
This is the brute force way of making an XmlRpc call in C#
static void MakeXmlRpcCallTheHardWay()
{
byte[] requestData = Encoding.ASCII.GetBytes("<?xml version=\"1.0\"?><methodCall><methodName>Bugzilla.version</methodName></methodCall>");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://bugzilla.mozilla.org/xmlrpc.cgi");
request.Method = "POST";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729;)";
request.ContentType = "text/xml";
request.ContentLength = requestData.Length;
@LGM-AdrianHum
LGM-AdrianHum / ViewModelBase.cs
Created November 6, 2015 04:24
Yet another ViewModelBase
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Collections.Specialized;
using System.Linq.Expressions;
namespace MVVMExamples
{
/// <summary>
@LGM-AdrianHum
LGM-AdrianHum / program.cs
Created January 8, 2016 09:42
Start Process Inside The Same Console Application Window
private static void Main()
{
Console.WriteLine( "Hello" );
var p = new Process();
p.StartInfo = new ProcessStartInfo( @"c:\windows\system32\netstat.exe", "-n" )
{
UseShellExecute = false
};
@LGM-AdrianHum
LGM-AdrianHum / ResizeImage.cs
Created January 12, 2016 22:42
Image Management Code (Cropping And Resizing) In C#
public static Image ResizeImage(Image imgToResize, Size destinationSize)
{
var originalWidth = imgToResize.Width;
var originalHeight = imgToResize.Height;
//how many units are there to make the original length
var hRatio = (float)originalHeight/destinationSize.Height;
var wRatio = (float)originalWidth/destinationSize.Width;
//get the shorter side