Results 1 to 5 of 5

Thread: Testing for a command button click on a different form

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    2

    Testing for a command button click on a different form

    Hi, New to the Forum. Used to code many years ago and am just trying to knock up a program and am in trouble. Any suggestions would be appreciated. BTW VB6 NOT Net
    Trying to keep it simple
    I am looping through comparing the contents of two arrays in a Subroutine using a particular condition that should be met. When that condition is not met I pass the specific contents of each array to a function asking for user input . That function calls a subroutine that loads up a new form containing the specific details of each array and asks the user to choose the desired outcome using option (radio) buttons and clicking on a command button. I need to get what option button is selected back to the original calling subroutine (via the function?).
    Because I am in a loop (in the function) waiting for the command button on the new form to be clicked even the command_click() on the new form does not seem to work.

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Testing for a command button click on a different form

    If your user input form is called frmInput, having an array of radio controls name Option1
    Code:
    Private m_iOptionIndex As Integer
    
    Public Property Get OptionIndex() As Integer
      OptionIndex = m_iOptionIndex
    End Property
    
    Private Sub Option1_Click(Index As Integer)
     m_iOptionIndex = Index
    End Sub
    Some air code of the inside of the loop:
    Code:
    Dim iOptionIndex As Integer 
    
    Set fInput = New frmInput
    fInput.Show vbModal
    iOptionIndex= fInput.OptionIndex
    Unload fInput

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,855

    Re: Testing for a command button click on a different form

    I think I need to see some sample code to truly understand the issue. However, at first glance, this looks like a situation where vbModal would be useful:

    Code:
    
        Form2.Show vbModal
    
    If I understand correctly from the description given, we just need to understand what's going on with the program's thread. When you show a form in a loop but it's not shown modally, you are not giving up the thread. Sure, code will go execute that will show this second form. However, you will never turn control over to Windows so that any events could be processed on that second form. I believe that's your problem.

    If you show that second form modally, your loop will pause where it's modally shown (very similar to calling a function and waiting on it to return). Once you reply to that secondary form and close it (or make it invisible), your loop will then regain the thread and continue executing.

    There are other ways this could be done, but I believe a simple use of vbModal will solve the problem.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Testing for a command button click on a different form

    As already mentioned, showing the user input form modally is best practice when you want your code to wait for user input from within another form. No looping needed. Modal halts your calling form until the user input form is either unloaded or hidden.

    As for which option button is clicked, you didn't mention whether those are in a control array or individually named.

    If individually named, you pretty much need to test each one for a "True" value.
    Code:
    Select Case True
        Case Option1
            ' what to do if this selected
        Case Option2
            ' what to do if this selected
        Case Option3
            ' what to do if this selected
        ' Case ...
    End Select
    If arrayed, then Arnoutdv gave one solution and here's another:
    Code:
    For n = Option1.LBound To Option1.UBound
        If Option1(n).Value = True Then
            ' what to do if this selected
            Exit For
        End If
    Next
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    2

    Re: Testing for a command button click on a different forVolpe

    [RESOLVED]
    Thanks heaps to LaVolpe. Elroy and Arnoutdv for their prompt replies.
    This problem had been bugging me for the last few days and typically about 5 hours after posting on the forum I got it to work. I tried to cancel the post, but as it was not up yet I was unsure how to proceed. This forum is fantastic and I sincerely want to thank those that spent their valuable time in trying to help.
    As I said, I have been out of the game for many years and have just about forgotten most of the good stuff. I got it to work without vbModal., and to be totally honest I am still unsure., (not had time to chase it up), what vbModal does. It is still 4am here and I will look it up later.
    My primary problem was DoEvents and my lack of using it. I can provide more information if anyone wants to know, but I really want to post this so more people do not spend their time on my stupidity.
    Regards
    Rick

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