Solved

MDI Child form resize controls Visual Basic 6

Posted on 2011-09-27
15
1,632 Views
Last Modified: 2012-05-12
In VB6, I have a MDI Child form and when I test this functionality on a non MDI Child form it works like a charm. But when I change the form back to MDIChild=true it totally cr@ps out. Im trying to resize a form and all the controls within the form and it works really well but once I make the form MDIChild its resizing is all screwed up. I have 2 calls from my form to a module with 2 Subs. Pretty simple right?. Could someone please tell me whats wrong.
And please no suggestions to go to VB.NET with all the advanced Docking features. I added VB.NET zone because most were VB programmers at one point. Also, Im pretty sure I could do this with panels but I want to see if I could do this first. THANKS!!
'Form Level code
Private Sub Form_Resize()
    ResizeControls Me
End Sub
Private Sub Form_Load()
    SaveSizes Me
End Sub

'Module level code
Private Type ControlPositionType
    Left As Single
    Top As Single
    Width As Single
    Height As Single
    FontSize As Single
End Type

Private m_ControlPositions() As ControlPositionType
Private m_FormWid As Single
Private m_FormHgt As Single

' Arrange the controls for the new size.
Public Sub ResizeControls(frm As Form)
Dim i As Integer
Dim ctl As Control
Dim x_scale As Single
Dim y_scale As Single

    ' Don't bother if we are minimized.
    If WindowState = vbMinimized Then Exit Sub

    ' Get the form's current scale factors.
    'If m_FormWid <> 0 And m_FormHgt <> 0 Then
    x_scale = frm.ScaleWidth / m_FormWid
    y_scale = frm.ScaleHeight / m_FormHgt
    'End If
    ' Position the controls.
    i = 1
    For Each ctl In frm.Controls
        With m_ControlPositions(i)
            If TypeOf ctl Is Line Then
                ctl.x1 = x_scale * .Left
                ctl.y1 = y_scale * .Top
                ctl.x2 = ctl.x1 + x_scale * .Width
                ctl.y2 = ctl.y1 + y_scale * .Height
            Else
                ctl.Left = x_scale * .Left
                ctl.Top = y_scale * .Top
                ctl.Width = x_scale * .Width
                If Not (TypeOf ctl Is ComboBox) Then
                    ' Cannot change height of ComboBoxes.
                    ctl.Height = y_scale * .Height
                End If
                On Error Resume Next
                ctl.Font.size = y_scale * .FontSize
                On Error GoTo 0
            End If
        End With
        i = i + 1
    Next ctl
End Sub

' Save the form's and controls' dimensions.
Public Sub SaveSizes(frm As Form)
Dim i As Integer
Dim ctl As Control

    ' Save the controls' positions and sizes.
    ReDim m_ControlPositions(1 To frm.Controls.Count)
    i = 1
    For Each ctl In frm.Controls
        With m_ControlPositions(i)
            If TypeOf ctl Is Line Then
                .Left = ctl.x1
                .Top = ctl.y1
                .Width = ctl.x2 - ctl.x1
                .Height = ctl.y2 - ctl.y1
            Else
                .Left = ctl.Left
                .Top = ctl.Top
                .Width = ctl.Width
                .Height = ctl.Height
                On Error Resume Next
                .FontSize = ctl.Font.size
                On Error GoTo 0
            End If
        End With
        i = i + 1
    Next ctl

    ' Save the form's size.
    m_FormWid = frm.ScaleWidth
    m_FormHgt = frm.ScaleHeight
End Sub

Open in new window

0
Comment
Question by:johnnyaction
  • 7
  • 5
  • 2
15 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 36716916
@johnnyaction

Are you trying to change the type to/from MDIchild during your application's execution?
0
 
LVL 1

Author Comment

