Skip to content

Instantly share code, notes, and snippets.

@riyadparvez
riyadparvez / StreamTokenizer.cs
Last active February 6, 2024 07:40
C# port of java's StreamTokenizer class. Everything kept same except some renaming for following C# naming convention. And it also implements IEnumerable for foreach support
/**
* The <code>StreamTokenizer</code> class takes an input stream and
* parses it into "tokens", allowing the tokens to be
* Read one at a time. The parsing process is controlled by a table
* and a number of flags that can be set to various states. The
* stream tokenizer can recognize identifiers, numbers, quoted
* strings, and various comment styles.
* <p>
* Each byte Read from the input stream is regarded as a character
* in the range <code>'&#92;u0000'</code> through <code>'&#92;u00FF'</code>.
@riyadparvez
riyadparvez / FocusablePanel.cs
Last active December 10, 2015 10:08
This panel class gets focus. On the other hand default panel control tries to get away from focus. This code is taken from Hans Passant from stackoverlfow
//This panel will get focus as the original .NET panel always tries to get away from focus
public class FocusablePanel : Panel
{
public FocusablePanel()
{
this.SetStyle(ControlStyles.Selectable, true);
this.TabStop = true;
}
@riyadparvez
riyadparvez / Winform Flickering.cs
Created December 31, 2012 12:58
Add this code snippet into your form or control class to stop flickering in winform
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
@riyadparvez
riyadparvez / UnscrollableComboBox.cs
Created December 31, 2012 12:59
ComboBox class which doesn't catch mouse wheel event rather it passes to other controls
//Combobox will handle mouse wheel event, but if you want to combobox which won't handle mouse wheel
//event, rather pass to the parent, use this class
public class UnscrollableComboBox : ComboBox
{
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x20a && this.Parent != null)
{
PostMessage(this.Parent.Handle, m.Msg, m.WParam, m.LParam);
}
@riyadparvez
riyadparvez / Navigation Key Handler.cs
Created December 31, 2012 13:01
Add this code snippet to your class to handle navigational key press
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Up)
{
}
else if (keyData == Keys.Down)
{
}
@riyadparvez
riyadparvez / Connected Component Labeling.cs
Created January 2, 2013 12:03
Implementation of ConnectedComponentLabeling algorithm. It uses rooted tree data structure for members of a label. It's of O(n^2). I've used this algorithm for line detection in images (which are already converted to binary images using threshold, eliminating noise)
using System.Collections.Generic;
using System.Drawing;
namespace PatternRecognition
{
public class ConnectedComponentLabeling
{
readonly int[,] board;
readonly int w;
readonly int h;
@riyadparvez
riyadparvez / NonNullable.cs
Created January 6, 2013 09:09
Non-nullable reference types. It's from Jon Skeet
public struct NonNullable<T> where T : class
{
private readonly T value;
public NonNullable(T value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
@riyadparvez
riyadparvez / CsvUtils.cs
Created January 6, 2013 14:48
DataTable to CSV extension methods. Export to CSV file, CSV string
public static class CSV
{
public static string ToCSV(this DataTable table)
{
var columnHeaders = (from DataColumn x in table.Columns
select x.ColumnName).ToArray();
StringBuilder builder = new StringBuilder(String.Join(",", columnHeaders));
builder.Append("\n");
foreach (DataRow row in table.Rows)
@riyadparvez
riyadparvez / ReflectionUtilities.cs
Created January 6, 2013 14:50
Reflection utilities for C#. Get all fields, constructors, methods and properties
/// <summary>
/// Utilties for reflection
/// </summary>
public static class ReflectionUtils
{
/// <summary>
/// Get all the fields of a class
/// </summary>
/// <param name="type">Type object of that class</param>
/// <returns></returns>
@riyadparvez
riyadparvez / EnumDescription.cs
Created January 6, 2013 14:57
An attribute class for providing description for enum types in C# and util methods to retrieve description written for enum types(if that attribute is used0.)
/// <summary>
/// Provides a description for an enumerated type.
/// </summary>
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field,
AllowMultiple = false)]
public sealed class EnumDescriptionAttribute : Attribute
{
private string description;