Positioning controls dynamically

Posted by: blackriver on 4 August 2017, 3:18 pm EST

  • Posted 4 August 2017, 3:18 pm EST

    Hello,
    I will try and explain my problem the best way I can.

    We're developing a report system that allows users to select which fields (from recordset) they want to show in their report.
    We load Field names from the recordset into ListView1 and then the user select the fields he/she wants to display and transfers them to ListView2.
    The report is created dynamically, and depending on how many fields the user has selected we create Labels and TextBoxes.

    So, the first label is created at .Left = 0 and its width is calculated like: TextWidth(ListView2.ListItems.ListViewItem.Text)
    After that, we create the first TextBox and set it's .Left = 0 and width is calculated like : TextWidth(recordset.Fields(recordposition).Value)

    After this we create all the other labels and fields.
    We check to see if the FIRST label width > FIRST field width...If this is TRUE then the second label .Left will be: FirstLabel.Left + FirstLabelWidth.If the FirstLabelWidth < FirstFieldWidth then SecondLabel.Left = FirstTextLeft + FirstTextWidth

    Now this works fine if we have a case where the first 3 or 4 values added from the recordset to the field have the same width.But what happens if the FIFTH value is much wider then the previous 4?Then the next Label will not start from the correct left position and also,there will be some overlapping of the TextBox controls.

    Basically, we're trying to find a way to set up the Width and the Left position of the controls AFTER everything has been added to the report.I know we can use WithEvents to access Details_Format sub and others, but not sure if that is the right way to go.

    Any suggestions will be appreciated
  • Replied 4 August 2017, 3:18 pm EST

    Hello,

    The report generates the pages at runtime and not at design time.So,it might not be possible to move the controls to a new page.But you may try using logic as following to meet your requirements.

    Dim no_of_controls_in_secondrow as double          

    If control.Left>PrintWidth then

    control.Left= no_of_controls_in_secondrow *Max_Label
    control.top=Label1.top+Label1.height + 1.0(random value)
    no_of_controls_in_secondrow =no_of_controls_in_secondrow +1

    Regards,
    Prantik
  • Replied 4 August 2017, 3:18 pm EST

    Thank you for your reply

    Your logic seems OK, but I think I didn't explain myself good enough.

    I add Labels to GroupHeader section and Fields to Detail section. If the LabelXY.Left > PrintWidth then I need to place that label UNDER the last record displayed in the Details Section and continue adding Fields as needed. The logic you suggested might work, but wouldn't that print the label every time the Detail_Format event is fired?

    So basically, I'm trying to figure out a way to print a report even if it's content is wider than PrintWidth. And my first idea was to print the labels that don't fit under the last record.
    If that doesn't work , what are my alternatives? Can I use Canvas? Subreports? Are there any examples that show how to deal with this kind of situation?

    Once again, thank you for your time!
  • Replied 4 August 2017, 3:18 pm EST

    Hello,

    Thank you for your query.I would like to inform you that you could set up the width and left properties of a control in the detail format.

     Dim obj1 As Object
     Set obj1 = Me.Sections("Detail").Controls("Name_of_Field")
      With obj
       .Left = Left
       .Width=width
      End With

    But this would not be advisable as it may cause issues in the display of other controls within the report.

    Considering that you have the recordset available to you before the report is created,I think the best way to go about this issue would be to:-

    1)Iterate through the Listview and find maximum value(Max_Label) of TextWidth(ListView2.ListItems.ListViewItem.Text)
    2)Iterate through the recordset and find maximum value(Max_TextBox) TextWidth(recordset.Fields(recordposition).Value)
    3)Now you could set the position the controls on the report on the basis of this data such as

                     Label1.Left=0
                     Label2.Left=Max_Label+Max_TextBox

    Regards,
    Prantik

  • Replied 4 August 2017, 3:18 pm EST

    Thank you for you reply PrantikS

    I used you suggestion as a starting point for my logic and it worked for me.
    But I do have one more question regarding PrintWidth and controls positions.

    Sometimes a user selects 15 or more fields to be displayed in a report.We print the report in Landscape mode,so in some cases the controls fit nicely, but sometimes ControlXY.Left is greater than PrintWidth.

    Is there a way for me to take that control and all other controls that have  .Left > PrintWidth and "move" them to a new page?

    I hope you understand my problem!

    Thank you!
  • Replied 4 August 2017, 3:18 pm EST

    Hello,



    In order to handle this situation,you may try drawing the labels and
    Fields that donot fit into the width of the report in the first report
    in a second report and then merge the pages.For merging the two
    reports,you can use code as follows.



    Dim i As Integer

    For i = 0 To rpt2.Pages.Count - 1

       rpt1.Pages.Insert rpt1.Pages.Count, rpt2.Pages(i)

    Next i

    rpt1.Pages.Commit



    Regards,

    Prantik
  • Replied 4 August 2017, 3:18 pm EST

    You have been very helpful so far and I have one more simple question for you, before I start working on my project again.
    I understand that I can use WithEvents to access different section events.
    So if I want to access Detail_Format would this be the correct way to go:

    'General Declarations
    Dim WithEvents sec1 as DDActiveReports2.Section
    'some code here

    Public Function CreateDynamicReport()

    Dim rptMain as ActiveReport1
    Set rptMain = New ActiveReport1

    'some code here for setting up the sections

    Set sec1 = rptMain.Sections("Detail")

    'code for setting up controls etc

    rptMani.Show

    End Function

    Private Sub sec1 _Format()
    'some code here
    End Sub

    Thank you!

  • Replied 4 August 2017, 3:18 pm EST

    Hello,

    The code that you have written would work fine.I have created a small sample to demonstrate this.Please take a look.

    Regards,
    Prantik

    2010/08/TestSample.zip
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels