home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Learn_abou623163152002.psc / frmSampleCode.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2002-03-14  |  14.2 KB  |  416 lines

  1. VERSION 5.00
  2. Begin VB.Form frmSample 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Sample..."
  5.    ClientHeight    =   7545
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   10920
  9.    Icon            =   "frmSampleCode.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    LockControls    =   -1  'True
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   7545
  15.    ScaleWidth      =   10920
  16.    StartUpPosition =   2  'CenterScreen
  17.    Begin VB.Frame fraNoSample 
  18.       Height          =   900
  19.       Left            =   8955
  20.       TabIndex        =   23
  21.       Top             =   345
  22.       Visible         =   0   'False
  23.       Width           =   2655
  24.       Begin VB.Label Label8 
  25.          Alignment       =   2  'Center
  26.          Caption         =   "Sorry !. No sample available for this topic now."
  27.          Height          =   465
  28.          Left            =   120
  29.          TabIndex        =   24
  30.          Top             =   270
  31.          Width           =   2430
  32.       End
  33.    End
  34.    Begin VB.Frame fraSkipLine 
  35.       Caption         =   "Skip Line:"
  36.       Height          =   2160
  37.       Left            =   4590
  38.       TabIndex        =   14
  39.       Top             =   3120
  40.       Visible         =   0   'False
  41.       Width           =   4245
  42.       Begin VB.TextBox txtSkipLine 
  43.          Height          =   1275
  44.          Left            =   75
  45.          MultiLine       =   -1  'True
  46.          ScrollBars      =   3  'Both
  47.          TabIndex        =   22
  48.          Top             =   195
  49.          Width           =   4080
  50.       End
  51.       Begin VB.CommandButton cmdSkipLine 
  52.          Caption         =   "SkipLine"
  53.          Height          =   390
  54.          Left            =   3120
  55.          TabIndex        =   20
  56.          Top             =   1635
  57.          Width           =   900
  58.       End
  59.       Begin VB.Label Label7 
  60.          Caption         =   "Skips the odd/even lines from the samplefile.txt file and display the remaining content"
  61.          Height          =   645
  62.          Left            =   180
  63.          TabIndex        =   21
  64.          Top             =   1500
  65.          Width           =   2805
  66.       End
  67.    End
  68.    Begin VB.Frame fraRead 
  69.       Caption         =   "Read :"
  70.       Height          =   2670
  71.       Left            =   4575
  72.       TabIndex        =   3
  73.       Top             =   330
  74.       Visible         =   0   'False
  75.       Width           =   4245
  76.       Begin VB.TextBox txtRead 
  77.          Height          =   1200
  78.          Left            =   90
  79.          MultiLine       =   -1  'True
  80.          ScrollBars      =   3  'Both
  81.          TabIndex        =   18
  82.          Top             =   765
  83.          Width           =   4005
  84.       End
  85.       Begin VB.CommandButton cmdRead 
  86.          Caption         =   "Read"
  87.          Height          =   390
  88.          Left            =   3120
  89.          TabIndex        =   17
  90.          Top             =   2160
  91.          Width           =   975
  92.       End
  93.       Begin VB.TextBox txtReadChars 
  94.          Height          =   345
  95.          Left            =   2880
  96.          TabIndex        =   15
  97.          Top             =   225
  98.          Width           =   1200
  99.       End
  100.       Begin VB.Label Label6 
  101.          Caption         =   "Reads the n from the file samplefile.txt"
  102.          Height          =   300
  103.          Left            =   90
  104.          TabIndex        =   19
  105.          Top             =   2235
  106.          Width           =   2850
  107.       End
  108.       Begin VB.Label Label5 
  109.          Caption         =   "Enter the number of chars to be read"
  110.          Height          =   300
  111.          Left            =   90
  112.          TabIndex        =   16
  113.          Top             =   270
  114.          Width           =   2640
  115.       End
  116.    End
  117.    Begin VB.Frame fraReadLine 
  118.       Caption         =   "ReadLine :"
  119.       Height          =   1905
  120.       Left            =   195
  121.       TabIndex        =   2
  122.       Top             =   5355
  123.       Visible         =   0   'False
  124.       Width           =   4245
  125.       Begin VB.TextBox txtReadLine 
  126.          Height          =   1020
  127.          Left            =   60
  128.          MultiLine       =   -1  'True
  129.          ScrollBars      =   3  'Both
  130.          TabIndex        =   12
  131.          Top             =   240
  132.          Width           =   4095
  133.       End
  134.       Begin VB.CommandButton cmdReadLine 
  135.          Caption         =   "Read Line"
  136.          Height          =   390
  137.          Left            =   3030
  138.          TabIndex        =   11
  139.          Top             =   1410
  140.          Width           =   1110
  141.       End
  142.       Begin VB.Label Label4 
  143.          Caption         =   "Reads line by line from the file app.path + ""\"" + samplefile.txt and displays here."
  144.          Height          =   450
  145.          Left            =   60
  146.          TabIndex        =   13
  147.          Top             =   1335
  148.          Width           =   2910
  149.       End
  150.    End
  151.    Begin VB.Frame fraReadAll 
  152.       Caption         =   "ReadAll :"
  153.       Height          =   3645
  154.       Left            =   195
  155.       TabIndex        =   1
  156.       Top             =   1650
  157.       Visible         =   0   'False
  158.       Width           =   4245
  159.       Begin VB.TextBox txtReadAll 
  160.          Height          =   2730
  161.          Left            =   105
  162.          MultiLine       =   -1  'True
  163.          ScrollBars      =   3  'Both
  164.          TabIndex        =   10
  165.          Top             =   195
  166.          Width           =   4065
  167.       End
  168.       Begin VB.CommandButton cmdReadAll 
  169.          Caption         =   "Read All"
  170.          Height          =   390
  171.          Left            =   3105
  172.          TabIndex        =   8
  173.          Top             =   3075
  174.          Width           =   945
  175.       End
  176.       Begin VB.Label Label3 
  177.          Caption         =   "Reads the file app.path + ""\"" + samplefile.txt and fills the textbox"
  178.          Height          =   510
  179.          Left            =   120
  180.          TabIndex        =   9
  181.          Top             =   3015
  182.          Width           =   2835
  183.          WordWrap        =   -1  'True
  184.       End
  185.    End
  186.    Begin VB.Frame fraCreateFile 
  187.       Caption         =   "CreateFile :"
  188.       Height          =   1335
  189.       Left            =   195
  190.       TabIndex        =   0
  191.       Top             =   240
  192.       Visible         =   0   'False
  193.       Width           =   4260
  194.       Begin VB.TextBox txtCreatefile 
  195.          Height          =   285
  196.          Left            =   1470
  197.          TabIndex        =   5
  198.          Text            =   "a.txt"
  199.          Top             =   345
  200.          Width           =   2655
  201.       End
  202.       Begin VB.CommandButton cmdCreateFile 
  203.          Caption         =   "Create "
  204.          Height          =   390
  205.          Left            =   2745
  206.          TabIndex        =   4
  207.          Top             =   795
  208.          Width           =   1365
  209.       End
  210.       Begin VB.Label Label2 
  211.          Caption         =   "File with the name app.path + ""\"" + textbox.text will be created."
  212.          Height          =   465
  213.          Left            =   135
  214.          TabIndex        =   7
  215.          Top             =   735
  216.          Width           =   2505
  217.          WordWrap        =   -1  'True
  218.       End
  219.       Begin VB.Label Label1 
  220.          Caption         =   "Enter File Name:"
  221.          Height          =   240
  222.          Left            =   150
  223.          TabIndex        =   6
  224.          Top             =   375
  225.          Width           =   1230
  226.       End
  227.    End
  228. Attribute VB_Name = "frmSample"
  229. Attribute VB_GlobalNameSpace = False
  230. Attribute VB_Creatable = False
  231. Attribute VB_PredeclaredId = True
  232. Attribute VB_Exposed = False
  233. Option Explicit
  234. Private m_iCurScreen As Integer    ' will be set from the caller
  235. Private m_objFS As Scripting.FileSystemObject
  236. Private m_objTextStream As Scripting.TextStream
  237. Private m_objFile As Scripting.File ' To access a file from the harddisk
  238. Const Con_lExtraHt = 500
  239. Const Con_lExtraWt = 150
  240. Const ConSampleFile = "SampleFile.txt"      ' sample file name used
  241. Public Property Let CurrentTopic(vData As Integer)
  242. ' This will be set by the caller
  243.     m_iCurScreen = vData
  244.     SetCurScreen
  245. End Property
  246. Private Sub SetCurScreen()
  247. ' Show the proper layer for the current example selected
  248.     'HideAllLayers
  249.     Select Case m_iCurScreen
  250.         Case 2      ' CreateFile
  251.             ShowLayer fraCreateFile
  252.         Case 4      'Read
  253.             ShowLayer fraRead
  254.         Case 5      'ReadAll
  255.             ShowLayer fraReadAll
  256.         Case 6      ' ReadLine
  257.             ShowLayer fraReadLine
  258.         Case 8      ' SkipLine
  259.             ShowLayer fraSkipLine
  260.         Case Else
  261.             ShowLayer fraNoSample
  262.     End Select
  263. End Sub
  264. Private Sub HideAllLayers()
  265. ' Hide all the layers
  266. On Error Resume Next
  267. Dim bBool As Boolean
  268.     bBool = False
  269.     fraCreateFile.Visible = bBool
  270.     fraNoSample.Visible = bBool
  271.     fraRead.Visible = bBool
  272.     fraReadLine.Visible = bBool
  273.     fraSkipLine.Visible = bBool
  274. End Sub
  275. Private Sub cmdCreateFile_Click()
  276.     ' Create the file mentioned in the file name
  277.     ' avoid any path if user has typed.
  278.     Dim sFile As String
  279.     Dim lRet As Long
  280.     If txtCreatefile.Text = "" Then
  281.         sFile = App.Path + "\" + "a.txt"
  282.     Else    ' get the file name and append with the app.path
  283.         sFile = App.Path + "\" + GetFileName(txtCreatefile.Text)
  284.     End If
  285.     'if the file already exist ask the user for overwrite
  286.     If LCase(Dir(sFile, vbNormal)) = LCase(GetFileName(sFile)) Then
  287.         lRet = MsgBox("File already exists. Do you want to overwrite?", vbOKCancel)
  288.         If lRet = vbCancel Then Exit Sub
  289.         
  290.         ' if the file has readonly attribute remove that
  291.         Set m_objFS = New Scripting.FileSystemObject
  292.         Set m_objFile = m_objFS.GetFile(sFile)
  293.         m_objFile.Attributes = Archive  ' 0
  294.         
  295.         Set m_objFile = Nothing
  296.         Set m_objFS = Nothing
  297.         DoEvents: DoEvents: DoEvents
  298.     End If
  299.            
  300.     ' Create the file and write a comment line there
  301.     Set m_objFS = New Scripting.FileSystemObject
  302.     Set m_objTextStream = m_objFS.CreateTextFile(sFile, True)
  303.     ' Write some information
  304.     m_objTextStream.WriteLine Now
  305.     m_objTextStream.WriteLine "This is the file created with FileSystem object "
  306.     Set m_objFS = Nothing
  307.     Set m_objTextStream = Nothing
  308. End Sub
  309. Private Sub ShowLayer(fraLayer As Frame)
  310. On Error GoTo ErrShow
  311. ' Move the required layer to the left top and resize the form to its size
  312.  fraLayer.Move 10, 10
  313.  Me.Width = fraLayer.Left + fraLayer.Width + Con_lExtraWt
  314.  Me.Height = fraLayer.Top + fraLayer.Height + Con_lExtraHt
  315.  fraLayer.Visible = True
  316.  Exit Sub
  317. ErrShow:
  318.     MsgBox "Error in Show Layer : " + Err.Description
  319. End Sub
  320. Private Sub cmdReadAll_Click()
  321. On Error GoTo ErrRead
  322.     ' Open the file and read all the contents in one go and display in the
  323.     ' text box
  324.     Dim sSampleFile As String
  325.     sSampleFile = App.Path + "\" + ConSampleFile
  326.     Set m_objFS = New Scripting.FileSystemObject
  327.     Set m_objTextStream = m_objFS.OpenTextFile(sSampleFile, ForReading)
  328.     txtReadAll.Text = ""
  329.     txtReadAll.Text = m_objTextStream.ReadAll
  330.     Set m_objFS = Nothing
  331.     Set m_objTextStream = Nothing
  332.     Exit Sub
  333. ErrRead:
  334.     MsgBox "CmdReadAll Click : " + Err.Description
  335. End Sub
  336. Private Sub cmdReadLine_Click()
  337. On Error GoTo ErrRead
  338.     Dim sSampleFile As String
  339.     Dim sFromFile As String
  340.     sSampleFile = App.Path + "\" + ConSampleFile
  341.     Set m_objFS = New Scripting.FileSystemObject
  342.     Set m_objTextStream = m_objFS.OpenTextFile(sSampleFile, ForReading)
  343.     txtReadLine.Text = ""
  344.     Do While Not m_objTextStream.AtEndOfStream
  345.         ' Read the line from the text file
  346.         sFromFile = m_objTextStream.ReadLine
  347.         txtReadLine.Text = txtReadLine.Text + vbCrLf + sFromFile
  348.     Loop
  349.     Set m_objFS = Nothing
  350.     Set m_objTextStream = Nothing
  351.     Exit Sub
  352. ErrRead:
  353.     MsgBox "CmdReadLine click : " + Err.Description
  354. End Sub
  355. Private Sub cmdRead_Click()
  356. On Error GoTo ErrRead
  357.     Dim sSampleFile As String
  358.     Dim iNumChars As Integer
  359.     sSampleFile = App.Path + "\" + ConSampleFile
  360.     iNumChars = txtReadChars.Text
  361.     ' If nothing mentioned take 10 as the default
  362.     If iNumChars = 0 Then iNumChars = 10
  363.     Set m_objFS = New Scripting.FileSystemObject
  364.     Set m_objTextStream = m_objFS.OpenTextFile(sSampleFile, ForReading)
  365.     txtRead.Text = ""
  366.     txtRead.Text = m_objTextStream.Read(iNumChars)
  367.     Set m_objFS = Nothing
  368.     Set m_objTextStream = Nothing
  369.     Exit Sub
  370. ErrRead:
  371.     MsgBox "CmdRead Click : " + Err.Description
  372. End Sub
  373. Private Sub cmdSkipLine_Click()
  374. On Error GoTo ErrSkip
  375.     Dim sSampleFile As String
  376.     Dim sFromFile As String
  377.     Dim bSkip As Boolean
  378.     sSampleFile = App.Path + "\" + ConSampleFile
  379.     Set m_objFS = New Scripting.FileSystemObject
  380.     Set m_objTextStream = m_objFS.OpenTextFile(sSampleFile, ForReading)
  381.     txtSkipLine.Text = ""
  382.     Do While Not m_objTextStream.AtEndOfStream
  383.         ' Read the alternate lines from the text file
  384.         If bSkip Then
  385.             m_objTextStream.SkipLine
  386.         Else
  387.             sFromFile = m_objTextStream.ReadLine
  388.             txtSkipLine.Text = txtSkipLine.Text + vbCrLf + sFromFile
  389.         End If
  390.         bSkip = Not bSkip
  391.         
  392.     Loop
  393.     Set m_objFS = Nothing
  394.     Set m_objTextStream = Nothing
  395.     Exit Sub
  396. ErrSkip:
  397.     MsgBox "CmdSkipLine click : " + Err.Description
  398. End Sub
  399. Private Sub txtReadChars_KeyPress(KeyAscii As Integer)
  400. On Error GoTo ErrReadChars
  401.     ' Alow only  a 4 digit number. The sample file may not be so big.If more number is needed
  402.     ' then change this code
  403.     If Len(txtReadChars.Text) = 4 Then KeyAscii = 0: Exit Sub
  404.     ' Allow only numeric and backspace here
  405.     If KeyAscii = 8 Then
  406.         ' do nothing
  407.     ElseIf KeyAscii >= 48 And KeyAscii <= 57 Then
  408.         ' do nothing
  409.     Else    ' do not allow the character
  410.         KeyAscii = 0
  411.     End If
  412.     Exit Sub
  413. ErrReadChars:
  414.     MsgBox "txtReadChars KeyPress : " + Err.Description
  415. End Sub
  416.