Henry Instruction: Winform DataGrid Structure Analysis (3) Class Code

zhaozj2021-02-16  50

------------------ X-class code X ---------------------

Option Strict on

Option Explicit on

Imports system.collections

Imports system.componentmodel

Imports system.drawing

Imports System.Windows.Forms

Imports system.data

Public Class DataGridComboBox

'Similarly to the DataGridTextBox class, define a ComboBox used in the drop-down box class.

Inherits ComboBox

Public Sub New ()

Mybase.new ()

End Sub

Public isineditornaVigateMode as boolean = true

END CLASS

Public Class DataGridComboBOXCOLUMN

Inherits DataGridColumnStyle

'Related variables related to user interface

Private xmargin as integer = 2

Private ymargin as integer = 1

Private Combo As DataGridComboBox

Private _displaymember as string

Private _valuemember as string

'Variables used to track editing state changes

Private OldVal as string = string.empty

Private INEDIT AS BOOLEAN = FALSE

'Constructor - the displaymember of the instance, the valueMEMber value is the value of the Integer type from the parent class.

Public Sub New (Byref Datasource As DataTable, _

Byval Displaymember as integer, _

BYVAL VALUEMEMBER AS INTEGER)

Combo = new dataGridcombox ()

_Displaymember = datasource.columns.Item (index: = displaymember) .tostring

_Valuemember = DataSource.columns.Item (index: = valuemember) .tostring

With Combo

.Visible = false

.Datasource = DataSource

.Displaymember = _displaymember

.Valuemember = _Valuemember

End with

End Sub

'Constructor - DisplayMember, valuemember is the value of string type

Public Sub New (Byref Datasource As DataTable, _

Byval Displaymember As String, _

Byval valuemember as string)

Combo = new dataGridcombox ()

With Combo

.Visible = false

.Datasource = DataSource

.Displaymember = Displaymember

.Valuemember = valuemember

End with

End Sub

'------------------------------------- -----

Method in inheriting from the DataGridColumnStyle class' --------------------------------------- -------------

'Change of the focus leaves the change after ComboBox

Protected Overloads Overrides Sub Abort (BYVAL ROWNUM AS INTEGER)

Debug.writeline ("Abort ()")

Rollback ()

HidecomboBOX ()

Endedit ()

End Sub

'Accept changes

Protected Overloads Overrides Function Commit (Byval DataSource As CurrencyManager, _

BYVAL ROWNUM AS INTEGER AS BOOLEAN

HidecomboBOX ()

IF not inendit

Return True

END IF

Try

DIM Value as object = como.selectedValue

If NullText.equals (Value) THEN

Value = convert.dbnull

END IF

SetColumnValueatrow (Datasource, Rownum, Value)

Catch e as exception

Rollback ()

Return False

END TRY

Endedit ()

Return True

END FUNCTION

'Remove the focus

Protected overloads overrides sub concedefocus ()

Combo.visible = false

End Sub

'Edit cell

Protected Overloads Overrides Sub Edit (Byval Source As CurrencyManager, _

Byval rownum as integer, _

Byval Bounds as Rectangle, _

BYVAL [Readonly] as boolean, _

Byval InstantText As String, _

BYVAL CELLISVISible as boolean)

Combo.Text = String.empty

Dim OriginalBounds as Rectangle = Bounds

Oldval = Combo.Text

IF Cellisvisible Then

Bounds.offset (xmargin, ymargin)

Bounds.width - = xmargin * 2

Bounds.height - = ymargin

Combo.Bounds = Bounds

Combo.visible = true

Else

Combo.Bounds = OriginalBounds

Combo.visible = false

END IF

Combo.selectedValue = GetText (GetColumnValueatrow (Source, Rownum))

IF not instanttext is nothing then

Combo.selectedValue = InstantText

END IF

Combo.righttoleft = me.datagridtablestyle.DataGrid.rightToleft

Combo.focus ()

IF instanttext is nothing thein

Combo.selectall ()

Else

DIM [end] as integer = como.text.lengthcombo.Select ([end], 0)

END IF

IF combo.visible kil

DataGridtablestyle.DataGrid.invalidate (OriginalBounds)

END IF

INEDIT = TRUE

End Sub

Protected overloads overrides function getminimumheight () AS integer

'Set the minimum height of ComboBox

Return Combo.PreferredHeight Ymargin

END FUNCTION

Protected Overloads Overrides Function GetPreferredheight (Byval g as graphics, _

BYVAL VALUE As Object) AS INTEGER

Debug.writeline ("getPreferredHeight ()")

DIM newlineindex as integer = 0

DIM newlines as integer = 0

Dim valueString as string = me.gettext (value)

DO

While NewLineIndex <> -1

NewLineIndex = Valuestring.indexof ("R / N", NewLineIndex 1)

NewLines = 1

End while

Loop

Return Fontheight * NewLines Ymargin

END FUNCTION

Protected Overloads Overrides Function GetPreferredSize (Byvalg as graphics, _

BYVAL VALUE As Object) As size

DIM EXTENTS as size = size.ceiling (g.measureString (gettext (value), _

Me.DataGridtableStyle.DataGrid.font)))

Extents.width = xmargin * 2 DataGridTableGridLineWidth

Extents.height = ymargin

Return Extents

END FUNCTION

Protected Overloads Overrides Sub Paint (Byvalg As Graphics, _

Byval Bounds as Rectangle, _

Byval Source as currencymanager, _

BYVAL ROWNUM AS INTEGER

Paint (G, Bounds, Source, Rownum, False)

End Sub

Protected Overloads Overrides Sub Paint (Byvalg As Graphics, _

Byval Bounds as Rectangle, _

Byval Source as currencymanager, _

Byval rownum as integer, _

Byval aligntoright as boolean)

DIM text as string = getText (getColumnValueatrow (Source, Rownum))

PaintText (G, Bounds, Text, Aligntoright)

End Sub

Protected Overloads Sub Paint (Byvalg As Graphics, _Byval Bounds as Rectangle, _

Byval Source as currencymanager, _

Byval rownum as integer, _

Byval backbrush as brush, _

Byval forebrush as brush, _

Byval aligntoright as boolean)

DIM text as string = getText (getColumnValueatrow (Source, Rownum))

PaintText (G, Bounds, Text, Backbrush, Forebrush, Aligntoright)

End Sub

Protected Overloads overrides sub setDataGridincolumn (Byval Value As DataGrid)

Mybase.setdataGridincolumn (Value)

IF not (combo.parent is value).

If not (como.parent is nothing ") THEN

Combo.Parent.Controls.Remove (Combo)

END IF

END IF

IF not (value is nothing) Then value.controls.add (Combo)

End Sub

Protected overloads overrides sub Updateui (Byval Source As CurrencyManager, _

Val rownum as integer, byval instanttext as string

Combo.Text = GetText (GetColumnValueatrow (Source, Rownum))

IF not (InstantText is nothing "

Combo.text = instanttext

END IF

End Sub

--- Und finished, see (3) Use the code one ---------

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

转载请注明原文地址:https://www.9cbs.com/read-25552.html

New Post(0)