How to implement progress bar
Hi guys, I need help on how to implement the progress bar when reading the file. Please help. Thank you
Private Sub cmdStart_Click()
Dim strdata As String
Dim FlexData As String
Dim Opcode As String
Dim inputcommand As String
Dim newlinelength, LineLen As Integer
Dim fs, f
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(txtFilename, ForReading, 0)
Do While f.AtEndOfLine <> True
strdata = f.ReadLine
newlinelength = Len(strdata) - 4 'Pos of chara b4 the last 4 chara
LineLen = newlinelength - 28 'Data length
FlexData = Mid(strdata, 29, LineLen) 'Extract data
inputcommand = Mid(strdata, 17, 4)
Opcode = "OPCODE=" & inputcommand
txtDisplayFlex.Text = txtDisplayFlex.Text & Opcode & vbCrLf & "Data = " & FlexData & vbCrLf & vbCrLf
Loop
f.Close
End Sub
[1063 byte] By [
Trazz] at [2007-11-11 10:07:25]

# 1 Re: How to implement progress bar
take the length of the file as progress max value , u can get it using filelen()
and every line read , increase the progress bar with the string length .
Is it a small dissasembler ?
Amahdy at 2007-11-11 17:23:10 >

# 2 Re: How to implement progress bar
hi Amahday,
wat do u mean by dissasembler?
I tried your method of filelen() as in my code below; by setting ProgressBar1.Max = 100 the Progressbar is able to work. Now i created a label to should the percentage status but the display dun seems to show out. It only show out when EOF. Any recommendations?
Option Explicit
Dim FileSize
Private Sub cmdBrowse_Click()
'-- Get a text file name to open
CommonDialog1.DialogTitle = "Open Flex File"
CommonDialog1.CancelError = True
CommonDialog1.Filter = "flex (*.hs)"
CommonDialog1.FileName = "*.hs"
CommonDialog1.Action = 1
'-- Display the file name
txtFilename = CommonDialog1.FileName
FileSize = FileLen(txtFilename) ' Returns file length (bytes).
End Sub
Private Sub cmdStart_Click()
Dim strdata As String
Dim fs, f
Dim iProgess, iLineCount As Long
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(txtFilename, ForReading, 0)
ProgressBar1.Max = 100
Do While f.AtEndOfLine <> True
strdata = f.ReadLine
strdataSize = Len(strdata)
'iLineCount = strdataSize
'***Codes for flexing line by line***
newlinelength = Len(strdata) - 4 'Position of the chara before the last 4 chara
LineLen = newlinelength - 28 'Data length
FlexData = Mid(strdata, 29, LineLen) 'Extract data
inputcommand = Mid(strdata, 17, 4)
txtDisplayFlex.Text = txtDisplayFlex.Text & inputcommand & vbCrLf & "Data = " & FlexData & vbCrLf & vbCrLf
iLineCount = iLineCount + strdataSize
ProgressBar1.Value = Int(100 * (iLineCount / FileSize))
lblFlexStatus.Caption = "Flexing in progress... " & ProgressBar1.Value & "%"
Loop
f.Close
End Sub
Trazz at 2007-11-11 17:24:10 >

# 3 Re: How to implement progress bar
Maybe very fast ? put a break point inside the loop and see it .. if not try to put a doevents inside the loop .
*The disassembler is a programme that get the assembly language back from an executable file [in the final format: object code] (read opcode, transform opcode to assembly lang.) .
Amahdy at 2007-11-11 17:25:16 >

# 4 Re: How to implement progress bar
Try adding a DoEvents statement within your loop. That will temporarily release control to the OS so that your Form will re-paint during processing.
# 5 Re: How to implement progress bar
Try This:Private Sub cmdStart_Click()
Dim strdata As String
Dim FlexData As String
Dim Opcode As String
Dim inputcommand As String
Dim newlinelength As Integer, LineLen As Integer
Dim fs, f
Dim fSize As Long, BytesRead As Long
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Set fs = CreateObject("Scripting.FileSystemObject")
'get file object for size lookup
Set f = fs.GetFile(txtFilename)
fSize = f.Size
'now that we have file size reuse f for TextStream Object
Set f = fs.OpenTextFile(txtFilename, ForReading, 0)
'Set Progressbar limits
ProgressBar1.Min = 0
ProgressBar1.Max = fSize
Do While f.AtEndOfLine <> True
strdata = f.ReadLine
'increment Bytes Read
BytesRead = BytesRead + Len(strdata)
newlinelength = Len(strdata) - 4 'Pos of chara b4 the last 4 chara
LineLen = newlinelength - 28 'Data length
FlexData = Mid(strdata, 29, LineLen) 'Extract data
inputcommand = Mid(strdata, 17, 4)
Opcode = "OPCODE=" & inputcommand
txtDisplayFlex.Text = txtDisplayFlex.Text & Opcode & vbCrLf & "Data = " & FlexData & vbCrLf & vbCrLf
'set progress by number of bytes read
ProgressBar1.Value = BytesRead
Loop
'make sure progressbar reaches the end
ProgressBar1.Value = fSize
f.Close
Set f = Nothing
Set fs = Nothing
End Sub
# 6 Re: How to implement progress bar
Hi Ron,
your codes works the same as mine. Thanks. At least i learnt something.
Set f = fs.GetFile(txtFilename) & fSize = f.Size
hi Amahday & Paul,
i added "lblStatus.Caption = DoEvents" just above the loop and it works! Thanks alot
Trazz at 2007-11-11 17:28:21 >

# 7 Re: How to implement progress bar
u don't need to assigne it to any thing just put it in a separated line :
"
.
.
DoevEnts
.
.
.
"
It's a keyword and u don't need it's returned value just you need it's effect .
Amahdy at 2007-11-11 17:29:14 >
