|
-
Jun 16th, 2010, 02:24 AM
#1
Thread Starter
Member
[RESOLVED] Help in Listbox
Hi all,
I wanted to construct a stop button to stop or interrupt the program from executing.
Code:
Option Explicit
Private Sub Write_Text(ByVal A, ByVal B, ByVal C)
List1.AddItem A & " | " & B & " | " & C & vbCrLf
End Sub
Private Sub Run_Button_Click()
Write_Text "A1", 1, 1
Write_Text "A2", 2, 2
Write_Text "A3", 3, 3
End Sub
In the Run_Button_Click, there r a lot of write_text
e.g. Write_Text "A1", 1, 1
Write_Text "A2", 2, 2
Write_Text "A3", 3, 3
:
:
Write_Text "A100", 100, 100
So when I click the stop button when the program executing Write_Text "A10", 10, 10 . It will stop and not execute Write_Text "A11", 11, 11
'*********************************************************
Need advise from someone.
Regards,
Kent
Last edited by kent5244; Jun 16th, 2010 at 02:55 AM.
Reason: Question 1 solved
-
Jun 16th, 2010, 03:54 AM
#2
Re: Help in Listbox
One way would be to set a variable so the write sub only adds if the variable is not set. If the code is busy you'll need to call DoEvents so the stop button click is acknowledged, example,...
Code:
Option Explicit
Dim BolStop As Boolean
Private Sub Run_Button_Click()
BolStop = False ' reset stop flag
Run_Button.Enabled = False ' disable button while writing
' For testing: Write lots of values...
Dim i As Integer
List1.Clear
For i = 0 To 32765
Write_Text "A" & i, i, i
If BolStop = True Then Exit For
Next i
Run_Button.Enabled = True ' all done, enable button.
End Sub
Private Sub Stop_Button_Click()
BolStop = True ' Set stop flag
End Sub
Private Sub Write_Text(ByVal A As String, ByVal B As String, ByVal C As String)
DoEvents ' update
If BolStop = False Then ' check stop flag
List1.AddItem A & " | " & B & " | " & C & vbCrLf
End If
End Sub
Last edited by Edgemeal; Jun 16th, 2010 at 03:59 AM.
-
Jun 16th, 2010, 04:16 AM
#3
Thread Starter
Member
Re: Help in Listbox
Edgemeal:
Thanks for your reply. But there is another problem. The value of parameters are not nicely arrange in the e.g. The actual output is similar to as following.
e.g. Write_Text "R1", 1000, 2200
Write_Text "C2", 1100, 2000
Write_Text "V3", 500, 600
:
:
Write_Text "R100", 1200, 1500
 Originally Posted by Edgemeal
Private Sub Run_Button_Click()
' For testing: Write lots of values...
Dim i As Integer
List1.Clear
For i = 0 To 32765
Write_Text "A" & i, i, i
If BolStop = True Then Exit For
Next i
Sorry for not illustrate my question clearly.
-
Jun 16th, 2010, 05:53 AM
#4
Re: Help in Listbox
What difference does how the numbes are arranged make?
If you want to stop execution, you MUST exit the loop, and Edgemeal illustrated how that would be done.
-
Jun 16th, 2010, 07:00 AM
#5
Thread Starter
Member
Re: Help in Listbox
 Originally Posted by Hack
