Problem with creating Multiple instances of Form in VB!
Hello!
In my currently under-development application, I need to create multiple instances of a Form with no title bar (ControlBox = False). I do it this way ...
Private oForm as frmNote
Public Sub CreateFormInstance()
Set oForm = new frmNote
oForm.Show
End Sub
This code works perfect and creates the multiple instances of Form called frmNote. However, I'm facing one very interesting problem with it! When a new instance of frmNote is created and it's hidden behind any already open Application window, whenever I click frmMain (the main form which has a button to create instances of frmNote) Title bar, the instance of frmNote, which was behind a certain application window comes in front of the Application window automatically! Why this happens so? I don't want this to happen. Any work-around for this problem?
To put my problem in a better way, I have created a sample VB Project, which has similar functionality coded. I request you to please download the attached ZIP file and run it from your IDE. All the necessary information to mimic my problem situation is written on the Form. I don't understand why this problem occur as I never faced such issue before!
Ya. it'll happen. cuz' they are in same process thread...
As the same way, you can put form1 behind the other app (i.e: notepad). Then, If you click the Form2, form1 will be automatically shown in front of the other application (ie: notepad)...
Ya. it'll happen. cuz' they are in same process thread...
As the same way, you can put form1 behind the other app (i.e: notepad). Then, If you click the Form2, form1 will be automatically shown in front of the other application (ie: notepad)...
Why do you want to change the standard Windows behaviour? When an application becames the forregraound app, all its windows are shown on top (except for the Task Manager if it is set to be always on top) Ans it make sense, because otherwise how you can pop them if they are hidden below other windows?
Anyway, you can try the SetWindowPos API, using the HWND_BOTTOM parameter. Probably you have to call it for each loaded forms in the Activate event. But I am not sure it will work
Marco
"There are two ways to write error-free programs. Only the third one works."
Unknown
Thanks Marco for your reply. The application is Sticky Notes application and there is already a provision of making a form on top. However, if this property is removed then it means user does not want to see the interface on top of already open Application window. The same issue is raised by the client (for whom) I'm developing this application. So, I need to look at it and implement it, if possible. I tried and faied to achieve it; hence thought I should ask you all. I will try SetWindowsPos with _BOTTOM; currently I'm using the same API for setting window on top; so I will be able to try it readily! Once again thanks for your reply and support ... I will get back to you with result.
Yeah.. Of course.. You should take a look at multithreading in VB6 or CreateWindowEx API. But Im wondering why you need that kinda things in your project. :O
I have created one small application as a sample in my blog....
Here is the link. http://michaelsync.net/2006/06/20/sa...ewindowex-api/
You can download the CreateWindows.zip from this page.
I think that this is what you want.. :-)
Have you checked the zip file?
Is it what you want?
Hello Sync;
I checked the ZIP file and that's certainly helped me to understand API based window creation. The concept works perfect; however it will be too much of work for me to use that method.
The frmNote is the form which allows user to edit new sticky note and then save it to database. There is RichEdit control, a toolbar for text formatting and two buttons along with a footer which displays number of letters consumed and number of letters remaining for the note. If I go for creation of Window with API, then I will also need to create all these components at runtime. Surely, it's possible but implementing this stuff in a project which has already moved ahead a lot with lot of XML HTTP coding which is accessed from the frmNote instance to post the note text to Server for online viewing and mobile phone viewing purpose!
Instead, I have derived another way to achieve it. I have attached my updated sample program which now has this method implemented. I use following API ...
Code:
Private Const GWL_HWNDPARENT = (-8)
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
I call this API on Form Load method. The only short-fall of this API is you can't change any window-layer based property of window unless you use SetWindowPos API. Fortunately I was using SetWindowPos API for setting the Window on Top feature; so I'm yet to find any bug in using this API. However, I found sometimes my CPU goes to 100%; I don;t know if this is due the API I'm using or some other application on my current machine is doing the trick! I've not tested this method on virtual PC, so I'm not sure if this one works 100% stable ... but one thing is sure, this one works!
I really have no words for your devoted supportive nature and your involvement in my issue to see it's solved! Please see the API implementation and let me know your opinion on it.
I also got another idea from other Forum as "Create frmNote instances from ActiveX EXE." This also sounds logical because ActiveX Exe is a out-of-process component and hence the form instances created by ActiveX Exe component will run in its own process. So, it will indepedent of main Application process. But I'm not sure how much useful this option will be for me; because there is a lot of data exchange between Application's main form which is minimized to System Tray and the frmNote Instances. It can be minimized easily, though, by moving frmMain code to Sub and Functions in class and having centralized data exchange with oNoteForm as Form as a param to Sub or Function itself. This way, no matter which instance is accessing the code block, it will pass itself as instance to function as ByRef and hence I will get total access to the different controls placed on the frmNote form. But as I said, I've not tested it and it's all just theory. So, I'm not sure how much it will be useful for me; just exchanging the idea with you (and all forum members who are interested in this discussion).
Thanks. I will look at it. It never happens always; can't tell you the exact steps to reproduce CPU 100% problem! I think you are right. SetWindowLong should be handled in Unload event of form instance as well.
I just noticed another issue with the SetWindowLong API ... when I use this API, though the ShowInTaskbar is set to False for the Form2, the instance with the use of SetWindowLong API does show in Taskbar. SetWindowLong is not borrowing the property ShowInTaskbar. Any solution for this issue? This is very import to disable ShowInTaskbar for each instance of Form!