Edit input with Microsoft Grid control

Source: Internet
Author: User
Tags chr integer

Visual Basic provides a number of standard and custom controls, each of which can provide a special set of user interfaces and programming capabilities. By making full use of the characteristics and methods of each control, you can make programming work easier and simpler.

Microsoft Grid Control MSFlexGrid is a custom control. With Microsoft Grid controls, you can display text, numbers, and pictures in a row or column order, just like a spreadsheet. The height, width, and other features of the grid can be adjusted, and the rows and columns of the grid can be manipulated individually or in groups. The MSFlexGrid control can classify, merge, and format the contents of the included cells, and can be bound to the database control. The MSFlexGrid control has more than 50 properties, more powerful and more flexible than other grid controls.

However, the MSFlexGrid control, like other grid grid controls, cannot edit the contents of a grid cell, which can be a flaw when it requires modifications to its contents. Although the DBGrid control can be programmed to complete the editing function, its functionality is not as powerful as MSFlexGrid. When the input workload is large, if the use of MSFlexGrid to enter, instead of text or other controls, so as to avoid the interface of repeated refresh and continuous operation of the button, input speed and efficiency will be greatly improved.

Before you use MSFlexGrid in your application, you should add the Msflxgrd.ocx file to your project.

Implementation of the scheme for editing grid units

In order to require the implementation to input the grid unit, the following code can be added to the MSFlexGrid KeyPress event, but it is difficult to modify the contents of the edited input.

  Sub MSFLexGrid1_KeyPress(KeyAscii As Integer)
   MSFlexGrid1.Text=MSFlexGrid1.Text & Chr$(KeyAscii)
   End Sub

Another method is the combination of text box and grid, the user edits the contents of the text box, and then moves to the grid unit after the operation. In this process, you should implement the following features:

(1) text box can be accurately positioned, and to edit the grid unit coincide (2) Usually text box does not show, display (3) When an edit operation is made, it is automatically judged and a new line is added after entering a line (4) press ENTER to confirm and automatically to the next column (5) Double-click the grid cell to display the text box to edit (6) The text box disappears, Move the contents of the text box to the grid cell.

The concrete implementation of editing grid unit

Create a new project, add controls Text1 and MSFlexGrid1 on the Form1, and set their properties based on table 1.

Table 1 objects and properties for form Form1

Object Property Set up
Text1 Visible False
Text “”
MSFlexGrid1 Rows 2
Cols Set yourself according to the number of fields
Fixedcols 0
Fixedrows 1

First, when the text box is displayed, it can coincide with the grid unit accurately. Showtextbox subroutine to implement this functionality. Consider the top and left values of the grid itself, the height and width of the grid cells, and the width of the border that separates the grid cells.

  Sub ShowTextBox()
     With MSFlexGrid1
     '隐藏文本框,设置高度和宽度
     Text1.Visible = False
     Text1.Height = .RowHeight(.Row) - (Screen.TwipsPerPixelY) * 2
     Text1.Width = .ColWidth(.Col)
     ' 计算文本框左坐标
     Text1.Left = .CellLeft + .Left
     Text1.Top = .CellTop + .Top
     Text1.Visible = True
     Text1.SetFocus
   End With
   End Sub

When a button triggers a grid cell, the contents of the cell are saved to the text box, and the text box is displayed for editing.

  Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
   Dim char As String
   If KeyAscii = 13 Then
   Text1.Text = MSFlexGrid1.Text
     Text1.SelStart = Len(Text1.Text)
     Else
       char = Chr$(KeyAscii)
       Text1.Text = char
       Text1.SelStart = 1
     End If
     ShowTextBox
     KeyAscii = 0
   End Sub

When the focus leaves a grid cell, first save the contents of the text box to the grid cell, and then detect whether the left cell is in the first column of the largest row (you can set a few columns yourself), if it is automatically added one line.

  Private Sub MSFlexGrid1_LeaveCell()
     MSFlexGrid1.Text = Text1.Text
   If MSFlexGrid1.Col = 0 And MSFlexGrid1.Row <> 0 And _
   MSFlexGrid1.Row = MSFlexGrid1.Rows - 1 And MSFlexGrid1.Text <> "" Then
     MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
   End If
   End Sub

To respond to mouse actions, add the following code.

  Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, _
                   x As Single, y As Single)
   Text1.Visible = False
   End Sub

When the grid unit changes, take the contents of the grid cell to the text box, waiting for editing, so that the contents of the text box is up-to-date.

  Private Sub MSFlexGrid1_RowColChange()
   Text1.Text = MSFlexGrid1.Text
   End Sub

Double-click the grid cell to edit the contents of the grid cell.

  Private Sub MSFlexGrid1_DblClick()
   If MSFlexGrid1.Row > 0 And MSFlexGrid1.Col = 0 Then MSFlexGrid1_KeyPress 13
   End Sub

The text box plays the role of the input edit box, simulates the grid unit, enters the contents of the text box, passes the processing to the grid, and presses the return key to the next column automatically when the input is finished, and if the last column, jumps to the first column of the next line to wait for input.

  Private Sub Text1_KeyPress(KeyAscii As Integer)
     If KeyAscii = 13 Then
       MSFlexGrid1.Text = Text1.Text
       Text1.Visible = False
       MSFlexGrid1.SetFocus
       If MSFlexGrid1.Col < (MSFlexGrid1.Cols - 1) Then
       MSFlexGrid1.Col = MSFlexGrid1.Col + 1
       ElseIf MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then
         MSFlexGrid1.Row = MSFlexGrid1.Row + 1
         MSFlexGrid1.Col = 0
       End If
       KeyAscii = 0
     End If
   End Sub

The above program is run by VB6.0 (Chinese Enterprise Edition), which realizes the input edit of MSFlexGrid grid unit. Of course, you can add some code to complete the response to some keystrokes, such as pressing the tab automatically to the next column, the ESE key to cancel the input and so on, this is easy to do, no longer described here. If you use the VB6.0 (Chinese Enterprise Edition), you can also use the MSHFlexGrid control to complete the editing input function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.