Problem with creating Multiple instances of Form in VB!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Problem with creating Multiple instances of Form in VB!

Hybrid View

  1. #1
    Join Date
    Jun 2006
    Posts
    29

    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!

    Eagerly waiting for your reply ...

    Thanks & Regards,

    Ruturaj.
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2004
    Posts
    717
    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)...
    Last edited by Sync; 06-20-2006 at 05:57 AM.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  3. #3
    Join Date
    Jun 2006
    Posts
    29
    Quote Originally Posted by Sync
    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)...
    So, is there any work-around for this issue?

  4. #4
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    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

  5. #5
    Join Date
    Jun 2006
    Posts
    29
    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.

  6. #6
    Join Date
    Dec 2004
    Posts
    717
    So, is there any work-around for this issue?
    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.. :-)
    Last edited by Sync; 01-21-2007 at 05:34 AM.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  7. #7
    Join Date
    Dec 2004
    Posts
    717
    Have you checked the zip file?
    Is it what you want?
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  8. #8
    Join Date
    Jun 2006
    Posts
    29
    Quote Originally Posted by Sync
    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.

    Regards,

    Ruturaj.
    Attached Files Attached Files

  9. #9
    Join Date
    Jun 2006
    Posts
    29
    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).

  10. #10
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    you forgot to include form3.frx, but I loaded the form putting a fake label by hand.
    it works like a charm, good job

    when do you get a 100% cpu? I mean, after which action?

    I saw a possible problem. You change the ownwer window using this in the form load:

    SetWindowLong Me.hwnd, GWL_HWNDPARENT, 0

    but you forgot to reset the original in the unload event.
    See an example on how to use GWL_HWNDPARENT here:
    http://www.devx.com/vb2themax/Tip/18318
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  11. #11
    Join Date
    Jun 2006
    Posts
    29
    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.

  12. #12
    Join Date
    Jun 2006
    Posts
    29
    Hello!

    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!

    Ruturaj.

  13. #13
    Join Date
    Dec 2004
    Posts
    717
    Good Job! vb-programmer. :-)
    one small thing of ur program: Form3 is always on top even unchecked the checkedbox.

    as "Create frmNote instances from ActiveX EXE."
    Ya. but i think it's not fixed idea for you.

    This is very important to disable ShowInTaskbar for each instance of Form!
    If it's very important, dont use " SetWindowLong Me.hwnd, GWL_HWNDPARENT, 0" :-)
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  14. #14
    Join Date
    Jun 2006
    Posts
    29
    Quote Originally Posted by Sync
    If it's very important, dont use " SetWindowLong Me.hwnd, GWL_HWNDPARENT, 0" :-)
    LOL! Can't we combine the two ... continue use of API whilst have ShowInTaskbar = False?

  15. #15
    Join Date
    Dec 2004
    Posts
    717
    Actually, I think it's possible..
    Here is some facts that I tested ..

    Form1 - btn
    Code:
    Private Sub btn_Click()
        Form2.Show
        SetWindowLong Form2.hWnd, GWL_HWNDPARENT, 0
    End Sub
    Form2 - Form_Load
    Code:
    Private Sub Form_Load()
      SetWindowLong Me.hWnd, GWL_EXSTYLE, (GetWindowLong(hWnd, _
            GWL_EXSTYLE) And Not WS_EX_APPWINDOW)
    End Sub
    Then. Run this program.. (Im tested this code above with the program that vb-programmer uploaded.)

    Form2 will be shown without Parent Form and without showing in TaskBar. (unless you click the form2. ** )
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

Similar Threads

  1. Windows form designer generated code VB
    By Benjamin in forum .NET
    Replies: 2
    Last Post: 04-05-2006, 04:06 AM
  2. Hiding dialup window called from a VB form
    By Jerald in forum VB Classic
    Replies: 1
    Last Post: 02-09-2002, 10:05 AM
  3. They created J#, why couldn't they do VB#?
    By Thomas Eyde in forum .NET
    Replies: 290
    Last Post: 12-22-2001, 03:13 PM
  4. Replies: 10
    Last Post: 09-06-2001, 07:42 PM
  5. Open Access database Form from VB 6.0?
    By Demo in forum VB Classic
    Replies: 7
    Last Post: 07-25-2000, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center