Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Go Back  Xtreme Visual Basic Talk > > > Implement Column/Cell/EditingControl derived classes, host custom ComboBox control


Reply
 
Thread Tools Display Modes
  #1  
Old 10-02-2015, 07:50 AM
VBobCat's Avatar
VBobCat VBobCat is offline
Freshman
 
Join Date: Aug 2009
Location: S„o Paulo, Brazil
Posts: 38
Default Implement Column/Cell/EditingControl derived classes, host custom ComboBox control


I have this custom Control class, derived from ComboBox. Its purpose is to provide a standard behavior for controls whose list of items will be populated depending on what user types in it as search criteria.
Code:
Imports System.ComponentModel
Public Class SearchBox
Inherits ComboBox

Private hasTextChanged As Boolean

Private _queryInterval As Integer = 3000
Public Property QueryInterval As Integer
Get
Return _queryInterval
End Get
Set(value As Integer)
_queryInterval = value
If _myTimer IsNot Nothing Then _myTimer.Interval = value
End Set
End Property

Private WithEvents _myTimer As Timer
Private ReadOnly Property MyTimer As Timer
Get
If _myTimer Is Nothing Then
_myTimer = New Timer() With {.Enabled = False, .Interval = QueryInterval}
End If
Return _myTimer
End Get
End Property

Private WithEvents _myPictureBox As PictureBox
Private ReadOnly Property MyPictureBox As PictureBox
Get
If _myPictureBox Is Nothing Then
_myPictureBox = New PictureBox() With {
.BackColor = Color.Transparent,
.BorderStyle = BorderStyle.None,
.Parent = Me,
.Height = Height - 2,
.Width = Height - 2,
.Top = 1,
.Left = Width - Height,
.Visible = False,
.SizeMode = PictureBoxSizeMode.CenterImage}
_myPictureBox.Image = Image.FromStream(
New IO.MemoryStream(
Convert.FromBase64String(
"R0lGODlhFAA...(long string)...EgQAOw==")))
End If
Return _myPictureBox
End Get
End Property

Friend Property WaitingIcon As Boolean
Get
Return MyPictureBox.Visible
End Get
Set(value As Boolean)
MyPictureBox.Visible = value
End Set
End Property

Public Shadows Property DataSource As DataTable
Get
Return MyBase.DataSource
End Get
Set(value As DataTable)
MyBase.DataSource = value
End Set
End Property

Public Shadows Property SelectedItem As DataRowView
Get
Return MyBase.SelectedItem
End Get
Set(value As DataRowView)
MyBase.SelectedItem = value
End Set
End Property

Private Sub _myTimer_Tick(sender As Object, e As EventArgs) Handles _myTimer.Tick
MyTimer.Stop()
SendKeys.Send("{TAB}")
End Sub

Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
MyTimer.Stop()
If e.KeyValue = 13 Then e.SuppressKeyPress = True
MyBase.OnKeyDown(e)
End Sub

Protected Overrides Sub OnKeyUp(e As KeyEventArgs)
If e.KeyValue = 13 Then e.SuppressKeyPress = True
MyBase.OnKeyUp(e)
If e.KeyValue = 13 Then
SendKeys.Send("{TAB}")
ElseIf hasTextChanged
hasTextChanged = False
MyTimer.Stop()
MyTimer.Start()
End If
End Sub

Protected Overrides Sub OnTextChanged(e As EventArgs)
hasTextChanged = True
MyBase.OnTextChanged(e)
End Sub

Protected WithEvents BGW As New BackgroundWorker
Private work As Action, whenDone As Action
Protected Sub DoAction(ByVal work As Action, whendone As Action)
Me.work = work
Me.whenDone = whendone
BGW.RunWorkerAsync()
End Sub
Private Sub BGW_DoWork(sender As Object, e As DoWorkEventArgs) Handles BGW.DoWork
Invoke(Sub() WaitingIcon = True)
If work IsNot Nothing Then work.Invoke
End Sub
Private Sub BGW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW.RunWorkerCompleted
Invoke(Sub() WaitingIcon = False)
If whenDone IsNot Nothing Then BeginInvoke(whenDone)
End Sub
End Class
These controls are working as expected, but now I would like my DataGridViews to host cells that behaved just like these. I tried to follow an example I found on web and wrote those classes below. However, it never worked. It raises the same exception in DataGridView's standard erro dialog, once when it is loaded and again repeats forever whenever I try to click it: System.FormatException: The property FormattedValueType of a cell cannot be null.
Code:
 Public Class SearchColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New SearchCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(value As DataGridViewCell)
If (value IsNot Nothing) AndAlso
Not value.GetType().IsAssignableFrom(GetType(SearchCell)) _
Then
Throw New InvalidCastException("Must be a SearchCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class

Public Class SearchCell
Inherits DataGridViewCell
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 SearchEditingControl =
CType(DataGridView.EditingControl, SearchEditingControl)

' Use the default row value when Value property is null.
If (Me.Value Is Nothing) Then
ctl.Text = CStr(Me.DefaultNewRowValue).Assert
Else
ctl.Text = CStr(Me.Value).Assert
End If
End Sub
End Class

Class SearchEditingControl
Inherits PessBox
Implements IDataGridViewEditingControl

Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer

Public Property EditingControlDataGridView As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(value As DataGridView)
dataGridViewControl = value
End Set
End Property

Public Property EditingControlFormattedValue As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Text.Assert
End Get
Set(value As Object)
Me.Text = CStr(value).Assert
End Set
End Property

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 Property EditingControlValueChanged As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(value As Boolean)
valueIsChanged = value
End Set
End Property

Public ReadOnly Property EditingPanelCursor As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property

Public ReadOnly Property RepositionEditingControlOnValueChange As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property

Public Sub ApplyCellStyleToEditingControl(dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor
End Sub

Public Sub PrepareEditingControlForEdit(selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub

Public Function EditingControlWantsInputKey(keyData As Keys, dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
Select Case keyData And Keys.KeyCode
Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
Return True
Case Else
Return Not dataGridViewWantsInputKey
End Select
End Function

Public Function GetEditingControlFormattedValue(context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.Text.Assert
End Function

Protected Overrides Sub OnTextChanged(e As EventArgs)
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnTextChanged(e)
End Sub

End Class
Any help you guys could give me, or directions for properly implementing this, will be deeply appreciated. Thanks!
Reply With Quote
Reply

Tags
property, public, return, private, implements, set, boolean, class, setvalue, integer, object, false, true, overrides, protected, _mytimer, valueischanged, action, whendone, function, _mypicturebox, datagridviewcellstyle, inherits, readonly, hastextchanged


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
 
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
Implement Column/Cell/EditingControl derived classes, host custom ComboBox control
 
-->