What difference does how the numbes are arranged make?
If you want to stop execution, you MUST exit the loop, and Edgemeal illustrated how that would be done.
Hack:
Do u mean that i should replace the following code
Private Sub Run_Button_Click()
Code:
' For testing: Write lots of values...
Dim i As Integer
List1.Clear
For i = 0 To 32765
Write_Text "A" & i, i, i
If BolStop = True Then Exit For
Next i
with
Code:
Write_Text "R1", 1000, 2200
Write_Text "C2", 1100, 2000
Write_Text "V3", 500, 600
:
:
Write_Text "R100", 1200, 1500
-
Jun 16th, 2010, 01:58 PM
#6
Re: Help in Listbox
Do u mean that i should replace the following code
Yes, replace that test code loop with your own code, obviously it was only provided for testing.
-
Jun 16th, 2010, 05:54 PM
#7
Re: Help in Listbox
I don't think he uses a loop.
In which case you'd want to use DoEvents(to allow a 'stop button' Click event to fire, which sets the variable) and a check for the module-level variable, and then exit sub or function if True.
Such as:
vb Code:
Write_Text "R1", 1000, 2200
DoEvents: If BolStop Then Exit Sub/Function
Write_Text "C2", 1100, 2000
DoEvents: If BolStop Then Exit Sub/Function
Write_Text "V3", 500, 600
DoEvents: If BolStop Then Exit Sub/Function
'...
I'd also suggest a static variable, which will guard against re-entry. Which'll look something like:
vb Code:
Sub/Function WhatHaveYou()
Static bolInProc as Boolean
If bolInProc Then Exit Sub/Function Else bolInProc = True
Write_Text "R1", 1000, 2200
DoEvents: If BolStop Then bolInProc = False: Exit Sub/Function
Write_Text "C2", 1100, 2000
DoEvents: If BolStop Then bolInProc = False: Exit Sub/Function
Write_Text "V3", 500, 600
DoEvents: If BolStop Then bolInProc = False: Exit Sub/Function
'...
bolInProc = False
End Sub/Function
Last edited by FireXtol; Jun 16th, 2010 at 06:03 PM.
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6( ).
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
-
Jun 16th, 2010, 10:29 PM
#8
Thread Starter
Member
Re: Help in Listbox
Edgemeal:
Thanks for your advise. Your code runs well, but i just discover a problems. I actually working on a program that will write a SCPI command to a multimeter and read the measurement back to VB listbox.
Your code is convenient to use because I can just copy and paste directly from excel.
Code:
Write_Text "R1", 1000, 2200
Write_Text "R12", 1000, 2200
:
:
Write_Text "C1", 100, 220
But your code only stops the program from reading measurement from multimeter. But did not stop the multimeter from measuring process.
I really appreciate your help, at least i understand a bit about how to stop the VB program from executing.
FireXtol:
Code:
1. Sub/Function WhatHaveYou()
2. Static bolInProc as Boolean
3. If bolInProc Then Exit Sub/Function Else bolInProc = True
4.
5. Write_Text "R1", 1000, 2200
6. DoEvents: If BolStop Then bolInProc = False: Exit Sub/Function
7. Write_Text "C2", 1100, 2000
8. DoEvents: If BolStop Then bolInProc = False: Exit Sub/Function
9. Write_Text "V3", 500, 600
10. DoEvents: If BolStop Then bolInProc = False: Exit Sub/Function
11. '...
12. bolInProc = False
13. End Sub/Function
Your code stops exit the sub when stop_button is click. It also stop the multimeter from its measuring process as well.
But Sub Write_Text will be repeat more than 100 times. So its there any way to implement your code into Sub Write_Text and not Sub Run_Button_Click?
Thanks,
Regards
-
Jun 16th, 2010, 10:44 PM
#9
Re: Help in Listbox
 Originally Posted by kent5244
But Sub Write_Text will be repeat more than 100 times. So its there any way to implement your code into Sub Write_Text and not Sub Run_Button_Click?
Thanks,
Regards
Not really, it'd be essentially the same. The Run_Button_Click event would be re-entered regardless of what you do in Write_Text, short of calling End, which would terminate the application.
You could put the Write_Text code in the Run_Button_Click, or vice versa... perhaps with GoSub and Return....
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6( ).
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
-
Jun 17th, 2010, 12:46 AM
#10
Thread Starter
Member
Re: Help in Listbox
FireXtol:
Thanks again for your help. Another question arises.
Is there any way to continue to executing the code after i click the stop button.
Code:
Write_Text "R1", 1000, 2200
Write_Text "C2", 1100, 2000
Write_Text "V3", 500, 600
:
:
Write_Text "R100", 1200, 1500
e.g. I click stop button when executing Write_Text "C2", 1100, 2000 , so now Write_Text "V3", 500, 600 will not be execute.
So when i click a button called continue, I will able to run Write_Text "V3", 500, 600 and the rest of code.
Is it possible to do that?
I having trouble to rate your awesome post. A message pops stated "You must spread some reputation around before giving it to FirexTol."
-
Jun 17th, 2010, 01:03 AM
#11
Re: Help in Listbox
An ugly solution would be to use a big ol Select Case statement, and using GoTo LineNumber/LineLabel.
vb Code:
Static lResumeOnLine as Long
If lResumeOnLine > 0 then
Select Case lResumeOnLine
Case 2
GoTo 2
Case 3
GoTo 3
'etc...
End Select
End If
lResumeOnLine = 2
2 Write_Text "R1", 1000, 2200
lResumeOnLine = 3
3 Write_Text "C2", 1100, 2000
lResumeOnLine = 4
4 Write_Text "V3", 500, 600
'...
lResumeOnLine = 0
AFAIK, a variable can't be used as an argument for GoTo. It'd assume it's a line label.
A loop would really make things simpler. If you use line numbering for Erl, I'd suggest line labels.
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6( ).
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|