[Net.vb] small problem highlights (for beginners)

zhaozj2021-02-16  73

statement:

1. All code is not indicated by himself.

2, all code is commissioned in this machine

3, this post applies to beginners (I also)

4. Welcome everyone's righteous ax, providing ideas or code

5, I am striving to supplement daily (now this is not done)

6. I don't agree, please do not reprint the code written by myself.

7, everyone says do you want to have the sixth?

8. Ha, the statement is written like this, it is not serious, nor

Water like smoke 2004.7.19

[Control class]

TreeView

1. Add a Tooltip to the node (2004.7.19)

Private TMPTREENODE As Treenode

Private MtreeViewTooltip As New Tooltip

Private sub treeview1_mousemove (Byval e as system.windows.forms.mouseeventargs) Handles TreeView1.mousemove

Dim Mnode as Treenode

Mnode = me.treeview1.getnodeat (E.x, E.Y)

'There is no node

IF mnode is nothing then

MtreeViewTooltip.SetTooltip (Me.TreeView1, Nothing)

EXIT SUB

END IF

'More than the border of the current Node

If mnode.bounds.right E.X THEN

MtreeViewTooltip.SetTooltip (Me.TreeView1, Nothing)

EXIT SUB

END IF

'Same Node

IF mnode is tmptreenode one

EXIT SUB

END IF

MtreeViewTooltip.AutomaticDelay = 500

MtreeViewTooltip.SetTooltip (Me.TreeView1, Mnode.Text)

TMPTREENODE = Mnode

End Sub

DataGrid

1, adaptive to each column (2004.7.19)

'Define a field information class

Private class columnInfo

Public [name] AS STRING

Public [datatype] AS STRING

Public maxwidth as integer = 0

Public Sub CompareStringLength (Byval Mstring As String)

Dim MLENGTH AS INTEGER

MLENGTH = system.text.Encoding.default.getbytes (mstring) .length ()

IF MaxWidth

End Sub

Public Function ColumnWidth (Byval MDataGrid AS DataGrid) AS Integer

DIM mgraphics as graphics = MDataGrid.creategraphics

DIM MCOLWIDTH AS SINGLE

