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\VB\DemoSample\Form1.vb.
Imports System.ComponentModel
Imports System.Data
Imports System.Globalization
Imports Softelvdm.SftBoxNET
Imports Softelvdm.SftBoxNET.Controls
Public Class Form1
Private boxes() As SftBox
Private order As New Order()
Private previewFont As Font
Public Sub New()
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 Or FontListOptionsEnum.ShowTypeImage)
SftBox3.Text = "Segoe UI"
If SftBox3.SelectedItem Is Nothing AndAlso SftBox3.Items.Count > 0 Then
SftBox3.SelectedItem = SftBox3.Items(0)
End If
BindLookup()
FillCombo(ComboSearch, GetType(SearchModeEnum), SftBox5.SearchMode)
FillCombo(ComboAlignment, GetType(DropDownAlignmentEnum), SftBox1.DropDownAlignment)
FillCombo(ComboPosition, GetType(DropDownPositionEnum), SftBox1.DropDownPosition)
FillCombo(ComboNumbering, GetType(RowHeaderNumberingEnum), SftBox7.List.RowHeaders.Numbering)
FillCombo(ComboDarkMode, GetType(DarkModeEnum), SftBox1.DarkMode)
FillCombo(ComboHighContrast, GetType(HighContrastModeEnum), SftBox1.HighContrastMode)
End Sub
' 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 Sub Form1_DpiChanged(ByVal sender As Object, ByVal e As DpiChangedEventArgs) Handles Me.DpiChanged
BeginInvoke(New MethodInvoker(Sub()
For Each c As Control In GroupOptions.Controls
If Not TypeOf c Is ComboBox Then Continue For
Using other As New Font(Font.FontFamily, Font.Size + 1, Font.Style, Font.Unit)
c.Font = other
c.Font = Nothing
End Using
Next
End Sub))
End Sub
Private Sub FillCombo(ByVal combo As ComboBox, ByVal enumType As Type, ByVal current As Object)
For Each value As Object In [Enum].GetValues(enumType)
combo.Items.Add(value)
Next
combo.SelectedItem = current
End Sub
' The sizes follow the chosen font; the preview shows the font at the chosen or typed size.
Private Sub SftBox3_SelectedItemChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftBox3.SelectedItemChanged
Dim family As FontFamily = TryCast(SftBox3.SelectedValue, FontFamily)
If family Is Nothing Then Return
Dim size As String = SftBox4.Text
SftBox4.FillFontSizes(family)
SftBox4.Text = If(size.Length > 0, size, "12")
UpdatePreview()
End Sub
Private Sub SftBox4_SelectedItemChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftBox4.SelectedItemChanged
UpdatePreview()
End Sub
Private Sub SftBox4_EditTextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftBox4.EditTextChanged
UpdatePreview()
End Sub
Private Sub UpdatePreview()
Dim family As FontFamily = TryCast(SftBox3.SelectedValue, FontFamily)
Dim size As Single
If family Is Nothing OrElse Not Single.TryParse(SftBox4.Text, NumberStyles.Float, CultureInfo.CurrentCulture, size) OrElse size < 4 OrElse size > 72 Then Return
For Each style As FontStyle In New FontStyle() {FontStyle.Regular, FontStyle.Bold, FontStyle.Italic, FontStyle.Bold Or FontStyle.Italic}
If Not family.IsStyleAvailable(style) Then Continue For
Dim old As Font = previewFont
previewFont = New Font(family, size, style)
LabelPreview.Font = previewFont
If old IsNot Nothing Then old.Dispose()
Return
Next
End Sub
' A lookup: the list shows the countries of a DataTable, and SelectedValue, the chosen country's Id, is bound to an order.
Private Sub BindLookup()
Dim countries As New DataTable("Countries")
countries.Columns.Add("Id", GetType(Integer))
countries.Columns.Add("Name", GetType(String))
countries.Columns.Add("Capital", GetType(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
AddHandler order.PropertyChanged, Sub() ShowOrder()
SftBox6.DataBindings.Add("SelectedValue", order, "CountryId", False, DataSourceUpdateMode.OnPropertyChanged)
ShowOrder()
End Sub
Private Sub ShowOrder()
LabelOrder.Text = "order.CountryId = " & order.CountryId
End Sub
Private Sub ComboSearch_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboSearch.SelectedIndexChanged
SftBox5.SearchMode = CType(ComboSearch.SelectedItem, SearchModeEnum)
End Sub
Private Sub ComboAlignment_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboAlignment.SelectedIndexChanged
For Each box As SftBox In boxes
box.DropDownAlignment = CType(ComboAlignment.SelectedItem, DropDownAlignmentEnum)
Next
End Sub
Private Sub ComboPosition_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboPosition.SelectedIndexChanged
For Each box As SftBox In boxes
box.DropDownPosition = CType(ComboPosition.SelectedItem, DropDownPositionEnum)
Next
End Sub
' Row headers without contents show the row's number, the item's position among all items or an outline number.
Private Sub ComboNumbering_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboNumbering.SelectedIndexChanged
Dim numbering As RowHeaderNumberingEnum = CType(ComboNumbering.SelectedItem, RowHeaderNumberingEnum)
SftBox7.List.RowHeaders.Numbering = numbering
SftBox7.List.RowHeaders.Width = If(numbering = RowHeaderNumberingEnum.None, 0, 30)
End Sub
Private Sub ComboDarkMode_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboDarkMode.SelectedIndexChanged
For Each box As SftBox In boxes
box.DarkMode = CType(ComboDarkMode.SelectedItem, DarkModeEnum)
Next
End Sub
Private Sub ComboHighContrast_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboHighContrast.SelectedIndexChanged
For Each box As SftBox In boxes
box.HighContrastMode = CType(ComboHighContrast.SelectedItem, HighContrastModeEnum)
Next
End Sub
Private Sub CheckHeaders_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckHeaders.CheckedChanged
SftBox1.List.Headers.Visible = CheckHeaders.Checked
End Sub
' InitializeComponent sets the check boxes before the list of combo boxes exists
Private Sub CheckResizable_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckResizable.CheckedChanged
If boxes Is Nothing Then Return
For Each box As SftBox In boxes
box.DropDownResizable = CheckResizable.Checked
Next
End Sub
Private Sub CheckHotTrack_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckHotTrack.CheckedChanged
If boxes Is Nothing Then Return
For Each box As SftBox In boxes
box.HotTrackSelection = CheckHotTrack.Checked
Next
End Sub
Private Sub CheckDropOnFocus_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckDropOnFocus.CheckedChanged
If boxes Is Nothing Then Return
For Each box As SftBox In boxes
box.DropOnFocus = CheckDropOnFocus.Checked
Next
End Sub
End Class
' The object the lookup combo box is bound to.
Public Class Order
Implements INotifyPropertyChanged
Private countryIdValue As Integer
Public Property CountryId() As Integer
Get
Return countryIdValue
End Get
Set(ByVal value As Integer)
If countryIdValue = value Then Return
countryIdValue = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CountryId"))
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class