Solved

MDI Child form resize controls Visual Basic 6

Posted on 2011-09-27
15
1,494 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now