McOLWIDTH = MGRAPHICS.MEASURESTRING (New String (CType ("a", char), maxwidth, mdatagrid.font) .width 2

Return CType (McOLWIDTH, INTEGER)

END FUNCTION

END CLASS

Private Sub MakeDataGridAutoExtend (Byval MDataGrid As DataGrid) 'Judging MDATAGRID Data Source Type

'If binding is DataSet or DataViewManager or not bind any data source, exit,

If TypeOf MDataGrid.DataSource IS System.Data.Dataset ORELSE _

TypeOf MDataGrid.DataSource IS System.Data.DataViewManager ORELSE _

MDataGrid.datasource is nothing kilos

'The following considerations two data sources, one is DataView, one is DATATABLE

DIM DT AS DATATABLE

If TypeOf MDataGrid.DataSource IS System.Data.DataView Then

DT = ctype (mdatagrid.datasource, dataview) .table

Else

Dt = ctype (mdatagrid.datasource, dataable)

END IF

'Take all field information

Dim McolumnInfo (DT.Columns.count - 1) As ColumnInfo

DIM I as integer = 0

DIM MCOLUMN AS Datacolumn

For Each Mcolumn in Dt.columns

DIM Minfo As New ColumnInfo

With minfo

.Name = mcolumn.columnname

.DATATYPE = MCOLUMN.DATATYPE.TOSTRING

.CompareStringLength (MCOLUMN.COLUMNAME)

End with

McolumnInfo (i) = minfo

i = 1

NEXT

'Take the maximum length of each field

Dim Mrow as DataRow

For Each Mrow in Dt.Rows

For i = 0 to dt.columns.count - 1

IF not isdbnull (Mrow (i)) THEN

McolumnInfo (i) .CompareStringLength (CType (MROW (i), String)

END IF

NEXT

NEXT

'Construction DataGridTableStyle

DIM TS AS New DataGridtableStyle

Ts.mappingname = dt.tablename 'map here to the table

For i = 0 to dt.columns.count - 1

IF MColumnInfo (i) .DATATYPE.EQUALS ("System.Boolean") THEN

'This is a boolean field

DIM BLNCOL As New DataGridBoolColumn

WITH BLNCOL

. Mappingname = mcolumninfo (i) .name

.Headertext = McolumnInfo (i) .name

.Width = mcolumninfo (i) .Columnwidth (MDataGrid)

.Nulltext = "" "

End with

Ts.GridColumnStyles.Add (blncol)

Else

'Non-boolean field

DIM TXTCOL AS New DataGridTextBoxColumn

WITH TXTCOL

. Mappingname = mcolumnInfo (i) .name 'This map to the field. Headertext = MCOLUMNINFO (i) .name' This can be changed to the name you want to display

.Width = mcolumninfo (i) .Columnwidth (MDataGrid) 'Set to 0, you can't see it.

.NullText = "" 'does not display (NULL)

.Readonly = false 'This can be set to read only

.Format = "" "You can set the display format, and you want to display the day-time seconds.

End with

Ts.GridColumnStyles.Add (txtcol)

END IF

NEXT

MDataGrid.tablestyles.clear ()

MDataGrid.TablesTyles.Add (TS)

'Being other functions, such as binding events, then supplement

End Sub

'transfer

Private sub Button2_click (byvalgend, byval e as system.eventargs) Handles Button2.click

MakedataGridAutoExtend (me.datagrid1)

End Sub

2, position to the designated condition line (2004.7.20)

Public Function Pointto (Byval MDataGrid As DataGrid, Byval MfieldName As String, Byval MfieldValue AS String) AS Integer

'Judging the type of MDataGrid data source

'If binding is DataSet or DataViewManager or not bind any data source, exit,

If TypeOf MDataGrid.DataSource IS System.Data.Dataset ORELSE _

TypeOf MDataGrid.DataSource IS System.Data.DataViewManager ORELSE _

MDataGrid.datasource is nothing kiln

'The following considerations two data sources, one is DataView, one is DATATABLE

DIM DV AS DATAVIEW

If TypeOf MDataGrid.DataSource IS System.Data.DataView Then

DV = ctype (MDataGrid.DataSource, DataView)

Else

DV = ctype (mdatagrid.datasource, dataable) .defaultView

END IF

Dv.Sort = mfieldname

DV.ApplyDefaultsort = TRUE

Dim rowindex as integer = -1

Try

RowIndex = DV.Find (MfieldValue)

If RowIndex <> -1 Ten MDataGrid.currentrowindex = Rowindex

Catch ex as systemexception

MSGBOX (ex.Message)

END TRY

Return RowIndex

END FUNCTION

2, multi-line selection process (2004.7.21)

Private sub Button2_click (byvalgend, byval e as system.eventargs) Handles Button2.click

Dim DM as bindingmanagerbasedm = me.bindingcontext (DS, DS.TABLES (0) .tablename)

DIM I as integer

Listbox2.Items.clear ()

For i = 0 to DM.COUNT - 1

If Me.DataGrid1.isselected (i) THEN

Me.ListBox2.Items.add (DS.Tables (0) .ROWS (i) (1)) 'This i is the selected line

END IF

NEXT

End Sub

3, display Gridcell data under the mouse in TextBox (2004.7.22)

PRIVATE SUB DATAGRID1_MOUSEMOVE (Byval e as system.windows.forms.mouseeventargs) Handles DataGrid1.mousemove

Dim myhit as datagrid.hittestinfo

MyHit = DataGrid1.hittest (E.X, E.Y)

If myhit.row = -1 oralse myhit.column = -1 Then

Me.TextBox1.text = ""

Else

Me.TextBox1.text = Me.DataGrid1.item (MyHit.Row, myhit.column) .tostring

END IF

End Sub

'When Mouse quickly removes DataGrid, the above MyHit = DataGrid1.hittest (E.x, E.Y)

'Is it unable to respond in time, so add this:

Private sub DataGrid1_mouseeleave (Byval e as system.eventargs) Handles DataGrid1.MouseLeave

Me.TextBox1.text = ""

End Sub

[Data Class]

DataSet

1. How to write a function in the DataSet to return the index of the current operation (2004.7.28)

Private function getpos (Byval DT As DataTable) AS INTEGER

Dim Mbind As BindingManagerBase

Mbind = me.bindingContext (DT)

Return mbind.position

END FUNCTION

'transfer

DIM MTABLEPOS AS INTEGER

MTABLEPOS = GetPos (DS.Tables (TABLENAME))

Reader

1. According to a SQL statement and related connections, a series of tuples is queried, and these tuples are saved and returned by a two-dimensional string array (2004.7.28)

Public Function SelectArray (Byval Query As String, Byval Connection As Oledb.oledbconnection) AS Array

DIM MCOLS AS INTEGER

Dim rowlist as new arraylist

DIM MCM AS New Oledb.Oledbcommand (Query, Connection)

If connection.state <> connectionState.open kilnness.Open ()

DIM MRD AS OLEDB.OLEDBDATAREADER = MCM.EXECUTEREADER

Mcols = mrd.fieldcount

While Mrd.Readdim Mrow (McOLS - 1) AS Object

MRD.GetValues ​​(MROW)

Rowlist.add (MROW)

End while

MRD.Close ()

Connection.Close ()

Return Rowlist.toArray

END FUNCTION

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

New Post(0)