AllExperts > Visual Basic 
Search      
Visual Basic
Volunteer
Answers to thousands of questions
 Home · More Visual Basic Questions · Answer Library  · Encyclopedia ·
More Visual Basic Answers
Question Library

Ask a question about Visual Basic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Golfnut_1969
Expertise

Instantiating form variables instead of using Global instances.
User Defined Types
DLLs, ActiveX Exes
String Manipulation
Working with Classes
Custom User Controls
Object Oriented Programming
Database access (SQL Server, MS Access, RDO, DAO, ADO)
Use of Arrays

I do not answer questions regarding databound controls so please ask those questions of one of the other experts.

Experience
I've been working with Visual Basic 5.0 and 6.0 for over 8 years. I've worked extensively with multi-tier database business solutions created in Visual Basic. I've also been working in Visual Basic .NET for over 2 years and Visual Basic .NET 2.0 since its first BETA release. I currently work developing custom applications for a variety of industries. In addition to all the work in Visual Basic 6.0 and .NET I have also worked extensively with ASP .NET and ASP .NET 2.0.

Education/Credentials
Associate’s Degree: Computer Programming St. Louis Community College at Florissant Valley (4.0 gpa) Microsoft Certified Technology Specialist (MCTS) .NET Framework 2.0 Windows Applications

Past/Present clients
Mallinckrodt Pharmaceuticals Commerce Bank CitiMortgage Elmwood Marine Services FedEx Memco Barge Lines Crounse Corporation AG Edwards

 
   

You are here:  Experts > Computing/Technology > Basic > Visual Basic > How to allow control to form while running the VB Code

Visual Basic - How to allow control to form while running the VB Code


Expert: Golfnut_1969 - 4/13/2007

Question
QUESTION: Hi,

I am new to VB Programming. I have written one application in VB that searches in  the EXCEL and generates the results.My problem is that when the code is running i am not able to click on any other buttons provided on the form not even the minimize button also. I need to mininize the form when the search is in progress and also i have provided some buttons to stop the search in the middle. how can i gain access to these buttons when the form is running?
ANSWER: When your application is running a process any control events (Button Clicks, etc..) are not raised, rather they are queued and wait until the process is completed.  However, there is a method called DoEvents that you can call that will tell your application to pause in the process and check for any queued events and raise them if they exist. You would use it like this.
Create a new project with a single form and add 2 command buttons named Command1 and Command2. Add the following code.

'This is a Windows API that tells the application to pause for a number of milliseconds. We will use it to slow down our loop.

Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private mnCount As Integer


Private Sub Form_Load()
   Command1.Caption = "Start"
   Command2.Caption = "Stop"
   Command2.Enabled = False
End Sub

Private Sub Command1_Click()
   mnCount = 60
   Command1.Enabled = False
   Command2.Enabled = True
   
   Do While mnCount > 0
       mnCount = mnCount - 1
       Command1.Caption = mnCount
       Me.Refresh
       Sleep 1000
       DoEvents 'This checks for queued events
   Loop
End Sub

Private Sub Command2_Click()
   Command1.Caption = "Start"
   Command1.Enabled = True
   Command2.Enabled = False
   mnCount = 0 'This will stop our loop
End Sub



---------- FOLLOW-UP ----------

QUESTION: Thanks for the response.. I have implemented the code but i have one more problem.When the search is in the middle if i click on stop it should ask me for Yes or No and based on the option that i select the program should continue.. i.e when i say no then the program should resume the execution from the place where i had stopped. how can i acheive this...

Please help me

Answer
For the sample I gave in the previous answer, change the code in the Command2 click event as follows.

Private Sub Command2_Click()
   Dim enmMsgBxRslt As VbMsgBoxResult
   Dim sMsg As String
   
   sMsg = "Do you want to stop processing?"
   
   enmMsgBxRslt = MsgBox(sMsg, vbYesNo + vbQuestion, "Stop Execution")
   
   If enmMsgBxRslt = vbYes Then
       'Only stop processing if the user clicked the Yes Button
       Command1.Caption = "Start"
       Command1.Enabled = True
       Command2.Enabled = False
       mnCount = 0 'This will stop our loop
   End If
End Sub


Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.