home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / msflex2a / grid.frm (.txt) < prev   
Encoding:
Visual Basic Form  |  1999-09-24  |  15.2 KB  |  476 lines

  1. VERSION 5.00
  2. Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
  3. Begin VB.Form BackGrnd 
  4.    AutoRedraw      =   -1  'True
  5.    BorderStyle     =   1  'Fixed Single
  6.    Caption         =   "Test Grid"
  7.    ClientHeight    =   3360
  8.    ClientLeft      =   45
  9.    ClientTop       =   360
  10.    ClientWidth     =   4695
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    Moveable        =   0   'False
  15.    ScaleHeight     =   3360
  16.    ScaleWidth      =   4695
  17.    StartUpPosition =   2  'CenterScreen
  18.    Begin VB.CheckBox AutoReturn 
  19.       Caption         =   "Auto Return to Col. 1"
  20.       Height          =   255
  21.       Left            =   2640
  22.       TabIndex        =   4
  23.       TabStop         =   0   'False
  24.       Top             =   120
  25.       Width           =   1815
  26.    End
  27.    Begin VB.CommandButton Stop 
  28.       Caption         =   "STOP"
  29.       Height          =   375
  30.       Left            =   1920
  31.       TabIndex        =   2
  32.       Top             =   2880
  33.       Width           =   975
  34.    End
  35.    Begin VB.TextBox T1 
  36.       Appearance      =   0  'Flat
  37.       BackColor       =   &H00C0FFFF&
  38.       BorderStyle     =   0  'None
  39.       Height          =   285
  40.       Left            =   2160
  41.       MaxLength       =   20
  42.       TabIndex        =   1
  43.       Top             =   1200
  44.       Visible         =   0   'False
  45.       Width           =   975
  46.    End
  47.    Begin MSFlexGridLib.MSFlexGrid FG1 
  48.       Height          =   2415
  49.       Left            =   240
  50.       TabIndex        =   0
  51.       Top             =   360
  52.       Width           =   4215
  53.       _ExtentX        =   7435
  54.       _ExtentY        =   4260
  55.       _Version        =   393216
  56.       Rows            =   30
  57.       Cols            =   10
  58.       AllowBigSelection=   0   'False
  59.       ScrollTrack     =   -1  'True
  60.       FillStyle       =   1
  61.    End
  62.    Begin VB.Label CellIndicator 
  63.       Appearance      =   0  'Flat
  64.       AutoSize        =   -1  'True
  65.       BackColor       =   &H80000005&
  66.       BorderStyle     =   1  'Fixed Single
  67.       ForeColor       =   &H80000008&
  68.       Height          =   255
  69.       Left            =   240
  70.       TabIndex        =   3
  71.       Top             =   120
  72.       UseMnemonic     =   0   'False
  73.       Width           =   105
  74.    End
  75.    Begin VB.Menu MnuFGridRows 
  76.       Caption         =   "Row Popup"
  77.       Visible         =   0   'False
  78.       Begin VB.Menu MnuFGridAddRow 
  79.          Caption         =   "Add a Row"
  80.       End
  81.       Begin VB.Menu MnuFGridInsRow 
  82.          Caption         =   "Insert a Row"
  83.       End
  84.       Begin VB.Menu MnuFGridDelRow 
  85.          Caption         =   "Delete a Row"
  86.       End
  87.       Begin VB.Menu MnuFGridExtrRow 
  88.          Caption         =   "Extract a Row"
  89.       End
  90.    End
  91.    Begin VB.Menu MnuFGridCols 
  92.       Caption         =   "Col Popup"
  93.       Visible         =   0   'False
  94.       Begin VB.Menu MnuFGridAddCol 
  95.          Caption         =   "Add a Col"
  96.       End
  97.       Begin VB.Menu MnuFGridInsCol 
  98.          Caption         =   "Insert a Col"
  99.       End
  100.       Begin VB.Menu MnuFGridDelCol 
  101.          Caption         =   "Delete a Col"
  102.       End
  103.       Begin VB.Menu MnuFGridExtrCol 
  104.          Caption         =   "Extract a Col"
  105.       End
  106.    End
  107. Attribute VB_Name = "BackGrnd"
  108. Attribute VB_GlobalNameSpace = False
  109. Attribute VB_Creatable = False
  110. Attribute VB_PredeclaredId = True
  111. Attribute VB_Exposed = False
  112. 'MSFlexgrid Edit : 9-12-99
  113. 'this is an effective and completely natural looking way
  114. 'to edit data in the MSFlexgrid object. I've chosen to keep
  115. 'all columns the same width that is why I've used .TextMatrix
  116. 'in Form_Load() below rather than .Format$.
  117. 'If you don't care that numbers and chars wind up aligned
  118. 'differently in the columns then remove .ColAlignment(-1)=1
  119. 'in the Form_Load() sub.
  120. 'Move the cell around with the cursor keys or click with
  121. 'the mouse on the destination cell.
  122. 'Starting to type will replace the current value in that cell.
  123. 'If you wish to edit the existing text in a cell then press F2.
  124. 'You can enter a cell, drag the mouse and then start typing and
  125. 'VOILA you fill in all the hi-lited cells. The same thing can
  126. 'be achieved by pressing the SHIFT & CURSOR keys. (This is also
  127. 'an effective way of erasing a whole block of data.)
  128. '<ENTER> key advances to the next cell.
  129. 'If you can improve the code...COOL, let me know.
  130. '9-13-99
  131. 'Went through the code and eliminated some dead code from
  132. 'previous versions as well as some typos.
  133. 'Navigtion keys are the usual <Home>, <End>, <Pg Up / Dn>
  134. 'along with their Ctrl alternates
  135. '9-14-99
  136. 'added a Cell indicator - may come in handy in BIG Grids
  137. 'Also added a choice button to autom. return the focus
  138. 'to the first column or just go down 1 row
  139. 'similar thing can be accomplished like so:
  140. '( in Incr_Cell() )
  141. '   if Fg1.ColIsVisible(1) then
  142. '       FG1.Col=1
  143. '   end if
  144. '9-23-99
  145. 'added In-Cell cursor control to move to adjacent cells for
  146. 'up/dn anytime, right/left when cursor pos is either right
  147. 'or leftmost, in cell, respectively. When in the last cell
  148. '(and editing) Right cursor will advance to next row col 1.
  149. 'When in the first cell (and editing) Left cursor will jump
  150. 'to last cell one row up.
  151. 'Typing into a cell with existing data OVERWRITES unless you
  152. 'press F2, but if you forget you can now press <ESC> and
  153. 'restore the previous value (before exiting the cell).
  154. 'also added Popupmenus for Adding, Deleting, Inserting and
  155. 'Extracting either Rows or Cols.
  156. 'Just put mouse in Col 0 or Row 0 and click Right Mouse Button.
  157. 'Add - means add Row at bottom or Col at end
  158. 'Delete - means del LAST Row or LAST Col
  159. 'Insert - means INSERT a Row / Col at present cursor pos.
  160. 'Extract - means EXTRACT a Row /Col at present cursor pos.
  161. 'If you try to DELETE / EXTRACT a Row / Col that has data
  162. 'in it you will be prompted if you wish to proceed.
  163. 'The Popupmenus were created the usual way and then had their
  164. 'Titles set to : Visible = False
  165. '9-24-99 minor fix to T1_KeyDown.... Case 27, 37-40
  166. 'otherwise it adds a char to the text in Case Else!
  167. 'Now posted as as .vbp file
  168. 'Peter Raddatz - lupo@unix.infoserve.net
  169. Private Sub Form_Load()
  170.     Dim y%
  171.     With FG1
  172.     .ColAlignment(-1) = 1
  173.     For y% = 1 To .Cols - 1
  174.         .TextMatrix(0, y%) = "Col " + Str(y%)
  175.     Next
  176.     For y% = 1 To FG1.Rows - 1
  177.         .TextMatrix(y%, 0) = "Row " + Str(y%)
  178.     Next
  179.     .Row = 1
  180.     .Col = 1
  181.     .CellBackColor = &HC0FFFF   'lt. yellow
  182.     BackGrnd.CellIndicator = " " + .TextMatrix(.Row, 0) + " : " + .TextMatrix(0, .Col) + " "
  183.     End With
  184. End Sub
  185. Private Sub AutoReturn_Click()
  186.     FG1.SetFocus
  187. End Sub
  188. Private Sub FG1_EnterCell()
  189.     BackGrnd.CellIndicator = " " + FG1.TextMatrix(FG1.Row, 0) + " : " + FG1.TextMatrix(0, FG1.Col) + " "
  190.     T1.Visible = False
  191.     FG1.Tag = FG1
  192.     FG1.CellBackColor = &HC0FFFF    'lt. yellow
  193.     FG1.SetFocus
  194. End Sub
  195. Private Sub FG1_LeaveCell()
  196.     FG1.CellBackColor = &H80000005  'white
  197. End Sub
  198. Private Sub FG1_KeyDown(KeyCode As Integer, Shift As Integer)
  199.     Select Case KeyCode
  200.         Case 113        'F2
  201.             Set_TextBox
  202.     End Select
  203. End Sub
  204. Private Sub FG1_KeyPress(KeyAscii As Integer)
  205.     Select Case KeyAscii
  206.         Case 13         'ENTER key
  207.             KeyCode = 0
  208.             INCR_CELL
  209.         Case 8      'BkSpc
  210.             FG1 = Left$(FG1, Len(FG1) - 1)
  211.             Set_TextBox
  212.         Case 27     'Esc - ignore
  213.         Case Else
  214.             FG1 = Chr$(KeyAscii)
  215.             T1 = Chr$(KeyAscii)
  216.             Set_TextBox
  217.     End Select
  218. End Sub
  219. Private Sub T1_KeyDown(KeyCode As Integer, Shift As Integer)
  220. Select Case KeyCode
  221.     Case 27     'ESC - OOPS, restore old text
  222.         T1 = FG1.Tag
  223.         T1.SelStart = Len(T1)
  224.     Case 37     'Left Arrow
  225.         If T1.SelStart = 0 And FG1.Col > 1 Then
  226.             FG1.Col = FG1.Col - 1
  227.         Else
  228.             If T1.SelStart = 0 And FG1.Row > 1 Then
  229.                 FG1.Row = FG1.Row - 1
  230.                 FG1.Col = FG1.Cols - 1
  231.             End If
  232.         End If
  233.     Case 38     'Up Arrow
  234.         If FG1.Row > 1 Then
  235.             FG1.Row = FG1.Row - 1
  236.         End If
  237.     Case 39     'Rt Arrow
  238.         If T1.SelStart = Len(T1) And FG1.Col < FG1.Cols - 1 Then
  239.             FG1.Col = FG1.Col + 1
  240.         Else
  241.             If T1.SelStart = Len(T1) And FG1.Row < FG1.Rows - 1 Then
  242.                 FG1.Row = FG1.Row + 1
  243.                 FG1.Col = 1
  244.             End If
  245.         End If
  246.     Case 40     'Dn Arrow
  247.         If FG1.Row < FG1.Rows - 1 Then
  248.             FG1.Row = FG1.Row + 1
  249.         End If
  250. End Select
  251.     IsCellVisible
  252. End Sub
  253. Private Sub T1_KeyPress(KeyAscii As Integer)
  254.     Dim pos%, l$, R$
  255.     Select Case KeyAscii
  256.         Case 13
  257.             KeyAscii = 0
  258.             FG1 = T1
  259.             T1.Visible = False
  260.             INCR_CELL
  261.             FG1.SetFocus
  262.         Case 8                      'BkSpc - split string @ cursor
  263.             pos% = T1.SelStart - 1 'where is the cursor?
  264.             If pos% >= 0 Then
  265.                 l$ = Left$(FG1, pos%)       'left of cursor
  266.                 R$ = Right$(FG1, Len(FG1) - pos% - 1) 'right of cursor
  267.                 FG1.Text = l$ + R$          'depleted string into fg1
  268.             End If
  269.         Case 27, 37 To 40
  270.             FG1 = T1        'or it's going to look funny
  271.         Case Else
  272.             FG1 = T1 + Chr(KeyAscii)
  273.     End Select
  274. End Sub
  275. Private Sub FG1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  276. Dim Row%, Col%
  277. T1.Visible = False
  278. Row% = FG1.MouseRow
  279. Col% = FG1.MouseCol
  280. If Button = 2 And (Col% = 0 Or Row% = 0) Then
  281.     FG1.Col = IIf(Col% = 0, 1, Col%)    'rows?
  282.     FG1.Row = IIf(Row% = 0, 1, Row%)    'or cols?
  283.     If Col% Then
  284.         PopupMenu MnuFGridCols
  285.     Else
  286.         PopupMenu MnuFGridRows
  287.     End If
  288. End If
  289. End Sub
  290. Private Sub MnuFGridAddCol_Click()
  291. With FG1
  292.     .Cols = .Cols + 1
  293.     .Col = .Cols - 1
  294.     .TextMatrix(0, .Col) = "Col " + Str(.Col)
  295. End With
  296. IsCellVisible
  297. End Sub
  298. Private Sub MnuFGridAddRow_Click()
  299. With FG1
  300.     .Rows = .Rows + 1
  301.     .Row = .Rows - 1
  302.     .TextMatrix(.Row, 0) = "Row " + Str(.Row)
  303. End With
  304. IsCellVisible
  305. End Sub
  306. Private Sub MnuFGridDelCol_Click()
  307. Dim R%, C%, Col%
  308. If FG1.Cols > 2 Then        'make sure we don't del col 1
  309.     Col% = FG1.Cols - 1
  310.     For R% = 1 To FG1.Rows - 1
  311.         If FG1.TextMatrix(R%, Col%) > "" Then   'data?
  312.             C% = 1
  313.             Exit For
  314.         End If
  315.     Next R%
  316.     If C% Then
  317.         R% = MsgBox("There is data in Col" + Str$(Col%) + " ! Delete anyway?", vbYesNo, "Delete Column!")
  318.     End If
  319.     If C% = 0 Or R% = 6 Then    'no exist data or YES
  320.         If FG1.Col = FG1.Cols - 1 Then  'last col?
  321.             FG1.Col = FG1.Col - 1       'move active cell
  322.         End If
  323.         FG1.Cols = FG1.Cols - 1         'del lat col
  324.     End If
  325. End If
  326. End Sub
  327. Private Sub MnuFGridDelRow_Click()
  328. Dim R%, C%, Row%
  329. If FG1.Rows > 2 Then        'make sure we don't del row 1
  330.     Row% = FG1.Rows - 1
  331.     For R% = 1 To FG1.Cols - 1
  332.         If FG1.TextMatrix(Row%, R%) > "" Then   'data?
  333.             C% = 1
  334.             Exit For
  335.         End If
  336.     Next R%
  337.     If C% Then
  338.         R% = MsgBox("There is data in Row" + Str$(Row%) + " ! Delete anyway?", vbYesNo, "Delete Row!")
  339.     End If
  340.     If C% = 0 Or R% = 6 Then        'no exist. data or YES
  341.         If FG1.Row = FG1.Rows - 1 Then  'last row?
  342.             FG1.Row = FG1.Row - 1       'move active cell
  343.         End If
  344.         FG1.Rows = FG1.Rows - 1     'del last row
  345.     End If
  346. End If
  347. End Sub
  348. Private Sub MnuFGridInsRow_Click()
  349. Dim R%, Row%, Col%
  350. With FG1
  351. R% = .Row
  352. .Rows = .Rows + 1               'add a row
  353. .TextMatrix(.Rows - 1, 0) = "Row " + Str$(.Rows - 1)    'new row title
  354. For Row% = .Rows - 1 To R% + 1 Step -1 'move data dn 1 row
  355.     For Col% = 1 To .Cols - 1
  356.         .TextMatrix(Row%, Col%) = .TextMatrix(Row% - 1, Col%)
  357.     Next Col%
  358. Next Row%
  359. For Col% = 1 To .Cols - 1       ' clear all cells in this row
  360.     .TextMatrix(R%, Col%) = ""
  361. Next Col%
  362. End With
  363. T1.Visible = False
  364. End Sub
  365. Private Sub MnuFGridInsCol_Click()
  366. Dim C%, Row%, Col%
  367. With FG1
  368. C% = .Col
  369. .Cols = .Cols + 1               'add a col
  370. .ColAlignment(-1) = 1           'set col alignment
  371. .TextMatrix(0, .Cols - 1) = "Col " + Str(.Cols - 1) 'new col title
  372. For Row% = 1 To .Rows - 1       'move exist. data over
  373.     For Col% = .Cols - 1 To C% + 1 Step -1
  374.         .TextMatrix(Row%, Col%) = .TextMatrix(Row%, Col% - 1)
  375.     Next Col%
  376. Next Row%
  377. For Row% = 1 To .Rows - 1       'clear all cells in this col
  378.     .TextMatrix(Row%, C%) = ""
  379. Next Row%
  380. End With
  381. T1.Visible = False
  382. End Sub
  383. Private Sub MnuFGridExtrRow_Click()
  384. Dim Row%, R%, C%
  385. With FG1
  386. If .Rows > 2 Then        'make sure we don't del row 1
  387.     Row% = .Row
  388.     For R% = 1 To .Cols - 1
  389.         If .TextMatrix(Row%, R%) > "" Then   'data?
  390.             C% = 1
  391.             Exit For
  392.         End If
  393.     Next R%
  394.     If C% Then
  395.         R% = MsgBox("There is data in Row" + Str$(Row%) + " ! Delete anyway?", vbYesNo, "Delete Row!")
  396.     End If
  397.     If C% = 0 Or R% = 6 Then        'no exist. data or YES
  398.         For R% = .Row To .Rows - 2      'move exist data up 1 row
  399.             For C% = 1 To FG1.Cols - 1
  400.                 .TextMatrix(R%, C%) = .TextMatrix(R% + 1, C%)
  401.             Next C%
  402.         Next R%
  403.         If Row% = .Rows - 1 Then    'set new cursor row
  404.             .Row = .Rows - 2
  405.         End If
  406.         .Rows = .Rows - 1           'delete last row
  407.     End If
  408. End If
  409. End With
  410. End Sub
  411. Private Sub MnuFGridExtrCol_Click()
  412. Dim Col%, R%, C%
  413. With FG1
  414. If .Cols > 2 Then        'make sure we don't del col 1
  415.     Col% = .Col
  416.     For R% = 1 To FG1.Rows - 1
  417.         If .TextMatrix(R%, Col%) > "" Then   'data?
  418.             C% = 1
  419.             Exit For
  420.         End If
  421.     Next R%
  422.     If C% Then
  423.         R% = MsgBox("There is data in Col" + Str$(Col%) + " ! Delete anyway?", vbYesNo, "Delete Column!")
  424.     End If
  425.     If C% = 0 Or R% = 6 Then    'no exist data or YES
  426.         For R% = 1 To .Rows - 1     'move exist. data left 1 col
  427.             For C% = Col% To .Cols - 2
  428.                 .TextMatrix(R%, C%) = .TextMatrix(R%, C% + 1)
  429.             Next C%
  430.         Next R%
  431.         If Col% = .Cols - 1 Then
  432.             .Col = .Cols - 2
  433.         End If
  434.     .Cols = .Cols - 1           'delete last col
  435.     End If
  436. End If
  437. End With
  438. End Sub
  439. Private Sub Stop_Click()
  440.     End
  441. End Sub
  442. Private Sub INCR_CELL()     'advance to next cell
  443.     With FG1
  444.     .HighLight = flexHighlightNever
  445.     If .Col < .Cols - 1 Then
  446.         .Col = .Col + 1
  447.     Else
  448.         If .Row < .Rows - 1 Then
  449.             .Row = .Row + 1                 'down 1 row
  450.             If AutoReturn.Value Then        'auto return?
  451.                 .Col = 1                    'first column
  452.             End If
  453.         End If
  454.     End If
  455.     IsCellVisible
  456.     .HighLight = flexHighlightAlways
  457.     End With
  458. End Sub
  459. Private Sub Set_TextBox()   'put textbox over cell
  460.     With T1
  461.     .Top = FG1.Top + FG1.CellTop
  462.     .Left = FG1.Left + FG1.CellLeft
  463.     .Width = FG1.CellWidth
  464.     .Height = FG1.CellHeight
  465.     .Text = FG1
  466.     .Visible = True
  467.     .SelStart = Len(.Text)
  468.     .SetFocus
  469.     End With
  470. End Sub
  471. 'this sub scrolls the cols / rows if they're not visible! (? why)
  472. Private Sub IsCellVisible()
  473.     Dim a As Boolean
  474.     a = FG1.CellTop
  475. End Sub
  476.