10 Mart 2011 Perşembe

Visual Basic Gelismis DataGridView bileseni - Cut, copy, paste, vb fonksiyonlari tanimli

DataGridView bileseninin en buyuk sorunu copy paste fonksiyonlarinin kendiliginden tanimli olmamasi. Bu yeni dataGridView bileseni, yerel menu iceriyor ve dogrudan menu uzerinden bu fonksiyonlara ulasilabiliyor. Ayrica eklenen yeni satira satir numarasi ekliyor.

Modulu projenize dahil ettikten sonra, once normal DataGridView ekleyin daha sonra, designer kodundan DataGridView tanimini newDGV olarak degistirin.

Sorun olusa erdenertorer (at) gmail . com 'a yazin.


' Advenced DataGridView - Cut, Copy, Paste, Insert Row, Delete rows implemented with context menu
' Even adds row numbers automatically!
' After adding the regular DataGridView Simply modify the designer code to point newDGV
' Feel free to use this class, but if you improve that please let me know and send me the new version!
' If you like it, just e-mail me let me know. That would make me happy :)
' Erden Ertorer August 2009, erdenertorer (at) yahoo . com

' Gelismis DataGridView - Cut, Copy, Paste, Insert Row, Delete rows, Select all fonksiyonlari ekledim.
' Hatta otomatik satir numarasi ekliyor.
' Normal DataGridView ekledikten sonra designer kodundaki DataGridView tanimini newDGV ile degistirin.
' Gonlunuzce kullanin ama modifiye edip daha iyisini yazarsaniz beni haberdar edin :)
' Hatta cok sevdiyseniz bir mail atin, mutlu olayim :)
' Erden Ertorer Austos 2009, erdenertorer (at) yahoo . com

Module NewDGVmodule
'Permission switches
Public p_cutAllowed As Boolean
Public p_pasteAllowed As Boolean
Public p_enlargeXallowed As Boolean
Public p_enlargeYallowed As Boolean
Public p_addRowNumbers As Boolean

Public Class NewDGV 'Name of the new DataGridView
Inherits System.Windows.Forms.DataGridView
Friend WithEvents NewDGV As System.Windows.Forms.DataGridView
Private menu As New ContextMenuStrip

Public Sub New() 'Initialise the parameters
menu.Items.Add("Cut")
menu.Items.Add("Copy")
menu.Items.Add("Paste")

menu.Items.Add("selectAll")

menu.Items.Add("Delete row(s)")
menu.Items.Add("Insert a row")

Me.ContextMenuStrip = menu

' Predefined permissions. Can be changed in code.
p_cutAllowed = True
p_pasteAllowed = True
p_enlargeXallowed = True
p_enlargeYallowed = True
p_addRowNumbers = True

'Add event handlers
AddHandler menu.Items(0).Click, AddressOf Cut_Click
AddHandler menu.Items(1).Click, AddressOf Copy_Click
AddHandler menu.Items(2).Click, AddressOf Paste_Click
AddHandler menu.Items(3).Click, AddressOf Select_All_Click
AddHandler menu.Items(4).Click, AddressOf DeleteRow_Click
AddHandler menu.Items(5).Click, AddressOf InsertRow_Click

'For capturing the keys
AddHandler menu.KeyDown, AddressOf Menu_key_down

End Sub
'Menu events
Private Sub Cut_Click()
Me.Cut()
End Sub
Private Sub Copy_Click()
Me.Copy()
End Sub
Private Sub Paste_Click()
Me.Paste()
End Sub
Private Sub Select_All_Click()
Me.Select_All()
End Sub
Public Sub Select_All()
Me.SelectAll()
End Sub
Private Sub DeleteRow_Click()
Me.DeleteRow()
End Sub
Private Sub InsertRow_Click()
Me.InsertRow()
End Sub
'Cut function
Public Sub Cut()
If Me.RowCount > 1 Then 'Check if it is the only one
Clipboard.SetDataObject(Me.GetClipboardContent()) 'Store cells
If p_cutAllowed = False Then Exit Sub
For i = 0 To Me.SelectedCells.Count - 1
Me.SelectedCells.Item(i).Value = "" ' Clear content
Next
End If
End Sub
'Copy
Public Sub Copy()
Clipboard.SetDataObject(Me.GetClipboardContent())
End Sub
'Paste
Public Sub Paste()

