è la prima volta che mi capita una domanda del genere, quindi ne faccio una io a voi: quale vantaggio c'è a lavorare in questo modo ? mi sembra più semplice inserire i dati nel foglio.
Codice: Seleziona tutto
Sub coboDataForm()
' Visualizza un modulo dati simile a quello di Excel.
Dim lungFrm, posY, posX as Single
Dim i as Integer
Dim oCtrl, aWidth, maxLblWidth
Dim oCur
initStrLocale ' inizializza la lingua
oRange = thisComponent.getCurrentSelection
oFoglio = oRange.getSpreadSheet
If oRange.Rows.Count = 1 And oRange.Columns.Count = 1 Then
' single cell, expand to current region
oCur = oFoglio.createCursorByRange(oRange)
oCur.collapseToCurrentRegion
oRange = oFoglio.getCellRangeByName(oCur.absoluteName)
End If
If oRange.Rows.Count = 1 Then 'And oRange.Columns.Count = 1 Then
' dati insufficienti per modulo dati, termina
msgbox strLocale(MSGNODATA), 16
Exit Sub
End If
nCols = oRange.getColumns.Count
nRows = oRange.getRows.Count
' ridimensiona array criteri
ReDim arrCriteri(1 to nCols)
' creazione dialog
lungFrm = CTRLS_MARGIN + nCols * (TXT_HEIGHT + CTRLS_MARGIN)
If lungFrm < MIN_HEIGHT_FRM Then lungFrm = MIN_HEIGHT_FRM
' dialog height is already known, set it in the model
oDlg = DlgTool_CreateNewDialog(0, 0, btnWidth, lungFrm, oFoglio.Name)
'*************************************************
'* Campi di controllo variabili relativi al range
posY = CTRLS_MARGIN
posX = 7 ' left margin (pixel)
For i = 1 to nCols
DlgTool_Label(oDlg, 3, posY+2, 71, TXT_HEIGHT, "lblCampo" & i, oRange.getCellByPosition(i-1, 0).getString & ":")
oCtrl = oDlg.getControl("lblCampo" & i)
aWidth = oCtrl.getPreferredSize.Width
oCtrl.setPosSize(posX, 0, aWidth, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
If aWidth > maxLblWidth Then maxLblWidth = aWidth
posY = posY + TXT_HEIGHT + CTRLS_MARGIN
Next
'**************************
'* calcolo larghezza form *
'**************************
Dim newWidth , widthScreen as Integer
newWidth = getMaxTextLen() * FONTSIZE
widthScreen = getScreenWidth() / 3
If newWidth < MINTEXTLEN Then
newWidth = MINTEXTLEN
End If
If newWidth > widthScreen Then
newWidth = widthScreen
End If
'---------------------------
posY = CTRLS_MARGIN
posX = posX + maxLblWidth + 10 ' += widest label + 10 pixel
For i = 1 to nCols
'DlgTool_Text(oDlg, 75, posY, 83, TXT_HEIGHT, "txtCampo" & i, "", "txtCampo")
DlgTool_Text(oDlg, 0,posY,83, TXT_HEIGHT, "txtCampo" & i, "", "txtCampo")
' --
' sposta casella in base alla larghezza della label più larga
oCtrl = oDlg.getControl("txtCampo" & i)
'oCtrl.setPosSize(posX, 0,130, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
'per cambiare la dimensione della casella di testo modificare il valore del terzo parametro
oCtrl.setPosSize(posX, 0, newWidth , 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
' --
posY = posY + TXT_HEIGHT + CTRLS_MARGIN
Next
'*************************************************
'* Campi di controllo fissi
'posX = posX + 130 + 10 ' += textbox width + 10 pixel
posX = posX + newWidth + 10 ' += textbox width + 10 pixel
' barra di scorrimento verticale
'DlgTool_Scrollbar(oDlg, -55, CTRLS_MARGIN, 8, lungFrm - (CTRLS_MARGIN * 2), "scrlBarra", com.sun.star.awt.ScrollBarOrientation.VERTICAL)
DlgTool_Scrollbar(oDlg, 0, CTRLS_MARGIN, 8, lungFrm - (CTRLS_MARGIN * 2), "scrlBarra", com.sun.star.awt.ScrollBarOrientation.VERTICAL)
oCtrl = oDlg.getControl("scrlBarra")
oCtrl.Maximum = nRows - 1
oCtrl.setPosSize(posX, 0, 17, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
posX = posX + 17 + 10 ' += scrollbar width + 10 pixel
' label in alto a destra
DlgTool_Label(oDlg, -5, 5, btnWidth, 14, "lblConta", "")
oCtrl = oDlg.getControl("lblConta")
oCtrl.Alignment = com.sun.star.awt.TextAlign.CENTER
oCtrl.setPosSize(posX, 0, btnWidth + 40, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
' pulsante "Nuovo"
DlgTool_Button(oDlg, -5, 17, btnWidth, 14, "btnNuovo", strLocale(CMDNEW))
oCtrl = oDlg.getControl("btnNuovo")
oCtrl.setPosSize(posX, 0, btnWidth + 40, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
' pulsante "Elimina"
DlgTool_Button(oDlg, -5, 35, btnWidth, 14, "btnElimina", strLocale(CMDDELETE))
oCtrl = oDlg.getControl("btnElimina")
oCtrl.setPosSize(posX, 0, btnWidth + 40, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
' pulsante "Ripristina"
DlgTool_Button(oDlg, -5, 54, btnWidth, 14, "btnRipristina", strLocale(CMDRESTORE))
oCtrl = oDlg.getControl("btnRipristina")
oCtrl.Enable = False ' disabilitato
oCtrl.setPosSize(posX, 0, btnWidth + 40, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
' pulsante "Trova prec."
DlgTool_Button(oDlg, -5, 75, btnWidth, 14, "btnPrec", strLocale(CMDFINDPREV))
oCtrl = oDlg.getControl("btnPrec")
oCtrl.setPosSize(posX, 0, btnWidth + 40, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
' pulsante "Trova succ."
DlgTool_Button(oDlg, -5, 93, btnWidth, 14, "btnSucc", strLocale(CMDFINDNEXT))
oCtrl = oDlg.getControl("btnSucc")
oCtrl.setPosSize(posX, 0, btnWidth + 40, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
' pulsante "Criteri"
DlgTool_Button(oDlg, -5, 110, btnWidth, 14, "btnCriteri", strLocale(CMDCRITERIA))
oCtrl = oDlg.getControl("btnCriteri")
oCtrl.setPosSize(posX, 0, btnWidth + 40, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
' pulsante "Chiudi"
DlgTool_Button(oDlg, -5, 133, btnWidth, 14, "btnChiudi", strLocale(CMDCLOSE))
oCtrl = oDlg.getControl("btnChiudi")
oCtrl.setPosSize(posX, 0, btnWidth + 40, 0, com.sun.star.awt.PosSize.X + com.sun.star.awt.PosSize.WIDTH)
'*************************************************
oDlg.setPosSize(0, 0, posX + btnWidth + 50, 0, com.sun.star.awt.PosSize.WIDTH)
scorriRecord(1) ' visualizza campi del primo record
modalita = MODULO ' modalità predefinita all'avvio
inizioEsecuzione = True
DlgTool_Execute(oDlg) ' visualizza il dialog
End Sub