Results 1 to 6 of 6

Thread: [RESOLVED] I've some problems calculating and putting controls in the correct coordinates

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Resolved [RESOLVED] I've some problems calculating and putting controls in the correct coordinates

    I've a form where I put a PictureBox control at the back as the container for the other pictureboxes.
    I've a main picturebox that is always present called PicBoxData. Sometimes I need to add more pictureboxes below the PicBoxData. Something like this:


    The first picturebox (the upper part) is the main one (PicBoxData). This picturebox is always visible.
    The 'optional' ones called PicBoxAux are the ones above. In this case there are 3, but it can be 0 or a maximum of 10 (I only need 10 as maximum).

    In the runtime I can add or remove 1 PicBoxAux at time. The PicBoxData is 2 times the height of the PicBoxAux. There is one exception and this exception is the last PicBoxAux (the lower one). This last PicBoxAux usually has a different height than the other PicBoxAux controls because there is a limit Y axis coordinate.



    I've part of the code done, but the problem is that sometimes the separation between the pictureboxes is 1 pixel, sometimes it isn't.
    At the same time, the total height sometimes is correct, sometimes it isn't. I think the problem is because I use 'Int' to make an integer value from the division and visual basic round it to the nearest value. For that reason sometimes is ok, sometimes no.

    In the form I've the following controls
    1. 1 picturebox that I use at the container one that is called PicBoxBackground
    2. 1 picturebox that I use as the picturebox to show data called PicBoxDatos (in the explanation of this post it's called PicBoxData)
    3. 1 picturebox that I use as the picturebox to show additional information called PicBoxIndicador (in the explanation of this post it's called PicBoxAux), with an .Index = 1.

    All the controls are in .ScaleMode = Pixels

    The variables are:
    CantidadDeIndicadores -> Amount of PicBoxAux windows that there are at a specific time in the form.
    AlturaTotalPBDatos -> Total height that I use to make the calculations. This is: Me.ScaleHeight - 25
    CantidadSectoresFormulario -> I use this to calculate how many 'blocks' it should be depending of the amount of PicBoxAux controls present. I always add '+2' because PicBoxData is always 2 times the height of the PicBoxAux.

    Here you've the code I'm using at the moment:
    Code:
    Option Explicit
    Dim CantidadDeIndicadores As Integer
    Dim AlturaTotalPBDatos As Long
    Dim CantidadSectoresFormulario As Integer
    
    Private Sub Form_Load()
        PicBoxFondo.Top = 0
        PicBoxFondo.Left = 0
        PicBoxDatos.Top = 2
        PicBoxDatos.Left = 2
        
        Dim Contador1 As Integer
        For Contador1 = 2 To 15 
            Load PicBoxIndicador(Contador1)
        Next
        Dim Contador2 As Integer
        For Contador2 = 1 To 15
            PicBoxIndicador(Contador2).Left = 2
        Next Contador2
        
        CantidadDeIndicadores = 3
        
        Call Form_Resize
    End Sub
    
    Private Sub Form_Resize()
        AlturaTotalPBDatos = Me.ScaleHeight - 25 
        CantidadSectoresFormulario = CantidadDeIndicadores + 2
        
        PicBoxFondo.Height = Me.ScaleHeight
        PicBoxFondo.Width = Me.ScaleWidth
        
        
        PicBoxDatos.Width = Me.ScaleWidth - 48
        PicBoxIndicador(1).Width = Me.ScaleWidth - 48
        
        
        If CantidadDeIndicadores = 0 Then
            PicBoxDatos.Height = Me.ScaleHeight - 23
        Else 
            PicBoxDatos.Height = (2 * (Int(AlturaTotalPBDatos / CantidadSectoresFormulario))) + 1
            Dim Contador1 As Integer
            For Contador1 = 1 To CantidadDeIndicadores
                If Contador1 = 1 Then
                    PicBoxIndicador(1).Top = PicBoxDatos.Height + 3 
                    If CantidadDeIndicadores = 1 Then PicBoxIndicador(1).Height = Me.ScaleHeight - PicBoxDatos.Height - 23
                    If CantidadDeIndicadores > 1 Then PicBoxIndicador(1).Height = Int(AlturaTotalPBDatos / CantidadSectoresFormulario)
                    PicBoxIndicador(1).Visible = True
                Else 
                    PicBoxIndicador(Contador1).Top = (Contador1 - 1) * (AlturaTotalPBDatos / CantidadSectoresFormulario) + PicBoxDatos.Height + 2 + Contador1
                    PicBoxIndicador(Contador1).Height = Int(AlturaTotalPBDatos / CantidadSectoresFormulario) 'Me.ScaleHeight - PicBoxIndicador(Contador1 - 1) - 23
                    PicBoxIndicador(Contador1).Width = Me.ScaleWidth - 48
                    PicBoxIndicador(Contador1).Visible = True
                End If
            Next
        End If
    End Sub
    What it has to be modified in the code to always have a separation of 1 pixel between the pictureboxes and that the lower part of the last PicBoxAux (the lower one) is always in the same coordinate (me.scaleheight - 23)?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: I've some problems calculating and putting controls in the correct coordinates

    Without going to far into the code you posted why not use a different method for locating the picture box

    Simply use the Top of the picturebox above it + the height of the picturebox above it + the distance of separation you want

    Code:
    NewPB.Top=OldPB.Top+OldPB.Height+DesiredSpacing

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: I've some problems calculating and putting controls in the correct coordinates

    Quote Originally Posted by DataMiser View Post
    Without going to far into the code you posted why not use a different method for locating the picture box

    Simply use the Top of the picturebox above it + the height of the picturebox above it + the distance of separation you want

    Code:
    NewPB.Top=OldPB.Top+OldPB.Height+DesiredSpacing
    It works good to keep always 1 pixel of distance between the picturebox controls. I'm performing more test to see more details, but with your code the distance is always good.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: I've some problems calculating and putting controls in the correct coordinates

    I've performed more tests and the separation problem (one pixel between the picturebox controls) is perfect.
    Now, the problem remains in the part where I said that the last PicBoxAux (the lower one) wasn't always the same height. The height varies a few pixels depending the size of the form. I think I've to add some code to calculate the correct height only in the case of the last PicBoxAux control. The other ones are the same, but the only one that is slightly different is the one in the lower part, because the height is not constant in comparation with the form size.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: I've some problems calculating and putting controls in the correct coordinates

    After writting some modifications in the code, I've the version that works ok.

    For that, I've given 3 conditions depending the amount of PicBoxAux present in the form:
    Condition 1: 1 PicBoxAux
    Condition 2: More than one and 1 unit less than the total
    Condition 3: The last PicBoxAux

    In the case of the last PicBoxAux I've modified the code that calculates the height of the last picturebox control, adapting it to Me.ScaleHeight - 23. The other PicBoxAux have the same .Height.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    290

    Re: I've some problems calculating and putting controls in the correct coordinates

    The next code represents the updated final version of the code. Note that I've implemented also some code to draw a small group of small vertical lines in the lower picturebox control (whatever picturebox) to see if the whole code was working according to the description.

    Code:
    Option Explicit
    Dim CantidadDeIndicadores As Integer
    Dim AlturaTotalPBDatos As Long 'altura en pixeles que ocuparia si solo hubiera la ventana PBDatos
    Dim CantidadSectoresFormulario As Integer 'cantidad en que se divide el formulario para hacer los calculos para el posicionamiento
    ' de las ventanas. Formula: CantidadDeIndicadores + 2
    Dim CantidadMinilineasFecha As Integer
    
    Private Sub Form_Load()
        PicBoxFondo.Top = 0
        PicBoxFondo.Left = 0
        PicBoxDatos.Top = 2
        PicBoxDatos.Left = 2
        
        Dim Contador1A As Integer
        For Contador1A = 2 To 22
            Load MiniLineaFecha(Contador1A)
        Next
        MiniLineaFecha(1).X1 = 3
        MiniLineaFecha(1).X2 = 3
        Dim Contador1B As Integer
        For Contador1B = 2 To 22
            With MiniLineaFecha(Contador1B)
                .X1 = MiniLineaFecha(Contador1B - 1).X1 + 64
                .X2 = MiniLineaFecha(Contador1B - 1).X2 + 64
            End With
        Next
        
        Dim Contador1 As Integer
        For Contador1 = 2 To 15 '15 es la cantidad maxima de indicadores soportada en esta version
            Load PicBoxIndicador(Contador1)
        Next
        Dim Contador2 As Integer
        For Contador2 = 1 To 15
            PicBoxIndicador(Contador2).Left = 2
        Next Contador2
        
        'Modificar esta linea para modificar el numero de indicadores que se muestran en pantalla
        CantidadDeIndicadores = 0
        
        Call Form_Resize
    End Sub
    
    Private Sub Form_Resize()
        AlturaTotalPBDatos = Me.ScaleHeight - 25 '(Me.ScaleHeight - 23 - 2)
        CantidadSectoresFormulario = CantidadDeIndicadores + 2
        
        PicBoxFondo.Height = Me.ScaleHeight
        PicBoxFondo.Width = Me.ScaleWidth
        
        
        PicBoxDatos.Width = Me.ScaleWidth - 48
        PicBoxIndicador(1).Width = Me.ScaleWidth - 48
        
        'MiniLineasFecha
        Dim Contador1A As Integer
        For Contador1A = 1 To (Int((PicBoxDatos.Width - 2) / 64) + 1)
            With MiniLineaFecha(Contador1A)
                .Y1 = Me.ScaleHeight - 21
                .Y2 = Me.ScaleHeight - 18
                .Visible = True
            End With
        Next
        Dim Contador1B As Integer
        For Contador1B = (Int((PicBoxDatos.Width - 2) / 64) + 2) To 22
            With MiniLineaFecha(Contador1B)
                .Visible = False
            End With
        Next
        
        
        If CantidadDeIndicadores = 0 Then
            PicBoxDatos.Height = Me.ScaleHeight - 23
        Else 'Altura del PBDatos dependiendo de la cantidad de indicadores que se esten mostrando actualmente
            PicBoxDatos.Height = (2 * (Int(AlturaTotalPBDatos / CantidadSectoresFormulario))) + 1
            Dim Contador1 As Integer
            For Contador1 = 1 To CantidadDeIndicadores
                If Contador1 = 1 Then
                    PicBoxIndicador(1).Top = PicBoxDatos.Height + PicBoxDatos.Top + 1 ' + 3 'espacio de 1 pixel entre ventana y ventana: 2 pixeles de separacion del PicBoxDatos y 1 pixel de separacion de ese formulario
                    If CantidadDeIndicadores = 1 Then PicBoxIndicador(1).Height = Me.ScaleHeight - PicBoxDatos.Height - 23 - 1
                    If CantidadDeIndicadores > 1 Then PicBoxIndicador(1).Height = Int(AlturaTotalPBDatos / CantidadSectoresFormulario)
                    PicBoxIndicador(1).Visible = True
                ElseIf Contador1 > 1 And Contador1 < (CantidadDeIndicadores) Then 'contador1 es mayor que 1          la ultima ventana hay que modificar el alto, las restantes ventanas de indicadores se calculan igual
                    PicBoxIndicador(Contador1).Top = PicBoxIndicador(Contador1 - 1).Top + PicBoxIndicador(Contador1 - 1).Height + 1 '(Contador1 - 1) * (AlturaTotalPBDatos / CantidadSectoresFormulario) + PicBoxDatos.Height + 2 + Contador1
                    PicBoxIndicador(Contador1).Height = Int(AlturaTotalPBDatos / CantidadSectoresFormulario) 'Me.ScaleHeight - PicBoxIndicador(Contador1 - 1) - 23
                    PicBoxIndicador(Contador1).Width = Me.ScaleWidth - 48
                    PicBoxIndicador(Contador1).Visible = True
                ElseIf Contador1 = CantidadDeIndicadores Then
                    PicBoxIndicador(Contador1).Top = PicBoxIndicador(Contador1 - 1).Top + PicBoxIndicador(Contador1 - 1).Height + 1
                    PicBoxIndicador(Contador1).Height = (Me.ScaleHeight - 23) - (PicBoxIndicador(1).Height * (CantidadDeIndicadores - 1)) - PicBoxDatos.Height - CantidadDeIndicadores
                    'Explicacion de la anterior linea ^
                    '-cantidad de indicadores porque tenemos que restar por cada indicador 1 pixel de separacion, por lo tanto CantidadDePixelesDeSeparacion = CantidadDeIndicadores
                    PicBoxIndicador(Contador1).Width = Me.ScaleWidth - 48
                    PicBoxIndicador(Contador1).Visible = True
                End If
            Next
    '        Dim Contador2 As Integer
    '        For Contador2 = CantidadDeIndicadores + 5 To 15
                PicBoxIndicador(CantidadDeIndicadores + 1).Visible = False
    '        Next
        End If
    End Sub
    
    Private Sub mnuAgregarVentanaIndicador_Click()
    CantidadDeIndicadores = CantidadDeIndicadores + 1
    Call Form_Resize
    End Sub
    
    Private Sub mnuEliminarVentanaIndicador_Click()
    If CantidadDeIndicadores <> 0 Then CantidadDeIndicadores = CantidadDeIndicadores - 1
    Call Form_Resize
    End Sub

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