Results 1 to 17 of 17

Thread: [RESOLVED] SetParent and vbModal...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Resolved [RESOLVED] SetParent and vbModal...

    Has anyone ever solved the problems with Using SetParent and putting the forms within a Modal form?

  2. #2

  3. #3

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: SetParent and vbModal...

    Ok, I'll try to explain it clearly!

    My parent form is Modal. I have two other forms that I use SetParent to display them with in the Modal Form via two Frames.

    One on the forms has text boxes on it, when you click on the textbox, the form kind of blinks, but the textbox doesn't keep the focus...

    Focus seems to return to the parent form...

    If I don't display the parent form modal, all works fine..

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: SetParent and vbModal...

    Attached is a simple example of what I am talking about....
    Attached Files Attached Files

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: SetParent and vbModal...

    Well I don't know if this helps or not but you don't need form4 (at least in the sample project). Change your project's properties so that the Startup Object is Sub Main and then put this in Module1.

    VB Code:
    1. Public Sub Main()
    2.   Load Form1
    3.  
    4.   Call SetParent(Form2.hWnd, Form1.Frame1.hWnd)
    5.   Call SetParent(Form3.hWnd, Form1.Frame2.hWnd)
    6.  
    7.   Form2.Move 0, 0, Form1.Frame1.Width, Form1.Frame1.Height
    8.   Form3.Move 0, 0, Form1.Frame2.Width, Form1.Frame2.Height
    9.   Form2.Show
    10.   Form3.Show
    11.  
    12. End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: SetParent and vbModal...

    This is just a little sample to show what is happening.. Something I just threw together..

    The application that I am dealing with is humongous...
    So I wanted to make sure it wasn't something else going on in it.....

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: SetParent and vbModal...

    Okay, my "solution" should have been

    VB Code:
    1. Public Sub Main()
    2.  
    3.   Call SetParent(Form2.hWnd, Form1.Frame1.hWnd)
    4.   Call SetParent(Form3.hWnd, Form1.Frame2.hWnd)
    5.  
    6.   Form2.Move 0, 0, Form1.Frame1.Width, Form1.Frame1.Height
    7.   Form3.Move 0, 0, Form1.Frame2.Width, Form1.Frame2.Height
    8.   Form2.Show
    9.   Form3.Show
    10.   [HL="#FFFF80"]Form1.Show[/HL]
    11.  
    12. End Sub
    Are you saying that you want to set Form1 to vbModal and yet be able to enter data into Form2 and/or Form3? If so then that's not possible but you could fake it by setting the Enabled property to False temporarily on all the forms you don't want people to be able to access.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: SetParent and vbModal...

    Yes, Form1 does need to be modal.

    The program was using a Sheridan control to do this (SSSplitter), but I wanted to get rid of the SSSplitter since it was causing some other issues.

    One thing the SSSplitter did was allowed the form to be modal with forms in each of its panes...

    Microsoft mentions that you should do something with WM_CHANGEUISTATE/WM_UPDATEUISTATE when you use SetParent, so I was going to look into that as well....

    Just didn't know if anyone else had found a way around this other than disabling/enabling the other forms....

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: SetParent and vbModal...

    The following seems to fix the problems with a Modal form and the SetParent API Call (at least in this project)....

    My humongous project still doesn't work, so I am trying to figure out what would make the below or attached code break.....

    VB Code:
    1. Public Const GWL_WNDPROC = (-4)
    2. Public Const GWL_HINSTANCE = (-6)
    3. Public Const GWL_HWNDPARENT = (-8)
    4. Public Const GWL_ID = (-12)
    5. Public Const GWL_STYLE = (-16)
    6. Public Const GWL_EXSTYLE = (-20)
    7. Public Const GWL_USERDATA = (-21)
    8.  
    9. Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    10. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    11.  
    12. Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    13.  
    14. Public Sub NewAttachSubForm(ByVal NewParent As Frame, ByVal TheForm As Form)
    15.  
    16.   Debug.Print SetWindowLong(TheForm.hwnd, GWL_STYLE, GetWindowLong(TheForm.hwnd, GWL_STYLE) Or WS_CHILD)
    17.   Call SetParent(TheForm.hwnd, NewParent.hwnd)
    18.   TheForm.Move 0, 0, NewParent.Width, NewParent.Height
    19.   TheForm.Visible = True
    20.  
    21. End Sub
    22.  
    23. '''''' In Form1.....
    24. Private Sub Form_Load()
    25.  
    26.   Call NewAttachSubForm(Form1.Frame1, Form2)
    27.   Call NewAttachSubForm(Form1.Frame2, Form3)
    28.    
    29.   Form2.Move 0, 0, Form1.Frame1.Width, Form1.Frame1.Height
    30.   Form3.Move 0, 0, Form1.Frame2.Width, Form1.Frame2.Height
    31.   Form2.Show
    32.   Form3.Show
    33.   Form4.Show
    34.   Form4.Move 0, 0
    35.   Me.Show vbModal
    36.  
    37. End Sub
    38.  
    39.  
    40. Private Sub Form_Resize()
    41.  
    42.   Me.Frame1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight \ 2
    43.   Me.Frame2.Move 0, Me.Frame1.Top + Me.Frame1.Height, Me.ScaleWidth, Me.ScaleHeight \ 2
    44.  
    45.   Form2.Move 0, 0, Form1.Frame1.Width, Form1.Frame1.Height
    46.   Form3.Move 0, 0, Form1.Frame2.Width, Form1.Frame2.Height
    47.  
    48. End Sub
    Attached Files Attached Files

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: SetParent and vbModal...

    This fixes the issue of Form1 appearing to lose focus (Caption color changing) as well...

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: SetParent and vbModal...

    Forgot that in my code...... dumb, dumb, dumb..... Just noticed that someone forgot the Option Explicit in the module!

    VB Code:
    1. Public Const WS_CHILD = &H40000000

    So now SetParent even works with Modal Forms... Also makes it appear the the Parent Form Keeps Focus!!!!

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: [RESOLVED] SetParent and vbModal...

    Now, another problem with SetParent!

    It seems that if I do:

    VB Code:
    1. Call SetWindowLong(MyForm.hwnd, GWL_STYLE, GetWindowLong(MyForm.hwnd, GWL_STYLE) Or WS_CHILD)

    If my form loses focus and then gets focus back, the Up/Down Arrows and PageUp/Down Keys won't work on the Datagrid...

    Any ideas???

    I attached a stupid little test program that I have been messing with trying to fix this new problem....
    Attached Files Attached Files

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: [RESOLVED] SetParent and vbModal...

    I haven't tried your new sample project but before I do let me ask why do you use forms in frames? Why not put whatever controls you need in the frames themselves?

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: [RESOLVED] SetParent and vbModal...

    Wasn't exactly my choice on how to do it, but it is a very large VB 6.0 project.

    What they did was create some common forms that other forms need to use regularly.

    Then on the forms that need the common forms, they used Sheridans SSSplitter Control to bring the Common forms in.

    I found the problem with the datagrids, and it seemed to be the Splitters fault, so I changed from the Splitter to Frames and the SetParent API.

    This fixed the datagrid problem until I did the SetWindowLong API call, then it breaks just like the Splitter (which must be doing the same thing).... The SetWindowLong needs to be done, otherwise the Parent Form loses focus when the child form is entered.... (also major problems if the form is displayed modally.....)

    Hopefully that kind of explained it!

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: [RESOLVED] SetParent and vbModal...

    This problem doesn't seem to happen with the Mircorsoft Flexgrid....

    Can the Flexgrid be dropped in in place of a DataGrid?

    Are there any known problems with the Flexgrid?

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2004
    Posts
    342

    Re: [RESOLVED] SetParent and vbModal...

    Don't know if anyone cares, but it also happens with the FlexGrid, but I did find a workaround for it, I added a text box to the screen with the DataGrid and simply put it behind the DataGrid, then do the following:

    VB Code:
    1. Private Sub DataGrid1_Click()
    2.  
    3.   me.txtDataGridFocusOnly.SetFocus
    4.   Me.DataGrid1.SetFocus
    5.  
    6. End Sub

    Seems like I'm the only one that ran into the problem, since no one else seemed to know about it! But now it is really resolved..............

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