home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch09 / flexgrid / flexgrid.frm (.txt) next >
Encoding:
Visual Basic Form  |  1996-04-25  |  13.2 KB  |  469 lines

  1. VERSION 5.00
  2. Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
  3. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  4. Begin VB.Form FlexGridControl 
  5.    BorderStyle     =   3  'Fixed Dialog
  6.    Caption         =   "FlexGrid"
  7.    ClientHeight    =   5700
  8.    ClientLeft      =   150
  9.    ClientTop       =   720
  10.    ClientWidth     =   8685
  11.    FillStyle       =   2  'Horizontal Line
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    MinButton       =   0   'False
  15.    ScaleHeight     =   5700
  16.    ScaleWidth      =   8685
  17.    ShowInTaskbar   =   0   'False
  18.    StartUpPosition =   3  'Windows Default
  19.    Begin MSFlexGridLib.MSFlexGrid Grid 
  20.       Height          =   5235
  21.       Left            =   105
  22.       TabIndex        =   2
  23.       Top             =   390
  24.       Width           =   8505
  25.       _ExtentX        =   15002
  26.       _ExtentY        =   9234
  27.       _Version        =   393216
  28.       Rows            =   100
  29.       Cols            =   100
  30.       ScrollTrack     =   -1  'True
  31.       AllowUserResizing=   3
  32.    End
  33.    Begin VB.TextBox Text1 
  34.       Height          =   285
  35.       Left            =   1020
  36.       TabIndex        =   1
  37.       Top             =   30
  38.       Width           =   3720
  39.    End
  40.    Begin MSComDlg.CommonDialog CommonDialog1 
  41.       Left            =   7440
  42.       Top             =   -30
  43.       _ExtentX        =   847
  44.       _ExtentY        =   847
  45.       _Version        =   393216
  46.       FontSize        =   1.17491e-38
  47.    End
  48.    Begin VB.Label Label1 
  49.       BorderStyle     =   1  'Fixed Single
  50.       Height          =   285
  51.       Left            =   90
  52.       TabIndex        =   0
  53.       Top             =   30
  54.       Width           =   4650
  55.    End
  56.    Begin VB.Menu FileMenu 
  57.       Caption         =   "File"
  58.       Begin VB.Menu FileNew 
  59.          Caption         =   "New"
  60.       End
  61.       Begin VB.Menu FileOpen 
  62.          Caption         =   "Open"
  63.       End
  64.       Begin VB.Menu FileSave 
  65.          Caption         =   "Save"
  66.       End
  67.       Begin VB.Menu FileSaveAs 
  68.          Caption         =   "Save As"
  69.       End
  70.       Begin VB.Menu FileExit 
  71.          Caption         =   "Exit"
  72.       End
  73.    End
  74.    Begin VB.Menu EditMenu 
  75.       Caption         =   "Edit"
  76.       Begin VB.Menu EditCopy 
  77.          Caption         =   "Copy"
  78.       End
  79.       Begin VB.Menu EditCut 
  80.          Caption         =   "Cut"
  81.       End
  82.       Begin VB.Menu EditPaste 
  83.          Caption         =   "Paste"
  84.       End
  85.       Begin VB.Menu EditClear 
  86.          Caption         =   "Clear"
  87.       End
  88.       Begin VB.Menu EditSelect 
  89.          Caption         =   "Select All"
  90.       End
  91.    End
  92.    Begin VB.Menu FormatMenu 
  93.       Caption         =   "Format"
  94.       Begin VB.Menu FormatFont 
  95.          Caption         =   "Font"
  96.       End
  97.       Begin VB.Menu FormatCellColor 
  98.          Caption         =   "CellColor"
  99.       End
  100.       Begin VB.Menu Separator2 
  101.          Caption         =   "-"
  102.       End
  103.       Begin VB.Menu FormatInteger 
  104.          Caption         =   "###"
  105.       End
  106.       Begin VB.Menu Format2decimal 
  107.          Caption         =   "###.00"
  108.       End
  109.       Begin VB.Menu FormatComaInteger 
  110.          Caption         =   "#,###.00"
  111.       End
  112.       Begin VB.Menu FormatDollar 
  113.          Caption         =   "$#,###.00"
  114.       End
  115.    End
  116.    Begin VB.Menu MergeMenu 
  117.       Caption         =   "Merge"
  118.       Begin VB.Menu MergeFree 
  119.          Caption         =   "Free Merge"
  120.       End
  121.       Begin VB.Menu MergeRows 
  122.          Caption         =   "Merge Rows"
  123.       End
  124.       Begin VB.Menu MergeCols 
  125.          Caption         =   "Merge Columns"
  126.       End
  127.       Begin VB.Menu MergeBoth 
  128.          Caption         =   "Merge Both"
  129.       End
  130.       Begin VB.Menu MergeNone 
  131.          Caption         =   "Do not Merge"
  132.       End
  133.    End
  134.    Begin VB.Menu SortMenu 
  135.       Caption         =   "Sort"
  136.       Begin VB.Menu SortAsc 
  137.          Caption         =   "Ascending"
  138.          Begin VB.Menu AscNumeric 
  139.             Caption         =   "Numeric"
  140.          End
  141.          Begin VB.Menu AscString 
  142.             Caption         =   "String"
  143.             Begin VB.Menu AscStringSensitive 
  144.                Caption         =   "Case Sensitive"
  145.             End
  146.             Begin VB.Menu AscStringNonsensitive 
  147.                Caption         =   "case insensitive"
  148.             End
  149.          End
  150.          Begin VB.Menu AscGeneric 
  151.             Caption         =   "All"
  152.          End
  153.       End
  154.       Begin VB.Menu SortDesc 
  155.          Caption         =   "Descending"
  156.          Begin VB.Menu DescNumeric 
  157.             Caption         =   "Numeric"
  158.          End
  159.          Begin VB.Menu DescString 
  160.             Caption         =   "String"
  161.             Begin VB.Menu DescStringSensitive 
  162.                Caption         =   "Case Sensitive"
  163.             End
  164.             Begin VB.Menu DescStringNonSensitive 
  165.                Caption         =   "case insensitive"
  166.             End
  167.          End
  168.          Begin VB.Menu DescGeneric 
  169.             Caption         =   "All"
  170.          End
  171.       End
  172.    End
  173.    Begin VB.Menu AlignMenu 
  174.       Caption         =   "Align"
  175.       Begin VB.Menu AlignLeft 
  176.          Caption         =   "Align Left"
  177.       End
  178.       Begin VB.Menu AlignCenter 
  179.          Caption         =   "Center"
  180.       End
  181.       Begin VB.Menu AlignRight 
  182.          Caption         =   "Align Right"
  183.       End
  184.    End
  185. Attribute VB_Name = "FlexGridControl"
  186. Attribute VB_GlobalNameSpace = False
  187. Attribute VB_Creatable = False
  188. Attribute VB_PredeclaredId = True
  189. Attribute VB_Exposed = False
  190. '  ******************************
  191. '  ******************************
  192. '  ** MASTERING VB6            **
  193. '  ** by Evangelos Petroutos   **
  194. '  ** SYBEX, 1998              **
  195. '  ******************************
  196. '  ******************************
  197. Option Explicit
  198. Dim OpenFile As String
  199. Sub NumberCells()
  200. Dim i As Integer
  201.     For i = 1 To Grid.Rows - 1
  202.         Grid.TextMatrix(0, i) = Format$(i, "000")
  203.     Next
  204.     For i = 1 To Grid.Cols - 1
  205.         Grid.TextMatrix(i, 0) = " " & Format$(i, "000")
  206.     Next
  207.     Grid.ColWidth(0) = TextWidth("99999")
  208. End Sub
  209. Sub FormatCells(formatString)
  210. Dim irow, icol As Integer
  211.     For irow = Grid.Row To Grid.RowSel
  212.         For icol = Grid.Col To Grid.ColSel
  213.             Grid.TextMatrix(irow, icol) = Format$(Grid.TextMatrix(irow, icol), formatString)
  214.         Next
  215.     Next
  216. End Sub
  217. Private Sub AlignCenter_Click()
  218.     Grid.FillStyle = flexFillRepeat
  219.     Grid.CellAlignment = 4
  220.     Grid.FillStyle = flexFillSingle
  221. End Sub
  222. Private Sub AlignLeft_Click()
  223.     Grid.FillStyle = flexFillRepeat
  224.     Grid.CellAlignment = 1
  225.     Grid.FillStyle = flexFillSingle
  226. End Sub
  227. Private Sub AlignRight_Click()
  228.     Grid.FillStyle = flexFillRepeat
  229.     Grid.CellAlignment = 7
  230.     Grid.FillStyle = flexFillSingle
  231. End Sub
  232. Private Sub AscGeneric_Click()
  233.     Grid.Sort = 1
  234. End Sub
  235. Private Sub AscNumeric_Click()
  236.     Grid.Sort = 3
  237. End Sub
  238. Private Sub AscStringNonsensitive_Click()
  239.     Grid.Sort = 5
  240. End Sub
  241. Private Sub AscStringSensitive_Click()
  242.     Grid.Sort = 7
  243. End Sub
  244. Private Sub DescGeneric_Click()
  245.     Grid.Sort = 2
  246. End Sub
  247. Private Sub DescNumeric_Click()
  248.     Grid.Sort = 4
  249. End Sub
  250. Private Sub DescStringNonSensitive_Click()
  251.     Grid.Sort = 6
  252. End Sub
  253. Private Sub DescStringSensitive_Click()
  254.     Grid.Sort = 8
  255. End Sub
  256. Private Sub EditClear_Click()
  257. Dim irow As Integer, icol As Integer
  258.     For irow = Grid.Row To Grid.RowSel
  259.         For icol = Grid.Col To Grid.ColSel
  260.             Grid.TextMatrix(irow, icol) = ""
  261.         Next
  262.     Next
  263. End Sub
  264. Private Sub EditCopy_Click()
  265. Dim tmpText As String
  266.     tmpText = Grid.Clip
  267.     Clipboard.Clear
  268.     Clipboard.SetText tmpText
  269. End Sub
  270. Private Sub EditCut_Click()
  271. Dim tmpText As String
  272.     tmpText = Grid.Clip
  273.     Clipboard.Clear
  274.     Clipboard.SetText tmpText
  275.     EditClear_Click
  276. End Sub
  277. Private Sub EditPaste_Click()
  278. Dim tmpText As String
  279.     tmpText = Clipboard.GetText
  280.     Grid.Clip = tmpText
  281. End Sub
  282. Private Sub EditSelect_Click()
  283.     Grid.Row = 1
  284.     Grid.Col = 1
  285.     Grid.RowSel = Grid.Rows - 1
  286.     Grid.ColSel = Grid.Cols - 1
  287. End Sub
  288. Private Sub FileNew_Click()
  289.     Grid.Clear
  290.     Text1.Text = ""
  291. End Sub
  292. Private Sub FileOpen_Click()
  293. Dim allCells As String
  294. Dim fnum As Integer
  295. Dim curRow, curCol As Integer
  296. On Error GoTo NoFileSelected
  297.     CommonDialog1.Filter = "FlexGrid Files|*.grd|All Files|*.*"
  298.     CommonDialog1.CancelError = True
  299.     CommonDialog1.InitDir = App.Path
  300.     CommonDialog1.ShowOpen
  301.     OpenFile = CommonDialog1.FileName
  302.     fnum = FreeFile
  303.     Open OpenFile For Input As #fnum
  304.     Input #fnum, allCells
  305.     EditSelect_Click
  306.     Grid.Clip = allCells
  307.     Close #fnum
  308.     Grid.Row = 1
  309.     Grid.Col = 1
  310.     Grid.RowSel = Grid.Row
  311.     Grid.ColSel = Grid.Col
  312.     NumberCells
  313.     Exit Sub
  314. NoFileSelected:
  315.     Exit Sub
  316. End Sub
  317. Private Sub FileSave_Click()
  318. Dim fnum As Integer
  319. Dim allCells As String
  320.     EditSelect_Click
  321.     allCells = Grid.Clip
  322.     fnum = FreeFile
  323.     If OpenFile = "" Then
  324.         CommonDialog1.DefaultExt = "FLEXGRID Files|GDT"
  325.         CommonDialog1.Action = 1
  326.         OpenFile = CommonDialog1.FileName
  327.         If OpenFile = "" Then Exit Sub
  328.     End If
  329.     Open OpenFile For Input As #fnum
  330.     Input #fnum, allCells
  331.     EditSelect_Click
  332.     Grid.Clip = allCells
  333.     Close #fnum
  334. End Sub
  335. Private Sub FileSaveAs_Click()
  336. Dim allCells As String
  337. Dim fnum As Integer
  338. Dim curRow, curCol As Integer
  339.     curRow = Grid.Row
  340.     curCol = Grid.Col
  341.     CommonDialog1.DefaultExt = "GRD"
  342.     CommonDialog1.Action = 2
  343.     If CommonDialog1.FileName = "" Then Exit Sub
  344.     EditSelect_Click
  345.     allCells = Grid.Clip
  346.     fnum = FreeFile
  347.     Open CommonDialog1.FileName For Output As #fnum
  348.     Write #fnum, allCells
  349.     Close #fnum
  350.     Grid.Row = curRow
  351.     Grid.Col = curCol
  352.     Grid.RowSel = Grid.Row
  353.     Grid.ColSel = Grid.Col
  354. End Sub
  355. Private Sub Form_Load()
  356.     NumberCells
  357. End Sub
  358. Private Sub Format2decimal_Click()
  359.     FormatCells ("###.00")
  360. End Sub
  361. Private Sub FormatCellColor_Click()
  362.     CommonDialog1.ShowColor
  363.     Grid.FillStyle = flexFillRepeat
  364.     Grid.CellBackColor = CommonDialog1.Color
  365.     Grid.FillStyle = flexFillSingle
  366. End Sub
  367. Private Sub FormatComaInteger_Click()
  368.     FormatCells ("#,###.00")
  369. End Sub
  370. Private Sub FormatDollar_Click()
  371.     FormatCells ("$#,###.00")
  372. End Sub
  373. Private Sub FormatFont_Click()
  374. On Error GoTo NoFontSelected
  375.     CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects
  376.     CommonDialog1.Color = Grid.CellForeColor
  377.     CommonDialog1.CancelError = True
  378.     CommonDialog1.FontName = Grid.CellFontName
  379.     CommonDialog1.FontBold = Grid.CellFontBold
  380.     CommonDialog1.FontItalic = Grid.CellFontItalic
  381.     CommonDialog1.FontSize = Grid.CellFontSize
  382.     CommonDialog1.Color = Grid.CellForeColor
  383.     CommonDialog1.ShowFont
  384.     Grid.FillStyle = flexFillRepeat
  385.     Grid.CellFontName = CommonDialog1.FontName
  386.     Grid.CellFontBold = CommonDialog1.FontBold
  387.     Grid.CellFontItalic = CommonDialog1.FontItalic
  388.     Grid.CellFontSize = CommonDialog1.FontSize
  389.     Grid.CellForeColor = CommonDialog1.Color
  390.     Grid.FillStyle = flexFillSingle
  391.     Exit Sub
  392. NoFontSelected:
  393.     Exit Sub
  394. End Sub
  395. Private Sub FormatInteger_Click()
  396.     FormatCells ("###")
  397. End Sub
  398. Private Sub Grid_Click()
  399.     Label1.Caption = Grid.TextMatrix(Grid.Col, 0) & " : " & Grid.TextMatrix(0, Grid.Row)
  400.     Text1.Text = Grid.Text
  401.     Text1.SetFocus
  402. End Sub
  403. Private Sub Grid_EnterCell()
  404. On Error Resume Next
  405.     Text1.Text = Grid.Text
  406.     Text1.SelStart = 0
  407.     Text1.SelLength = Len(Text1.Text)
  408. End Sub
  409. Private Sub Grid_LeaveCell()
  410.     Grid.Text = Text1.Text
  411. End Sub
  412. Private Sub MergeBoth_Click()
  413. Dim irow, icol As Integer
  414.     For irow = Grid.Row To Grid.RowSel
  415.         Grid.MergeRow(irow) = True
  416.     Next
  417.     Grid.MergeCells = 4
  418.     For icol = Grid.Col To Grid.ColSel
  419.         Grid.MergeCol(icol) = True
  420.     Next
  421.     Grid.MergeCells = 4
  422. End Sub
  423. Private Sub MergeCols_Click()
  424. Dim icol As Integer
  425.     For icol = Grid.Col To Grid.ColSel
  426.         Grid.MergeCol(icol) = True
  427.     Next
  428.     Grid.MergeCells = 3
  429. End Sub
  430. Private Sub MergeFree_Click()
  431. Dim irow, icol As Integer
  432.     For irow = Grid.Row To Grid.RowSel
  433.         Grid.MergeRow(irow) = True
  434.     Next
  435.     For icol = Grid.Col To Grid.ColSel
  436.         Grid.MergeCol(icol) = True
  437.     Next
  438.     Grid.MergeCells = 1
  439. End Sub
  440. Private Sub MergeNone_Click()
  441.     Grid.MergeCells = 0
  442. End Sub
  443. Private Sub MergeRows_Click()
  444. Dim irow As Integer
  445.     For irow = Grid.Row To Grid.RowSel
  446.         Grid.MergeRow(irow) = True
  447.     Next
  448.     Grid.MergeCells = 2
  449. End Sub
  450. Private Sub Text1_KeyPress(KeyAscii As Integer)
  451. Dim SRow, SCol As Integer
  452.     If KeyAscii = 13 Then
  453.         Grid.Text = Text1.Text
  454.         SRow = Grid.Row + 1
  455.         SCol = Grid.ColSel
  456.         If SRow = Grid.Rows Then
  457.             SRow = Grid.FixedCols
  458.             If SCol < Grid.Cols - Grid.FixedCols Then SCol = SCol + 1
  459.         End If
  460.         Grid.Row = SRow
  461.         Grid.Col = SCol
  462.         Grid.RowSel = SRow
  463.         Grid.ColSel = SCol
  464.         Text1.Text = Grid.Text
  465.         Text1.SetFocus
  466.         KeyAscii = 0
  467.     End If
  468. End Sub
  469.