Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created April 7, 2010 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpoehls/358878 to your computer and use it in GitHub Desktop.
Save jpoehls/358878 to your computer and use it in GitHub Desktop.
WPF & WinForms Control Extensions
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Forms;
namespace Samples
{
public static class WinFormsControlExtensions
{
public static string GetSelectedText(this ComboBox combo)
{
return GetSelectedText(combo, false);
}
public static string GetSelectedText(this ComboBox combo, bool ignoreFirstItem)
{
if ((ignoreFirstItem && combo.SelectedIndex == 0)
|| combo.SelectedItem == null)
{
return null;
}
return combo.SelectedItem.ToString();
}
public static void SetSoloTab(this TabControl tabControl, TabPage tabPage)
{
var removeTabs = new List<TabPage>(tabControl.TabPages.Count - 1);
foreach (TabPage tab in tabControl.TabPages)
{
if (tab != tabPage)
{
removeTabs.Add(tab);
}
}
foreach (TabPage tab in removeTabs)
{
tabControl.TabPages.Remove(tab);
}
if (!tabControl.TabPages.Contains(tabPage))
{
tabControl.TabPages.Add(tabPage);
}
tabControl.SelectTab(tabPage);
}
public static void BindToEnum<T>(this ComboBox comboBox)
where T : struct
{
BindToEnum(comboBox, typeof(T));
}
public static void BindToEnum(this ComboBox comboBox, Type enumType)
{
if (!enumType.IsEnum)
{
throw new ArgumentException("Target type must be an enum.");
}
var items = new Dictionary<string, object>();
var values = Enum.GetValues(enumType);
foreach (Enum value in values)
{
items.Add(value.GetDescription(), value);
}
if (EnumHelper.GetSortMode(enumType) == EnumSortMode.ByName)
{
items = items.OrderBy(x => x.Key, StringComparer.OrdinalIgnoreCase).ToDictionary(x => x.Key, x => x.Value);
}
comboBox.Items.Clear();
comboBox.DataSource = new BindingSource(items, null);
comboBox.DisplayMember = "Key";
comboBox.ValueMember = "Value";
}
public static void BindTo(this ComboBox comboBox, IEnumerable items, bool includeDefaultOption)
{
BindTo(comboBox, items, includeDefaultOption, "All");
}
public static void BindTo(this ComboBox comboBox, IEnumerable items, bool includeDefaultOption, string defaultOptionText)
{
comboBox.Items.Clear();
if (includeDefaultOption)
{
comboBox.Items.Add(defaultOptionText);
}
foreach (object item in items)
{
if (item == null)
continue;
comboBox.Items.Add(item);
}
comboBox.SelectedIndex = 0;
}
public static C SetDataProperty<C, T>(this C column, Expression<Func<T>> property) where C : DataGridViewColumn
{
var propertyInfo = ((MemberExpression)property.Body).Member as PropertyInfo;
if (propertyInfo == null)
{
throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
}
column.DataPropertyName = propertyInfo.Name;
return column;
}
public static DataTable ToDataTable(this DataGridView gridView)
{
DataTable table = new DataTable();
foreach (DataGridViewColumn gridColumn in gridView.Columns)
{
DataColumn dc = new DataColumn();
dc.ColumnName = gridColumn.HeaderText;
dc.DataType = typeof(string);
table.Columns.Add(dc);
}
foreach (DataGridViewRow gridRow in gridView.Rows)
{
DataRow dr = table.NewRow();
for (int i = 0; i < gridView.Columns.Count; i++)
{
dr[i] = gridRow.Cells[i].FormattedValue.ToString();
}
table.Rows.Add(dr);
}
return table;
}
public static void ResetMinAndMaxDates(this DateTimePicker picker)
{
picker.MinDate = new DateTime(1973, 1, 1);
picker.MaxDate = new DateTime(9998, 12, 31);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
namespace Samples
{
public static class WpfControlExtensions
{
public static void BindToEnum<T>(this ComboBox comboBox, bool includeDefaultOption)
{
BindToEnum<T>(comboBox, includeDefaultOption, "All");
}
public static void BindToEnum<T>(this ComboBox comboBox, bool includeDefaultOption, string defaultOptionText)
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Target type must be an enum.");
}
IOrderedEnumerable<string> names = Enum.GetNames(typeof(T)).OrderBy(x => x, StringComparer.OrdinalIgnoreCase);
var nameList = new List<string>(names);
BindTo(comboBox, nameList, includeDefaultOption, defaultOptionText);
}
public static void BindTo(this ComboBox comboBox, IEnumerable items, bool includeDefaultOption)
{
BindTo(comboBox, items, includeDefaultOption, "All");
}
public static void BindTo(this ComboBox comboBox, IEnumerable items, bool includeDefaultOption, string defaultOptionText)
{
comboBox.Items.Clear();
if (includeDefaultOption)
{
comboBox.Items.Add(defaultOptionText);
}
foreach (object item in items)
{
if (item == null)
continue;
comboBox.Items.Add(item);
}
comboBox.SelectedIndex = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment