User Control and Binding - vb.net

I've created a custom control which consists of two radio buttons with their appearance set to "Button". This control is meant to act like two toggle buttons with an "On" toggle button and an "Off" toggle button. I have a public boolean property "isAOn" which is used to set the state of both buttons and I want to be able to bind this property to boolean values. I have imported the component model and set
Now when I add this to a form for a boolean value in one of my classes it doesn't seem to update the boolean value of the class when I change the button pressed.
Advice on how to resolve this issue and constructive criticism on class design is more than welcome.
Thanks!
Here is the code:
Imports System.ComponentModel
''#
<DefaultBindingProperty("isAOn")> _
Public Class ToggleButtons
Private _isAOn As Boolean
Private _aText As String
Private _bText As String
Private _aOnColor As Color
Private _aOffColor As Color
Private _bOnColor As Color
Private _bOffColor As Color
Public Sub New()
''# This call is required by the Windows Form Designer.
InitializeComponent()
''# Add any initialization after the InitializeComponent() call.
aOffColor = Color.LightGreen
aOnColor = Color.DarkGreen
bOffColor = Color.FromArgb(255, 192, 192)
bOnColor = Color.Maroon
isAOn = False
aText = "A"
bText = "B"
End Sub
Private Sub configButtons()
If _isAOn Then
rbA.Checked = True
rbA.BackColor = _aOnColor
rbB.Checked = False
rbB.BackColor = _bOffColor
Else
rbA.Checked = False
rbA.BackColor = _aOffColor
rbB.Checked = True
rbB.BackColor = _bOnColor
End If
rbA.Text = aText
rbB.Text = bText
End Sub
Public Property isAOn() As Boolean
Get
Return _isAOn
End Get
Set(ByVal value As Boolean)
_isAOn = value
configButtons()
End Set
End Property
Private Sub rbOn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbA.CheckedChanged
isAOn = rbA.Checked
End Sub
Private Sub rbOff_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbB.CheckedChanged
isAOn = Not rbB.Checked
End Sub
Public Property aText() As String
Get
Return _aText
End Get
Set(ByVal value As String)
_aText = value
End Set
End Property
Public Property bText() As String
Get
Return _bText
End Get
Set(ByVal value As String)
_bText = value
End Set
End Property
Public Property aOnColor() As Color
Get
Return _aOnColor
End Get
Set(ByVal value As Color)
_aOnColor = value
End Set
End Property
Public Property aOffColor() As Color
Get
Return _aOffColor
End Get
Set(ByVal value As Color)
_aOffColor = value
End Set
End Property
Public Property bOnColor() As Color
Get
Return _bOnColor
End Get
Set(ByVal value As Color)
_bOnColor = value
End Set
End Property
Public Property bOffColor() As Color
Get
Return _bOffColor
End Get
Set(ByVal value As Color)
_bOffColor = value
End Set
End Property
End Class

You need to add an isAOnChanged event and raise it in the property setter.
By the way, properties and methods in .Net should be UpperCamelCased.

Related

DataGridView column of arbitrary controls per row (or replicate that appearance)

