473,786 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Instances of form in VB .Net

I want to open a form if it is not already open, otherwise I want to set
focus to the existing instance. I have tried three solutions from Google to
check activate the existing instance but none seem to work (At least I cannot
get them to work...probably me). Can anybody help please?
Terry

1: http://www.developerfusion.co.uk/show/3107/
2: http://devhood.com/tutorials/tutoria...utorial_id=726 (This
seemed the most likely but ....???)
The other I discounted early.

The code from 2: is as follows

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

If
UBound(Diagnost ics.Process.Get ProcessesByName (Diagnostics.Pr ocess.GetCurren tProcess.Proces sName)) > 0 Then
'Send opening form's TEXT property as a parameter to the function
"ActivatePrevIn stance"
ActivatePrevIns tance(TEXT_OF_O PENING_FORM)
End If
End Sub

--------------------------------------------------------------------------

--Add these following declarations in the form code or in a COMMON module if
you have one.

''Declarations of Windows API functions
Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SetForegroundWi ndow Lib "user32" (ByVal hwnd As Long) As Long

Sub ActivatePrevIns tance(ByVal argStrAppToFind As String)
Dim PrevHndl As Long
Dim result As Long

'Variable to hold individual Process
Dim objProcess As New Process
'Collection of all the Processes running on local machine
Dim objProcesses() As Process
''Get all processes into the collection
objProcesses = Process.GetProc esses()

For Each objProcess In objProcesses
''Check and exit if we have SMS running already
If UCase(objProces s.MainWindowTit le) = UCase(argStrApp ToFind) Then
MsgBox("Another instance of " & argStrAppToFind & " is already running on
this machine. You cannot run TWO instances at a time. Please use the other
instance.")
PrevHndl = objProcess.Main WindowHandle.To Int32()
Exit For
End If
Next
If PrevHndl = 0 Then Exit Sub 'if No previous instance found exit the
application.
''If found
result = OpenIcon(PrevHn dl) 'Restore the program.
result = SetForegroundWi ndow(PrevHndl) 'Activate the application.

End 'End the current instance of the application.
End Sub

Dec 8 '05 #1
0 1350

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2698
by: Job Lot | last post by:
How can I prevent multiple instances of MdiChild form? I have a MdiParent form with a DataGrid showing all the clients in the database. User clicks on a row to open MdiChild form which display details of the client. User can have multiple clients open at the same time, but how can I prevent them from opening multiple instances of the client?
12
3238
by: (Pete Cresswell) | last post by:
I know I can open many instances of a given form, but I've never done it. Now I'm analyzing an application where that seems like just the ticket: Many investment funds, *lots* of data points for each fund, and a desire by the users to see several funds presented side-by-side. Is opening, say, five instances of the same form real-world-doable? -- PeteCresswell
2
3122
by: DraguVaso | last post by:
Hi, I found some examples for storing the FormSettings of a Form in an XML-file, but none of these could match my criteria: What I am looking for is the possibility to save the FormSettings of multiple Instances of 1 form. I have a Form frmSource from which I have multiple Instances, each with a unique Identifier. So when I Load or Close a specific instance, I want to load or save the Settings (Top, Height, Width, etc) in an XML-file....
11
21005
by: Clark Stevens | last post by:
I just finished a WinForms app in VB.NET. I want to allow the user to be able to run multiple instances of the program like you can with Notepad and Wordpad. The way it is now, once I run the executable I can not run another instance as long as the first instance is running. How can I change this behavior? Thanks.
2
1665
by: Helen Trim | last post by:
I have an application with three forms that are msde visible and activated when needed. It uses Word to open documents and one of the forms is opened as the Word document is closed in the DocumentBeforeClose event. It works the first time but when it makes the form visible for the second time, it freezes. Sometimes it gives an out of memory error, but I think that may be a red herring. Any ideas? Has anyone come across similar...
4
3293
by: GGerard | last post by:
Hello I have a program where the user can open as many instances of a form as the user wants. The only limit to how many instances can be opened is determined by the limit of the computer itself (the amount of RAM - I think). When enough instances of this form are opened and the limit of the computer is reached
6
2659
by: Bob Alston | last post by:
Looking for someone with experience building apps with multiple instances of forms open. I am building an app for a nonprofit organizations case workers. They provide services to the elderly. so far I have built a traditional app, switchboard, forms, etc. Part of this app is to automate the forms they previously prepared manually. After the app was built and works just fine, I find out there are several case managers using MS word...
6
1823
by: Savante | last post by:
I have been writing a datalogging application. It reads in double's into a database. I want to be able to click on a row in a database (holds name of variable and also current value of variable) and then instantiate a new form. I created a new form form2 and then just on double click on a button.... dim form as new form2 form.show
5
3311
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening the forms all at once, I'm opening them as needed. I have a main form with multiple records; and then I have a pop-up form that the user opens with button. The pop-up form contains one record relating to the current record in the main form (but...
4
2691
by: nottarealaddress | last post by:
I'm trying to get my feet wet in VB2005 (our new standard at work after officially stopping new development in VB6 about a month ago). I'm working with a simple sql 2005 table of 50 entries, one for each state. Each entry contains Name, postal abbreviation, etc. Just simple stuff to understand the mechanisms, syntax, etc. I'm now to the point where I've got a Master MDI form that opens one or more types of forms, I'm able to edit/save...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.