Platforms to show: All Mac Windows Linux Cross-Platform

/DynaPDF/Table with Listbox auto width


Required plugins for this example: MBS DynaPDF Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Table with Listbox auto width

This example is the version from Sun, 17th Jan 2015.

Project "Table with Listbox auto width.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Class MyDynaPDFMBS Inherits DynaPDFMBS
EventHandler Function Error(ErrorCode as integer, ErrorMessage as string, ErrorType as integer) As integer // output all messages on the console: System.DebugLog str(ErrorCode)+": "+ErrorMessage // and display dialog: Dim d as New MessageDialog //declare the MessageDialog object Dim b as MessageDialogButton //for handling the result d.icon=MessageDialog.GraphicCaution //display warning icon d.ActionButton.Caption="Continue" d.CancelButton.Visible=True //show the Cancel button // a warning or an error? if BitAnd(ErrorType, me.kE_WARNING) = me.kE_WARNING then // if user decided to ignore, we'll ignore if IgnoreWarnings then Return 0 d.Message="A warning occurred while processing your PDF code." // we add a third button to display all warnings d.AlternateActionButton.Caption = "Ignore warnings" d.AlternateActionButton.Visible = true else d.Message="An error occurred while processing your PDF code." end if d.Explanation = str(ErrorCode)+": "+ErrorMessage b=d.ShowModal //display the dialog Select Case b //determine which button was pressed. Case d.ActionButton Return 0 // ignore Case d.AlternateActionButton IgnoreWarnings = true Return 0 // ignore Case d.CancelButton Return -1 // stop End select End EventHandler
Property IgnoreWarnings As Boolean
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control SaveButton Inherits PushButton
ControlInstance SaveButton Inherits PushButton
EventHandler Sub Action() // setup PDF dim pdf as new MyDynaPDFMBS call pdf.CreateNewPDF call pdf.SetPageCoords(pdf.kpcTopDown) call pdf.Append call pdf.SetFont("Helvetica", 12) // create table dim ColumnCount as integer = List.ColumnCount const AllColumns = -1 const AllRows = -1 const TableWidth = 500.0 dim tbl as DynaPDFTableMBS = pdf.CreateTable(List.ListCount, ColumnCount, TableWidth, 100.0) call tbl.SetBorderWidth(AllRows, AllColumns, 1.0, 1.0, 1.0, 1.0) call tbl.SetFont(AllRows, AllColumns, "Helvetica", pdf.kfsNone, false, pdf.kcpUnicode) call tbl.SetGridWidth( 1.0, 1.0) call tbl.SetGridHorizontalColor(100, 100, 100) call tbl.SetGridVerticalColor(100, 100, 100) call tbl.SetBorderColor(AllRows, AllColumns, 100, 100, 100) dim ColumnWidths() as Double redim ColumnWidths(ColumnCount-1) // add header row dim rowNum as integer = tbl.AddRow call tbl.SetFlags rowNum, -1, tbl.ktfHeaderRow for Column as integer = 0 to ColumnCount-1 dim t as string = List.Heading(Column) call tbl.SetCellText rowNum, Column, pdf.ktaLeft, tbl.kcoCenter, t dim w as Double = pdf.GetTextWidth(t) ColumnWidths(Column) = w next // now fill all the cells dim c as integer = List.ListCount-1 for Row as integer = 0 to c rowNum = tbl.AddRow for Column as integer = 0 to ColumnCount-1 // which alignment for this cell? dim AlignMent as integer = pdf.ktaLeft Select case List.ColumnAlignment(column) case List.AlignCenter AlignMent = pdf.ktaCenter case List.AlignRight AlignMent = pdf.ktaRight end Select dim t as string = list.Cell(row, Column) call tbl.SetCellText rowNum, Column, AlignMent, tbl.kcoCenter, t dim w as Double = pdf.GetTextWidth(t) if w > ColumnWidths(Column) then ColumnWidths(Column) = w end if next next dim SumColumnWidth as Double for Column as integer = 0 to ColumnCount-1 if ColumnWidths(Column) > TableWidth/2 then // limit all columns to max 50% ColumnWidths(Column) = TableWidth/2 end if SumColumnWidth = SumColumnWidth + ColumnWidths(Column) next if SumColumnWidth >= TableWidth then dim f as Double = TableWidth / SumColumnWidth for Column as integer = 0 to ColumnCount-1 ColumnWidths(Column) = ColumnWidths(Column) * f call tbl.SetColWidth(Column, ColumnWidths(Column), false) next end if // Draw the table now call tbl.DrawTable(50.0, 50.0, 742.0) while tbl.HaveMore call pdf.EndPage call pdf.Append call tbl.DrawTable(50.0, 50.0, 742.0) wend call pdf.EndPage // A table stores errors and warnings in the error log dim err as DynaPDFErrorMBS dim ErrLogMessageCount as integer = pdf.GetErrLogMessageCount for ErrLogMessage as integer = 0 to ErrLogMessageCount-1 err = pdf.GetErrLogMessage(ErrLogMessage) MsgBox err.Message next // No fatal error occurred? if pdf.HaveOpenDoc then // We write the output file into the current directory. // OK, now we can open the output file. dim f as FolderItem = SpecialFolder.Desktop.Child("Table with Listbox.pdf") if not pdf.OpenOutputFile(f) then MsgBox "Failed to open output file!" end if call pdf.CloseFile f.launch end if End EventHandler
End Control
EventHandler Sub Open() List.ColumnAlignment(2) = List.AlignRight End EventHandler
End Class
End Project

The items on this page are in the following plugins: MBS DynaPDF Plugin.


The biggest plugin in space...