PDA

View Full Version : How to Host a Button in DataGridView to get a 3D point in AutoCAD



JAquin
2009-08-31, 05:25 PM
I'd like to use the DataGridView (In VB.net) to store 3D coordinates. What I would want is when the user clicks a cell, a button appears at the far right of the cell. Then this button could be clicked to prompt to get a 3D point in AutoCAD. (fairly similar to the AutoCAD property window when you click a cell and the "Calculator" or "GetPoint" button appears a the far right of the cell. I tryied to code a sample DataGrid column with a button, but the button takes the entire cell when you click the cell. Heres what I coded (derived from a MSDN sample):

Imports System
Imports System.Windows.Forms

Public Class ButtonColumn
Inherits DataGridViewColumn

Public Sub New()
MyBase.New(New ButtonCell())
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 (value IsNot Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(ButtonCell)) _
Then
Throw New InvalidCastException("Must be a ButtonCell")
End If
MyBase.CellTemplate = value

End Set
End Property

Private Sub InitializeComponent()
'
'CalendarColumn
'
Me.Resizable = System.Windows.Forms.DataGridViewTriState.[True]

End Sub
End Class

Public Class ButtonCell
Inherits DataGridViewTextBoxCell

Public Sub New()
' Use the short date format.
Me.Style.Format = "d"
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 ctl2 As ButtonEditingControl = _
CType(DataGridView.EditingControl, ButtonEditingControl)
ctl2.Text = "Test"

End Sub

Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing contol that CalendarCell uses.
Return GetType(ButtonEditingControl)
End Get
End Property

Public Overrides ReadOnly Property ValueType() As Type
Get
' Return the type of the value that CalendarCell contains.
Return GetType(DateTime)
End Get
End Property

Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
' Use the current date and time as the default value.
Return DateTime.Now
End Get
End Property

End Class

Class ButtonEditingControl
Inherits Button
Implements IDataGridViewEditingControl

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

Public Sub New()
Me.Text = "Test"
Me.Anchor = AnchorStyles.Right
End Sub

Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue

Get
Return Me.Text
End Get

Set(ByVal value As Object)
If TypeOf value Is String Then
Me.Text = "Test"
End If
End Set

End Property

Public Function GetEditingControlFormattedValue(ByVal context _
As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue

Return Me.Text = "Test"

End Function

Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
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

Return True

Case Else
Return Not dataGridViewWantsInputKey
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
End Class
Public Class Form1
Inherits Form

Private dataGridView1 As New DataGridView()

<STAThreadAttribute()> _
Public Shared Sub Main()
Application.Run(New Form1())
End Sub

Public Sub New()
Me.dataGridView1.Dock = DockStyle.Fill
Me.Controls.Add(Me.dataGridView1)
Me.Text = "DataGridView calendar column demo"
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.Load
Me.dataGridView1.Columns.Add(New ButtonColumn)
Me.dataGridView1.RowCount = 5
Dim row As DataGridViewRow
For Each row In Me.dataGridView1.Rows
row.Cells(0).Value = "Test"
Next row
End Sub
End Class
If someone could show me how to make the button appear the right way, this could help me alot.
If it's not too much asked, I'd like to know how I could code that button to prompt for the 3D point and return the value in the cell.

Thanks in advance.