2017-04-24 6 views
1

Je crée une boîte de dialogue personnalisée dans laquelle l'utilisateur est censé sélectionner l'une des multiples entrées possibles. J'utilise une zone de liste pour répertorier les entrées possibles à sélectionner.Table de dialogue personnalisée Libre Office

Il y a plusieurs variables pour chaque ligne, donc je voudrais utiliser une table pour aligner correctement les entrées. Est-il possible de le faire?

Ce que je:

abcdefg hijkl mnopq 
abcd efghijk lmno 

Ce que je veux:

abcdefg hijkl  mnopq 
abcd  efghilkl mno 

Répondre

0

Utilisez une police à largeur fixe pour la zone de liste et pad les cordes avec des espaces.

Sub PaddedListboxItems 
    oListBox.addItems(Array(
     PaddedItem(Array("abcdefg", "hijkl", "mnopq")), 
     PaddedItem(Array("abcd", "efghijk", "lmno"))), 0) 
End Sub 

Function PaddedItem(item_strings As Array) 
    PaddedItem = PadString(item_strings(0), 10) & _ 
     PadString(item_strings(1), 11) & item_strings(2) 
End Function 

Function PadString(strSource As String, lPadLen As Long) 
    PadString = strSource & " " 
    If Len(strSource) < lPadLen Then 
     PadString = strSource & Space(lPadLen - Len(strSource)) 
    End If 
End Function 

d'autres façons de chaînes de pad en base sont à http://www.tek-tips.com/viewthread.cfm?qid=522164, bien que le travail ne sont pas tous les LibreOffice Basic.

0

Oui, c'est possible.

Créez une nouvelle boîte de dialogue et, en bas, ajoutez une étiquette. Créer un nouveau module et ajoutez le code suivant:

Option Explicit 
Option Base 0 

Dim oDialog1 As Object, oDataModel As Object, oListener As Object 

Sub OpenDialog() 
    Dim oGrid As Object, oGridModel As Object, oColumnModel As Object, oCol As Object 
    Dim oLabel1 As Object, rect(3) As Integer 

    DialogLibraries.LoadLibrary("Standard") 
    oDialog1 = CreateUnoDialog(DialogLibraries.Standard.Dialog1) 
    oGridModel = oDialog1.getModel().createInstance("com.sun.star.awt.grid.UnoControlGridModel") 

    oLabel1 = oDialog1.getModel().getByName("Label1") 
    rect(0) = oLabel1.getPropertyValue("PositionX") 
    rect(1) = 10 
    rect(2) = oLabel1.getPropertyValue("Width") 
    rect(3) = oLabel1.getPropertyValue("PositionY") - 2*rect(1) 
    With oGridModel 
     .PositionX = rect(0) 
     .PositionY = rect(1) 
     .Width = rect(2) 
     .Height = rect(3) 
    End With 

    oColumnModel = oGridModel.ColumnModel 
    oCol = oColumnModel.createColumn() 
    oCol.Title = "Column 1" 
    oColumnModel.addColumn(oCol) 

    oCol = oColumnModel.createColumn() 
    oCol.Title = "Column 2" 
    oColumnModel.addColumn(oCol) 

    oCol = oColumnModel.createColumn() 
    oCol.Title = "Column 3" 
    oColumnModel.addColumn(oCol) 

    oDialog1.getModel().insertByName("grid", oGridModel) 
    oGrid = oDialog1.getControl("grid") 
    oListener = (CreateUnoListener("grid_", "com.sun.star.awt.grid.XGridSelectionListener")) 
    oGrid.addSelectionListener(oListener) 

    oDataModel = oGridModel.GridDataModel 
    oDataModel.addRow("a", Array("abcdefg", "hijkl", "mnopq")) 
    oDataModel.addRow("b", Array("abcd", "efghijk", "lmno")) 

    oDialog1.execute() 
    oDialog1.dispose() 
End Sub 

Pour obtenir les valeurs de la ligne sélectionnée, ajoutez un écouteur pour l'événement grid_selectionChanged:

Sub grid_selectionChanged(ev) 
    Dim oRows() As Object, oLabel1 As Object, sCells(2) As String 
    oRows = ev.Source.getSelectedRows() 
    oLabel1 = oDialog1.getModel().getByName("Label1") 
    sCells(0) = oDataModel.getRowData(oRows(0))(0) 
    sCells(1) = oDataModel.getRowData(oRows(0))(1) 
    sCells(2) = oDataModel.getRowData(oRows(0))(2) 
    oLabel1.setPropertyValue("Label", "Selected values: " + sCells(0) + "," + sCells(1) + "," + sCells(2)) 
End Sub 

Si vous avez fait tout correctement, en exécutant OpenDialog vous devriez obtenir votre grille:

enter image description here