Results 1 to 11 of 11

Thread: [RESOLVED] Help in Listbox

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    32

    Resolved [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

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    32

    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


    Quote Originally Posted by Edgemeal View Post

    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.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    32

    Re: Help in Listbox

    Quote Originally Posted by Hack View Post
    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

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    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.

  7. #7
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    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:
    1. Write_Text "R1", 1000, 2200
    2. DoEvents: If BolStop Then Exit Sub/Function
    3. Write_Text "C2", 1100, 2000
    4. DoEvents: If BolStop Then Exit Sub/Function
    5. Write_Text "V3", 500, 600
    6. DoEvents: If BolStop Then Exit Sub/Function
    7. '...

    I'd also suggest a static variable, which will guard against re-entry. Which'll look something like:

    vb 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

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    32

    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

  9. #9
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Help in Listbox

    Quote Originally Posted by kent5244 View Post
    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....

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2010
    Posts
    32

    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."

  11. #11
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Help in Listbox

    An ugly solution would be to use a big ol Select Case statement, and using GoTo LineNumber/LineLabel.

    vb Code:
    1. Static lResumeOnLine as Long
    2. If lResumeOnLine > 0 then
    3.   Select Case lResumeOnLine
    4.     Case 2
    5.       GoTo 2
    6.     Case 3
    7.       GoTo 3
    8.     'etc...
    9.   End Select
    10. End If
    11.   lResumeOnLine = 2
    12. 2 Write_Text "R1", 1000, 2200
    13.   lResumeOnLine = 3
    14. 3 Write_Text "C2", 1100, 2000
    15.   lResumeOnLine = 4
    16. 4 Write_Text "V3", 500, 600
    17. '...
    18. 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width