by:johnnyaction
ID: 36717567
no Im just trying to make this code work for an MDI Child. I created a test form that was NOT an MDI Child and it worked. Then I went to apply code to the form I wanted this to work for and it didnt work, then I noticed it was an MDI Child so that seems to be causing all my problems?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 36717714
This may be a scaling issue.  Check the form's scalemode property when it is and isn't an MDIchild.

This may also be caused by a change in the form that isn't reflected in the variables you use to store the form's size.  I only see these variables set in the Form_Load() event.  You might need to add this code to other events that fire when the form's dimensions change.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 1

Author Comment

by:johnnyaction
ID: 36718452
I will definitely check aikimark, I just cant do it now. I hope that is the case though. I will let you know. Thanks
0
 
LVL 1

Author Comment

by:johnnyaction
ID: 36816909
ok well the forms ScaleMode = 1(twips) and they are both the same in MDI and NON MDI modes? This is very strange. I think some of the properties that I am setting in the resizecontrols sub are getting values from the MDI form instead of the child. In the ResizeControls Sub I get the following values in both modes
    ' Get the form's current scale factors.
    x_scale = frm.ScaleWidth / m_FormWid  = ' NonMDI-14625      MDI-19770
    y_scale = frm.ScaleHeight / m_FormHgt  = ' NONMDI-9855       MDI-7655

Very strange
0
 
LVL 45

Expert Comment

by:aikimark
ID: 36817638
add a bit of logging in that section of code and record the values you are about to assign to the form dimensions.  do two runs with normal and MDIchild types.
0
 
LVL 1

Author Comment

by:johnnyaction
ID: 36817842
What were you wanting to know the values for? The ResizeControls Sub loops through each control in the form and the sizes are based on the x_scale and y_scale which is being set in the SaveSizes Sub:
    ' Save the form's size.
    m_FormWid = frm.ScaleWidth
    m_FormHgt = frm.ScaleHeight
If that is off Im thinking its screwing everything else up? Let me know your thoughts...Thanks
0
 
LVL 1

Author Comment

by:johnnyaction
ID: 36817861
Wow, I actually hardcoded the nonMDI values to the    
m_FormWid = frm.ScaleWidth
m_FormHgt = frm.ScaleHeight
and it seemed to work ok but the screen is so much wider than usual and it doesnt seem right still but at least we have narrowed this down somewhat...
0
 
LVL 45

Expert Comment

by:aikimark
ID: 36817977
I suspect that the actual width and height change between the setting of the m_FormWid and m_FormHgt variables
0
 
LVL 1

Author Comment

by:johnnyaction
ID: 36818101
I agree...What to do....what to do...
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 36818221
I think the easiest way would be to set a watch on the two variables and break when the value changes.  The same should be done for the form's height and width properties.
0
 
LVL 6

Expert Comment

by:judgeking
ID: 36911600
I've done lots of work with resizing MDI forms in VB6, you need to save the init size of the form (from design mode), then compare it when loading the form.  Change the form code to the code below and it will work, just plug in your height and width values in the Constants.
'Form Level code
Private Const INIT_FORM_HEIGHT = 6000
Private Const INIT_FORM_WIDTH = 6000

Dim bIgnoreResize As Boolean

Private Sub Form_Resize()
  If Not bIgnoreResize Then
    ResizeControls Me
  End If
End Sub

Private Sub Form_Load()
  bIgnoreResize = False
  
  If Me.Height <> INIT_FORM_HEIGHT Or Me.Width <> INIT_FORM_WIDTH Then
    bIgnoreResize = True
    Me.Height = 6000
    Me.Width = 6000
    bIgnoreResize = False
  End If
  
  SaveSizes Me
End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:judgeking
ID: 36925635
johnnyaction, any update?
0
 
LVL 1

Author Closing Comment

by:johnnyaction
ID: 36944310
I was able to do this by setting the forms height and width if they were MDI Children. Thanks for your help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 67
Change the path of the data source in my Excel pivot table with macro 4 48
Help me. 3 60
Send outlook email from VBS Script 2 45
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question