This sample shows combo boxes with columns, a hierarchy, font lists, Filter search, a lookup bound to a data source and a Simple style list with check boxes and row numbers, with options that change the search mode, the dropdown, dark mode, high contrast and other properties.
The source code is located at C:\Program Files (x86)\Softelvdm\SftBox NET 3.0\Samples\CSharp\DemoSample\Form1.cs.
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using Softelvdm.SftBoxNET;
using Softelvdm.SftBoxNET.Controls;
namespace DemoSample {
public partial class Form1 : Form {
private SftBox[] boxes;
private Order order = new Order();
private Font previewFont;
public Form1() {
InitializeComponent();
boxes = new SftBox[] { sftBox1, sftBox2, sftBox3, sftBox4, sftBox5, sftBox6, sftBox7 };
sftBox1.SelectedItem = sftBox1.Items[5];
sftBox2.SelectedItem = sftBox2.Items[0].Children[1];
// with the Build phase collapsed, the rows shown, all items and the outline are numbered differently
sftBox7.Items[1].Collapse(CollapseStyleEnum.All);
// the installed fonts, each name shown in its own font, with an image for TrueType and OpenType fonts
sftBox3.FillFontNames(FontListOptionsEnum.ShowInFont | FontListOptionsEnum.ShowTypeImage);
sftBox3.Text = "Segoe UI";
if (sftBox3.SelectedItem == null && sftBox3.Items.Count > 0)
sftBox3.SelectedItem = sftBox3.Items[0];
BindLookup();
FillCombo(comboSearch, typeof(SearchModeEnum), sftBox5.SearchMode);
FillCombo(comboAlignment, typeof(DropDownAlignmentEnum), sftBox1.DropDownAlignment);
FillCombo(comboPosition, typeof(DropDownPositionEnum), sftBox1.DropDownPosition);
FillCombo(comboNumbering, typeof(RowHeaderNumberingEnum), sftBox7.List.RowHeaders.Numbering);
FillCombo(comboDarkMode, typeof(DarkModeEnum), sftBox1.DarkMode);
FillCombo(comboHighContrast, typeof(HighContrastModeEnum), sftBox1.HighContrastMode);
DpiChanged += Form1_DpiChanged;
}
// On the .NET Framework, a ComboBox in a container keeps its old font when the window moves to a monitor with
// another scale. Setting another font and then the inherited one again makes it use the new one.
private void Form1_DpiChanged(object sender, DpiChangedEventArgs e) {
BeginInvoke(new MethodInvoker(delegate {
foreach (Control c in groupOptions.Controls) {
if (!(c is ComboBox)) continue;
using (Font other = new Font(Font.FontFamily, Font.Size + 1, Font.Style, Font.Unit)) {
c.Font = other;
c.Font = null;
}
}
}));
}
private void FillCombo(ComboBox combo, Type enumType, object current) {
foreach (object value in Enum.GetValues(enumType))
combo.Items.Add(value);
combo.SelectedItem = current;
}
// The sizes follow the chosen font; the preview shows the font at the chosen or typed size.
private void sftBox3_SelectedItemChanged(object sender, EventArgs e) {
FontFamily family = sftBox3.SelectedValue as FontFamily;
if (family == null)
return;
string size = sftBox4.Text;
sftBox4.FillFontSizes(family);
sftBox4.Text = size.Length > 0 ? size : "12";
UpdatePreview();
}
private void sftBox4_SelectedItemChanged(object sender, EventArgs e) {
UpdatePreview();
}
private void sftBox4_EditTextChanged(object sender, EventArgs e) {
UpdatePreview();
}
private void UpdatePreview() {
FontFamily family = sftBox3.SelectedValue as FontFamily;
float size;
if (family == null || !float.TryParse(sftBox4.Text, NumberStyles.Float, CultureInfo.CurrentCulture, out size) || size < 4 || size > 72)
return;
foreach (FontStyle style in new FontStyle[] { FontStyle.Regular, FontStyle.Bold, FontStyle.Italic, FontStyle.Bold | FontStyle.Italic }) {
if (!family.IsStyleAvailable(style))
continue;
Font old = previewFont;
previewFont = new Font(family, size, style);
labelPreview.Font = previewFont;
if (old != null)
old.Dispose();
return;
}
}
// A lookup: the list shows the countries of a DataTable, and SelectedValue, the chosen country's Id, is bound to an order.
private void BindLookup() {
DataTable countries = new DataTable("Countries");
countries.Columns.Add("Id", typeof(int));
countries.Columns.Add("Name", typeof(string));
countries.Columns.Add("Capital", typeof(string));
countries.Rows.Add(43, "Austria", "Vienna");
countries.Rows.Add(32, "Belgium", "Brussels");
countries.Rows.Add(1, "Canada", "Ottawa");
countries.Rows.Add(45, "Denmark", "Copenhagen");
countries.Rows.Add(33, "France", "Paris");
countries.Rows.Add(49, "Germany", "Berlin");
countries.Rows.Add(39, "Italy", "Rome");
countries.Rows.Add(81, "Japan", "Tokyo");
countries.Rows.Add(52, "Mexico", "Mexico City");
countries.Rows.Add(34, "Spain", "Madrid");
sftBox6.Columns.Count = 2;
sftBox6.Columns[0].DataMember = "Name";
sftBox6.Columns[1].DataMember = "Capital";
sftBox6.DropDownWidthMode = DropDownWidthModeEnum.Optimal;
sftBox6.DisplayMember = "Name";
sftBox6.ValueMember = "Id";
sftBox6.DataSource = countries;
order.CountryId = 81;
order.PropertyChanged += delegate { ShowOrder(); };
sftBox6.DataBindings.Add("SelectedValue", order, "CountryId", false, DataSourceUpdateMode.OnPropertyChanged);
ShowOrder();
}
private void ShowOrder() {
labelOrder.Text = "order.CountryId = " + order.CountryId;
}
private void comboSearch_SelectedIndexChanged(object sender, EventArgs e) {
sftBox5.SearchMode = (SearchModeEnum)comboSearch.SelectedItem;
}
private void comboAlignment_SelectedIndexChanged(object sender, EventArgs e) {
foreach (SftBox box in boxes)
box.DropDownAlignment = (DropDownAlignmentEnum)comboAlignment.SelectedItem;
}
private void comboPosition_SelectedIndexChanged(object sender, EventArgs e) {
foreach (SftBox box in boxes)
box.DropDownPosition = (DropDownPositionEnum)comboPosition.SelectedItem;
}
// Row headers without contents show the row's number, the item's position among all items or an outline number.
private void comboNumbering_SelectedIndexChanged(object sender, EventArgs e) {
RowHeaderNumberingEnum numbering = (RowHeaderNumberingEnum)comboNumbering.SelectedItem;
sftBox7.List.RowHeaders.Numbering = numbering;
sftBox7.List.RowHeaders.Width = numbering == RowHeaderNumberingEnum.None ? 0 : 30;
}
private void comboDarkMode_SelectedIndexChanged(object sender, EventArgs e) {
foreach (SftBox box in boxes)
box.DarkMode = (DarkModeEnum)comboDarkMode.SelectedItem;
}
private void comboHighContrast_SelectedIndexChanged(object sender, EventArgs e) {
foreach (SftBox box in boxes)
box.HighContrastMode = (HighContrastModeEnum)comboHighContrast.SelectedItem;
}
private void checkHeaders_CheckedChanged(object sender, EventArgs e) {
sftBox1.List.Headers.Visible = checkHeaders.Checked;
}
private void checkResizable_CheckedChanged(object sender, EventArgs e) {
foreach (SftBox box in boxes)
box.DropDownResizable = checkResizable.Checked;
}
private void checkHotTrack_CheckedChanged(object sender, EventArgs e) {
foreach (SftBox box in boxes)
box.HotTrackSelection = checkHotTrack.Checked;
}
private void checkDropOnFocus_CheckedChanged(object sender, EventArgs e) {
foreach (SftBox box in boxes)
box.DropOnFocus = checkDropOnFocus.Checked;
}
}
// The object the lookup combo box is bound to.
public class Order : INotifyPropertyChanged {
private int countryId;
public int CountryId {
get { return countryId; }
set {
if (countryId == value) return;
countryId = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("CountryId"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}