'Check if it is allowed
If p_pasteAllowed = False Then Exit Sub

Dim tArr() As String
Dim arT() As String
Dim i, ii As Integer
Dim c, cc, r As Integer

'Parsing the line
tArr = Clipboard.GetText().Split(Environment.NewLine)

r = Me.SelectedCells(0).RowIndex
If Me.SelectedCells(0).RowIndex > Me.SelectedCells(Me.SelectedCells.Count - 1).RowIndex Then r = Me.SelectedCells(Me.SelectedCells.Count - 1).RowIndex

c = Me.SelectedCells(0).ColumnIndex
If Me.SelectedCells(0).ColumnIndex > Me.SelectedCells(Me.SelectedCells.Count - 1).ColumnIndex Then c = Me.SelectedCells(Me.SelectedCells.Count - 1).ColumnIndex

' Check if the selection is bigger than the row size and enlarging is permitted
If (tArr.Length + r) > Me.RowCount And p_enlargeYallowed Then
Me.RowCount = (tArr.Length + r) + 1
ElseIf p_enlargeYallowed = False Then
MsgBox("The item is bigger than the table!", MsgBoxStyle.OkOnly)
End If

' Paste the cells
For i = 0 To tArr.Length - 1
If tArr(i) <> "" Then
arT = tArr(i).Split(vbTab)
cc = c
For ii = 0 To arT.Length - 1
If cc > Me.ColumnCount - 1 Then Exit For
If r > Me.Rows.Count - 1 Then Exit Sub
With Me.Item(cc, r)
.Value = arT(ii).TrimStart
End With
cc = cc + 1
Next
r = r + 1
End If
Next
End Sub

'Adding row number for new rows
Private Sub dataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles Me.RowPostPaint
'Check the permission
If p_addRowNumbers = False Then Exit Sub

Using b As SolidBrush = New SolidBrush(Me.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString(e.RowIndex.ToString(e.RowIndex), Me.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
End Using

End Sub

Private Sub DeleteRow()
Dim i, nRmin, nRmax As Integer

'Scan all selected cells to get the max and min row numbers
nRmin = 10000
nRmax = 0
For i = 0 To Me.SelectedCells.Count - 1
If nRmin > Me.SelectedCells(i).RowIndex Then nRmin = Me.SelectedCells(i).RowIndex
If nRmax < Me.SelectedCells(i).RowIndex Then nRmax = Me.SelectedCells(i).RowIndex
Next

'Delete rows from higher to lower
If Me.RowCount > 1 And Me.CurrentCell.RowIndex < Me.RowCount - 1 Then

For i = nRmax To nRmin Step -1
Me.Rows.RemoveAt(i)
Next

End If
End Sub
'Insert a new row
Private Sub InsertRow()
If Me.RowCount > 1 Then
Me.Rows.Insert(Me.CurrentCell.RowIndex, 1)
End If
End Sub
' Capture the keys and process
Private Sub Menu_key_down(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.C AndAlso e.Control Then Copy()
If e.KeyCode = Keys.X AndAlso e.Control Then Cut()
If e.KeyCode = Keys.V AndAlso e.Control Then Paste()

If e.KeyCode = Keys.A AndAlso e.Control Then Select_All()

If e.KeyCode = Keys.D AndAlso e.Control Then DeleteRow_Click()
If e.KeyCode = Keys.I AndAlso e.Control Then InsertRow()
End Sub
End Class
End Module

Hiç yorum yok:

Yorum Gönder