Implement PickList function in VB

zhaozj2021-02-08  244

Implement PickList function in VB

Foreword

People who use Delphi programming that the database control DBGRID has a PickList function in Delphi, that is, when editing the column, as long as the column is clicked, the column will appear in the combo box, list all content for users, this is very Convenient data entry. As shown in the figure.

Figure 1. Delphi's PickList function display diagram

Regrettably, the controls provided by VB DBGrid32.ocx is looking for, without the above PickList function.

The above PickList function is still implemented in VB.

Idea:

1. Add a combo box on the form, and the number is exactly the same as the number of columns that need to implement the PickList function.

2, in related events, such as mouse or cursor (ie, focus) position in the Picklist column, display the combo box, its location, the size of the unit is completely coincident, by default, the value of the combination frame is the unit of DBGRID. Value; otherwise the combo box is hidden.

3. Select or enter the relevant information by the user in the ComboBox combo box, and put the information of the combo box into the DBGRID unit.

Implementation process:

For the sake of simplicity, assume that only one column of PickList is only one column, the corresponding database file has been added to the relevant control.

1. Add a new project to add Microsoft Data Bound Control (ie, file dbgrid32.ocx); put the control into the form; envision the record file including name, retirement type, etc., retirement type is second column (for The first column of DBGRID1 is implemented in the PickList function; take DBGrid1.RowHeight = 300, which is equal to the width of the combo box, and does not allow the DBGRID1 line size.

2, add the ComboBox combo combination box, name: CBL retire species; Visible: false ;; Add information in list attribute: such as retirement | Food workers in the country | Retirement | Return | Note: | Representing a newline.).

And put the CBL retirement type on DBGRID1 and placed before;

The main source code for implementing the PickList function is as follows:

Option expedition

DIM LASTRECNUM AS INTEGER 'Record the location of the composite box content change

Private Sub CBL Retirement Type Show_Lostfocus ()

'Update record value

DIM CURRECNUM 'Current record number

With data1.recordset

CurRecnum = .AbsolutePosition 1

.MOVEFIRST

.MOVE LASTRECNUM - 1

. Ait

.Fields (1) = frmPick.cbl retire species display

.Update

'Refractive location

.MOVE CURRECNUM - LastRecnum

End with

End Sub

'Pay attention to the following three events

Private sub DBGRID1_ROWCOLCHANGE (Lastrow As Variant, Byval Lastcol as inTeger)

DisplayComboBox

End Sub

Private sub DBGRID1_COLRESIZE (Byval Colindex As Integer, Cancel AS Integer)

DisplayComboBox

End Sub

Private sub DBGRID1_Scroll (Cancel AS Integer)

Cancel = false

'Combination frame disappears

Doevents

FRMPICK.CBL Retirement Type Show .visible = falseEnd Sub

Sub DisplayComboBox ()

The operation that should be made to the combination box when it meets the conditions. The key to the process is the key to the article.

'The location where the current record is

LastRecnum = data1.recordset.absoluteposition 1

'frmPick.caption = data1.recordset.fields (1) .value

WITH FRMPICK.CBL Retirement species display

'When the cursor or mouse is listed in the Retirement Type,

IF dbgrid1.col = 1 THEN

The 'combo box appears and has a focus;

.Visible = TRUE

.Setfocus

IF dbgrid1.columns (1) = "" "

.Text = .list (0)

Else

.Text = dbgrid1.columns (1)

END IF

'CBL retirement species display location

'left:

Dim RecordtitleWidth 'Record Head Width

RecordTITLEWIDTH = 280 'This value does not know how to get it, this department is gradually adjusted;

.Left = recorditlewidth DBGRID1.LEFT GETLEFTCOLPOS

Dim colheadheight 'column

COLHEADHEIGHT = 220 'This value still does not know how to acquire, this department is gradually adjusted;

.Top = dbgrid1.top dbgrid1.row * DBGRID1.ROWHEIGHT COLHEADHEIGHT

.Width = dbgrid1.columns (1) .width

END IF

'When the cursor is not in the PickList column

IF dbgrid1.col <> 1 THEN

.Visible = false

END IF

End with

End Sub

Function GetLeftColpos ()

The left side of the 'DBGRID1 control can be seen that the packet implementation picklist column

DIM I

i = dbgrid1.Leftcol

GetleftColpos = 0

IF i <1 THEN

Do WHILE I <1 'The column 1 of the site 1 Realize the column of PickList

GetleftColpos = GetleftColpos DBGRID1.COLUMNS (i) .width

i = i 1

Loop

END IF

IF i> 1 THEN

Do While i> 1 'The column of the site 1 Realize the column of PickList

GetleftColpos = GetleftColpos - DBGRID1.COLUMNS (i) .width

i = i - 1

Loop

END IF

END FUNCTION

Figure 2 is an example of a PickList function written in VB code.

Figure 2, implement the column of dbgrid with VB code PickList example

The above code is passed in VB6.0, Windows98 Chinese version.

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

New Post(0)