I have a DataGridView which contains, among other things, a "Feature" column and a "Value" column. The "Feature" column is a DataGridViewComboBoxColumn. What I would like to do, is manipulate the "Value" column, such that it can be one of a number of controls, depending on the item selected in the "Feature" column on any given row. So, for example :
Feature
Value Cell Behaviour
A
Combobox with a pre-determined set of options, specific to Feature A
B
Combobox with a different set of pre-determined options, specific to Feature B
C
Checkbox
D
Textbox (free-format)
X
etc. etc.
My initial naive approach to this (which I never really expected to work but figured I'd try anyway...) was to programmatically manipulate the specific value cell in the grid whenever the Feature combobox on the same row was changed :
Private Sub dgv_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellValueChanged
Dim changedCell As DataGridViewCell = CType(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewCell)
If changedCell.ColumnIndex = cboFeatureColumn.Index Then
Dim cboChangedFeature = CType(changedCell, DataGridViewComboBoxCell)
If cboChangedFeature.Value IsNot Nothing Then ConfigureValueField(cboChangedFeature)
End If
End Sub
Private Sub ConfigureValueField(cboFeature As DataGridViewComboBoxCell)
Dim cllValueField As DataGridViewCell = dgv.Rows(cboFeature.RowIndex).Cells(valueColumn.Index)
Dim featureID As Integer = dgv.Rows(cboFeature.RowIndex).Cells(featureIDColumn.Index).Value
Dim matches = From row In dtbFeatureList Let lookupID = row.Field(Of Integer)("ID") Where lookupID = featureID
Dim strFieldControl As String = ""
If matches.Any Then strFeatureControl = matches.First().row.Field(Of String)("FieldControl")
Select Case strFieldControl
Case "Checkbox"
' Do something
Case "Textbox"
' Do something
Case "Combobox"
Dim cboConfigured As New DataGridViewComboBoxCell
Dim dvOptions As New DataView(dtbFeatureValueList)
dvOptions.RowFilter = "[FeatureID] = " & featureID
Dim dtbOptions As DataTable
dtbOptions = dvOptions.ToTable
With cboConfigured
.DataSource = dtbOptions
.ValueMember = "Value"
.DisplayMember = "ValueText"
.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
.ReadOnly = False
End With
cllValueField = cboConfigured
End Select
End Sub
But this (probably, obviously to many) doesn't work; for starters it throws a DataError Default Error Dialog :
The following exception occurred in the DataGridView:
System.FormatException: DataGridViewComboBoxCell value is not valid To
replace this default dialog please handle the DataError event.
...which I can trap and handle (i.e. ignore!) easily enough :
Private Sub dgv_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles dgv.DataError
e.Cancel = True
End Sub
...and the resulting cell does have the appearance of a combobox but nothing happens when I click the dropdown (no list options appear) I suspect there are a whole host of DataErrors being thrown; to be quite honest, I'm not entirely comfortable with ignoring exceptions like this without handling them properly...
The only alternative option I can think of is to add separate columns for each possible value type (so add a DataGridViewComboBoxColumn for combos, a DataGridViewCheckBoxColumn for checkboxes, a DataGridViewTextBoxColumn for textboxes etc.) but then all of my values are scattered across multiple columns instead of all under a single heading which will be really confusing to look at.
And I really want to retain the appearance of comboboxes (set list of options) versus checkboxes (boolean values) versus textboxes (free-format text), as it makes it a lot easier for users to differentiate the values.
I read somewhere that it may be possible to derive my own custom column class, inheriting the native DataGridViewColumn class, but I couldn't find any examples of how this was done. I've done something similar with a customised DataGridViewButtonColumn but literally just to change the appearance of the buttons across the entire column, not the functionality of the individual cells within it.
Would anybody have any suggestions as to how it might be possible to have a mix of controls in the same column, configured specifically to the row in which they reside?
EDIT
So, I followed the walkthrough at : https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-host-controls-in-windows-forms-datagridview-cells?view=netframeworkdesktop-4.8&redirectedfrom=MSDN
And I've added four new classes to my project as follows :
UserControl class : a basic control which contains a textbox, a combobox and a checkbox, and basic methods for showing/hiding each one as appropriate, configuring the combobox if necessary etc. By default, the UserControl should display as an empty textbox.
CustomConfigurableCellEditingControl : derived from the UserControl in #1
DataGridViewCustomConfigurableCell : container for the EditingControl in #2 and derived from DataGridViewTextBoxCell
DataGridViewCustomConfigurableColumn : container for the Cell in #3 and derived from DataGridViewColumn
Public Class UserControl
Private displayControl As String
Private displayValue As String
Private myValue As String
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
displayControl = "Textbox"
displayValue = ""
refreshDisplay()
End Sub
Public Property ControlToDisplay As String
Get
Return displayControl
End Get
Set(value As String)
displayControl = value
End Set
End Property
Public Property ValueToDisplay As String
Get
Return displayValue
End Get
Set(value As String)
displayValue = value
End Set
End Property
Public Property Value As String
Get
Return myValue
End Get
Set(value As String)
myValue = value
End Set
End Property
Public Sub refreshDisplay()
Select Case displayControl
Case "Textbox"
With ucTextBox
.Text = displayValue
.Visible = True
End With
ucComboBox.Visible = False
ucCheckbox.Visible = False
Case "Combobox"
With ucComboBox
.Visible = True
End With
ucTextBox.Visible = False
ucCheckbox.Visible = False
Case "Checkbox"
With ucCheckbox
.Checked = myValue
.Visible = True
End With
ucTextBox.Visible = False
ucComboBox.Visible = False
End Select
End Sub
Public Sub configureCombobox(dtb As DataTable, valueMember As String, displayMember As String, style As ComboBoxStyle)
With ucComboBox
.DataSource = dtb
.ValueMember = "ID"
.DisplayMember = "FriendlyName"
.DropDownStyle = style
End With
End Sub
End Class
Class CustomConfigurableCellEditingControl
Inherits UserControl
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Public Sub New()
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
'Return Me.Value.ToShortDateString()
Return Me.valueIsChanged.ToString
End Get
Set(ByVal value As Object)
Me.Value = value
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.valueIsChanged.ToString
End Function
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
End Sub
Public Property EditingControlRowIndex() As Integer _
Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal key As Keys,
ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
Implements IDataGridViewEditingControl.EditingControlWantsInputKey
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean _
Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean _
Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public ReadOnly Property EditingControlCursor() As Cursor _
Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
' Notify the DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)
End Sub
End Class
Public Class DataGridViewCustomConfigurableCell
Inherits DataGridViewTextBoxCell
Public Sub New()
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer,
ByVal initialFormattedValue As Object,
ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As CustomConfigurableCellEditingControl = CType(DataGridView.EditingControl, CustomConfigurableCellEditingControl)
' Use the default row value when Value property is null.
If (Me.Value Is Nothing) Then
ctl.Value = CType(Me.DefaultNewRowValue, String)
Else
ctl.Value = CType(Me.Value, String)
End If
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing control that Cell uses.
Return GetType(CustomConfigurableCellEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
' Return the type of the value that Cell contains.
Return GetType(String)
End Get
End Property
Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
Return ""
End Get
End Property
End Class
Imports System.Windows.Forms
Public Class DataGridViewCustomConfigurableColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New DataGridViewCustomConfigurableCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a Custom Configurable Cell.
If (value IsNot Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(DataGridViewCustomConfigurableCell)) Then
Throw New InvalidCastException("Must be a Custom Configurable Cell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
But... I'm still none the wiser as to how I populate, display, manipulate etc. The code compiles fine, but I just get a blank column. I can't see any controls, I can't see any values and I can't seem to "trap" any of the events that should manipulate them?
With dgv
cfgValueColumn = New DataGridViewCustomConfigurableColumn With {.DisplayIndex = valueColumn.Index + 1}
With cfgValueColumn
.HeaderText = "Custom Value"
.Width = 300
End With
.Columns.Add(cfgValueColumn)
Dim cfgCell As DataGridViewCustomConfigurableCell
For Each row As DataGridViewRow In .Rows
cfgCell = CType(row.Cells(cfgValueColumn.Index), DataGridViewCustomConfigurableCell)
With cfgCell
.Value = row.Cells(valueColumn.Index).Value
End With
Next
End With
Your main approach is correct if you just need to change the type of the given cell based on the ComboBox selection.
When the value changes of the ComboBox cell:
Dispose of the current cell of the target column.
Create a new cell whose type is defined by the ComboBox selection.
Assign the new cell to the grid place of the old one.
Here's a working example.
Private Sub dgv_CellValueChanged(
sender As Object,
e As DataGridViewCellEventArgs) Handles dgv.CellValueChanged
If e.RowIndex < 0 Then Return
If e.ColumnIndex = dgvcTypeSelector.Index Then
Dim cmb = DirectCast(dgv(e.ColumnIndex, e.RowIndex), DataGridViewComboBoxCell)
Dim selItem = cmb.FormattedValue.ToString()
Dim valSelCell = dgv(dgvcValueSelector.Index, e.RowIndex)
valSelCell.Dispose()
Select Case selItem
Case "Text"
valSelCell = New DataGridViewTextBoxCell With {
.Value = "Initial value If any."
}
Case "Combo"
valSelCell = New DataGridViewComboBoxCell With {
.ValueMember = "Value",
.DisplayMember = "ValueText",
.DataSource = dt,
.Value = 2 ' Optional...
}
Case "Check"
valSelCell = New DataGridViewCheckBoxCell With {
.ValueType = GetType(String),
.Value = True
}
valSelCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
Case "Button"
valSelCell = New DataGridViewButtonCell With {
.Value = "Click Me!"
}
valSelCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
End Select
dgv(dgvcValueSelector.Index, e.RowIndex) = valSelCell
ElseIf e.ColumnIndex = dgvcValueSelector.Index Then
Dim cell = dgv(e.ColumnIndex, e.RowIndex)
Console.WriteLine($"{cell.Value} - {cell.FormattedValue}")
End If
End Sub
Private Sub dgv_CurrentCellDirtyStateChanged(
sender As Object,
e As EventArgs) Handles dgv.CurrentCellDirtyStateChanged
If dgv.IsCurrentCellDirty Then
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
' If you need to handle the Button cells.
Private Sub dgv_CellContentClick(
sender As Object,
e As DataGridViewCellEventArgs) Handles dgv.CellContentClick
If e.RowIndex >= 0 AndAlso
e.ColumnIndex = dgvcValueSelector.Index Then
Dim btn = TryCast(dgv(e.ColumnIndex, e.RowIndex), DataGridViewButtonCell)
If btn IsNot Nothing Then
Console.WriteLine(btn.FormattedValue)
End If
End If
End Sub
Note, I've changed the value type of the check box cell by ValueType = GetType(String) to avoid throwing exceptions caused by the different value types of the check box and main columns. I'd assume the main column here is of type DataGridViewTextBoxColumn. So you have String vs. Boolean types. If you face problems in the data binding scenarios, then just swallow the exception.
Private Sub dgv_DataError(
sender As Object,
e As DataGridViewDataErrorEventArgs) Handles dgv.DataError
If e.RowIndex >= 0 AndAlso e.ColumnIndex = dgvcValueSelector.Index Then
e.ThrowException = False
End If
End Sub

List.Add(var) overwrites previous items in list

Every time when you press the button, you should get a customer added to your list. But it just overrides my previous value and doesn't update the List.
This is exactly what they did in my book, but I don't know why the next variable doesn't get added to the list
Private Sub btnOpslaan_Click(sender As Object, e As EventArgs) Handles btnOpslaan.Click
Dim klantenlijst As New List(Of Klant)
Dim nieuwe_klant As New Klant
Dim path As String = IO.Path.GetTempFileName()
nieuwe_klant.Naam = txtNaam.Text
nieuwe_klant.Straat = txtStraat.Text
nieuwe_klant.Postcode = txtPostcode.Text
nieuwe_klant.Gemeente = txtGemeente.Text
nieuwe_klant.Telefoon = txtTelefoon.Text
nieuwe_klant.Email = txtEmail.Text
If chkHardware.Checked = True Then
nieuwe_klant.Hardware = True
End If
If chkInternet.Checked = True Then
nieuwe_klant.Internet = True
End If
If chkMultimedia.Checked = True Then
nieuwe_klant.Multimedia = True
End If
If chkSoftware.Checked = True Then
nieuwe_klant.Software = True
End If
klantenlijst.Add(nieuwe_klant)
MsgBox(klantenlijst.Count)
End Sub
My class "Klant"
Public Class Klant
Private mNaam As String
Private mStraat As String
Private mPostcode As String
Private mGemeente As String
Private mTelefoon As String
Private mEmail As String
Private mHardware As Boolean
Private mSoftware As Boolean
Private mInternet As Boolean
Private mMultimedia As Boolean
Public Sub New()
mHardware = False
mInternet = False
mSoftware = False
mMultimedia = False
mNaam = "Niet ingevuld"
mStraat = "Niet ingevuld"
mPostcode = "Niet ingevuld"
mGemeente = "Niet ingevuld"
mTelefoon = "Niet ingevuld"
mEmail = "Niet ingevuld"
End Sub
Public Property Hardware() As Boolean
Get
Return mHardware
End Get
Set(ByVal value As Boolean)
mHardware = value
End Set
End Property
Public Property Software() As Boolean
Get
Return mSoftware
End Get
Set(ByVal value As Boolean)
mSoftware = value
End Set
End Property
Public Property Internet() As Boolean
Get
Return mInternet
End Get
Set(ByVal value As Boolean)
mInternet = value
End Set
End Property
Public Property Multimedia() As Boolean
Get
Return mMultimedia
End Get
Set(ByVal value As Boolean)
mMultimedia = value
End Set
End Property
Public Property Naam() As String
Get
Return mNaam
End Get
Set(ByVal value As String)
mNaam = value
End Set
End Property
Public Property Straat() As String
Get
Return mStraat
End Get
Set(ByVal value As String)
mStraat = value
End Set
End Property
Public Property Postcode() As String
Get
Return mPostcode
End Get
Set(ByVal value As String)
mPostcode = value
End Set
End Property
Public Property Gemeente() As String
Get
Return mGemeente
End Get
Set(ByVal value As String)
mGemeente = value
End Set
End Property
Public Property Telefoon() As String
Get
Return mTelefoon
End Get
Set(ByVal value As String)
mTelefoon = value
End Set
End Property
Public Property Email() As String
Get
Return mEmail
End Get
Set(ByVal value As String)
mEmail = value
End Set
End Property
End Class
The issue here is that on every click, you are declaring a new 'klantenlijst' and making it private. Just declare it outside of the click and you'll get the desired result:
Dim klantenlijst As New List(Of Klant)
Private Sub btnOpslaan_Click(sender As Object, e As EventArgs) Handles btnOpslaan.Click
Dim nieuwe_klant As New Klant
Dim path As String = IO.Path.GetTempFileName()
nieuwe_klant.Naam = txtNaam.Text
//continue your code...
klantenlijst.Add(nieuwe_klant)
MsgBox(klantenlijst.Count)
Every time the button is clicked, you create a new list:
Dim klantenlijst As New List(Of Klant)
And then you add exactly one item to that list:
klantenlijst.Add(nieuwe_klant)
So that list will only ever contain one item.
Instead, create a class-level list and add to that. So put this line at the class level:
Dim klantenlijst As New List(Of Klant)
Then the same list is available throughout the instance of the class. A few things to note:
Without more context, it's possible that you may even be looking for a larger scope than the class. There are a variety of places you can store information, a class-level variable is simply the next higher scope from your method-level variable.
If you're using ASP.NET then the lifespan of the class is very short (per request) and not persisted between requests. In that case you'd want to store your data somewhere else, possibly in session state or a database.

custom datagridview column Control will not accept a period (.)

I cannot get a control I found online to accept a period(.). I need to be able to enter numeric values with decimal places. I wanted to use the numericupdown control in a datagridview cell so I can use the up-down arrows for adjusting values.
This control implments the NumericUpDown control as the editing control on a datagridview column. I found it online (don't remember where), and ti was based on a similar custom datagridview column based in a calendar control.
I made a few modifications to it so I could set the maximum, minimum, decimalplaces and imcrement properties.
However, even when decimal places is set to 2 and increment is .1, when I'm typing a value the control will not accept a period.
Below is the code, which includes the classes for the column, cell, and editing control. Please help. I have no clue what the problem is.
Public Class NumericUpDownColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New NumericUpDownCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a CalendarCell.
If Not (value Is Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(NumericUpDownCell)) _
Then
Throw New InvalidCastException("Must be a CalendarCell")
End If
MyBase.CellTemplate = value
End Set
End Property
Private _Maximum As Decimal = 100
Private _Minimum As Decimal = 0
Private _Increment As Decimal = 0.1
Private _DecimalPlaces As Integer = 2
Public Property DecimalPlaces() As Integer
Get
Return _DecimalPlaces
End Get
Set(ByVal value As Integer)
If _DecimalPlaces = value Then
Return
End If
_DecimalPlaces = value
End Set
End Property
Public Property Maximum() As Decimal
Get
Return _Maximum
End Get
Set(ByVal value As Decimal)
_Maximum = value
End Set
End Property
_
Public Property Minimum() As Decimal
Get
Return _Minimum
End Get
Set(ByVal value As Decimal)
_Minimum = value
End Set
End Property
_
Public Property Increment() As Decimal
Get
Return _Increment
End Get
Set(ByVal value As Decimal)
_Increment = value
End Set
End Property
End Class
Public Class NumericUpDownCell
Inherits DataGridViewTextBoxCell
Public Sub New()
' Use the short date format.
Me.Style.Format = "N2"
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal initialFormattedValue As Object, _
ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
Dim ctl As NumericUpDownEditingControl = _
CType(DataGridView.EditingControl, NumericUpDownEditingControl)
RemoveHandler ctl.Enter, AddressOf Me.OnNumericEnter
AddHandler ctl.Enter, AddressOf Me.OnNumericEnter
ctl.Maximum = CType(Me.DataGridView.Columns(Me.ColumnIndex), NumericUpDownColumn).Maximum
ctl.Minimum = CType(Me.DataGridView.Columns(Me.ColumnIndex), NumericUpDownColumn).Minimum
ctl.Increment = CType(Me.DataGridView.Columns(Me.ColumnIndex), NumericUpDownColumn).Increment
ctl.DecimalPlaces = CType(Me.DataGridView.Columns(Me.ColumnIndex), NumericUpDownColumn).DecimalPlaces
ctl.ThousandsSeparator = True
ctl.Value = CType(Me.Value, Decimal)
End Sub
'''
''' Handle on enter event of numeric
'''
'''
'''
'''
Private Sub OnNumericEnter(ByVal sender As Object, ByVal e As EventArgs)
Dim control As NumericUpDownEditingControl = CType(sender, NumericUpDownEditingControl)
Dim strValue As String = control.Value.ToString("N2")
control.Select(0, strValue.Length)
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing contol that CalendarCell uses.
Return GetType(NumericUpDownEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
' Return the type of the value that CalendarCell contains.
Return GetType(Decimal)
End Get
End Property
Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
' Use the current date and time as the default value.
Return 0
End Get
End Property
End Class
Class NumericUpDownEditingControl
Inherits NumericUpDown
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Public Sub New()
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Value.ToString("N2")
End Get
Set(ByVal value As Object)
If TypeOf value Is Decimal Then
Me.Value = Decimal.Parse(value)
End If
End Set
End Property
_
Public Function GetEditingControlFormattedValue(ByVal context _
As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.Value.ToString("N2")
End Function
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor
End Sub
Public Property EditingControlRowIndex() As Integer _
Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal key As Keys, _
ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
Implements IDataGridViewEditingControl.EditingControlWantsInputKey
' Let the DateTimePicker handle the keys listed.
Select Case key And Keys.KeyCode
'Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
' Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
Case Keys.Up, Keys.Down
Return True
Case Else
Return False
End Select
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() _
As Boolean Implements _
IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean _
Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public ReadOnly Property EditingControlCursor() As Cursor _
Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
' Notify the DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)
End Sub
End Class
I used an old programmers trick to solve this. I just downloaded the sample code from the post Plutonix referenced and just added the DLL it came with to my project. This work fine and saved me a lot of trouble I wasn't looking for.

Public Property keeps changing it's value by itself

This value needs to be .visible = True but...
Public Property Active_bool As Boolean
Get
Return btn_Begin.Visible
End Get
Set(ByVal value As Boolean)
btn_Begin.Visible = value
End Set
End Property
I can't change my value in properties with the drop down box. It literally won't select True! So I have to change the code in the designer.vb but as soon as I view my form1 designer the values change back to False!
Is there maybe a way to set a default value to this property?
The property is meant to see if a button on a UserControl is visible or not. If it's visible then it will initiate a sub.
Private Sub btn_Start_All_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Start_All.Click
Dim allActiveUserControls =
From uc_Index In Controls.OfType(Of LapTimerGUI)()
Where uc_Index.Active_bool
For Each User_Control In allActiveUserControls
User_Control.Start_Race()
Next
End Sub
You could try this but i have to ask what is the environment ( Winform, WebForm) and what is the purpose of this property?
dim _isEnabled = true
Public Property IsEnabled As Boolean
Get
Return _isEnabled
End Get
Set(ByVal value As Boolean)
_isEnabled = value
btn_Begin.Visible = _isEnabled
End Set
End Property

Adding a margin to custom datagridview column

I've used the following code, taken from here, to create my own custom column for datagridview, so that I can now have images and text in the same cell:
Public Class TextAndImageColumn
Inherits DataGridViewTextBoxColumn
Private imageValue As Image
Private m_imageSize As Size
Public Sub New()
Me.CellTemplate = New TextAndImageCell
End Sub
Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageColumn = TryCast(MyBase.Clone, TextAndImageColumn)
c.imageValue = Me.imageValue
c.m_imageSize = Me.m_imageSize
Return c
End Function
Public Property Image() As Image
Get
Return Me.imageValue
End Get
Set(ByVal value As Image)
Me.imageValue = value
Me.m_imageSize = value.Size
Dim inheritedPadding As Padding = Me.DefaultCellStyle.Padding
Me.DefaultCellStyle.Padding = New Padding(ImageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End Set
End Property
Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell
Get
Return TryCast(Me.CellTemplate, TextAndImageCell)
End Get
End Property
Friend ReadOnly Property ImageSize() As Size
Get
Return m_imageSize
End Get
End Property
End Class
Public Class TextAndImageCell
Inherits DataGridViewTextBoxCell
Private imageValue As Image
Private imageSize As Size
Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageCell = TryCast(MyBase.Clone, TextAndImageCell)
c.imageValue = Me.imageValue
c.imageSize = Me.imageSize
Return c
End Function
Public Property Image() As Image
Get
If Me.OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then
Return imageValue
Else
If Not (Me.imageValue Is Nothing) Then
Return Me.imageValue
Else
Return Me.OwningTextAndImageColumn.Image
End If
End If
End Get
Set(ByVal value As Image)
Me.imageValue = value
Me.imageSize = value.Size
Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
Me.Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End Set
End Property
Protected Overloads Overrides Sub Paint(graphics As Graphics, clipBounds As Rectangle, cellBounds As Rectangle,
rowIndex As Integer, cellState As DataGridViewElementStates, value As Object, formattedValue As Object,
errorText As String, cellStyle As DataGridViewCellStyle, advancedBorderStyle As DataGridViewAdvancedBorderStyle,
paintParts As DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
If Not (Me.Image Is Nothing) Then
Dim container As System.Drawing.Drawing2D.GraphicsContainer = graphics.BeginContainer
graphics.SetClip(cellBounds)
graphics.DrawImageUnscaled(Me.Image, cellBounds.Location)
graphics.EndContainer(container)
End If
End Sub
Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn
Get
Return TryCast(Me.OwningColumn, TextAndImageColumn)
End Get
End Property
End Class
It works very well, except that the image that I use is right at the edges of the cell. I'd like to give it a small margin. How can I do this?
You could add a property to TextAndImageCell like :
Private m_imagePadding As New Padding(3)
Public Property ImagePadding() As Padding
Get
Return m_imagePadding
End Get
Set(ByVal value As Padding)
m_imagePadding = value
End Set
End Property
and implement (in Paint) like :
graphics.DrawImageUnscaled(Me.Image, _
New Point(cellBounds.Location.X + m_imagePadding.Left, _
cellBounds.Location.Y + m_imagePadding.Top))
also would need to change in TextAndImageColumn :
Me.DefaultCellStyle.Padding = New Padding(ImageSize.Width + _
TextAndImageCellTemplate.ImagePadding.Right, inheritedPadding.Top, _
inheritedPadding.Right, inheritedPadding.Bottom)
There's room for refinement, obviously (trigger redraws on padding change, sort out row heights, text padding, etc) but something like this should work.