Differenze tra le versioni di "Adattare la dimensione di un Oggetto grafico al testo contenuto"

Da Gambas-it.org - Wikipedia.
Riga 46: Riga 46:
 
   Dim DS As Integer = Desktop.Scale
 
   Dim DS As Integer = Desktop.Scale
 
   
 
   
  ' Ottengo le righe meno 1:
+
  <FONT Color=gray>' ''Ottengo le righe meno 1:''</font>
 
   If String.InStr(sText, "\n") > 0 Then  
 
   If String.InStr(sText, "\n") > 0 Then  
 
     i = Split(sText, "\n").Max
 
     i = Split(sText, "\n").Max

Versione delle 09:09, 25 giu 2023

La presente pagina considera i Controlli grafici (widget) che posseggono la Proprietà ".Text" o similare, alla quale assegnare una stringa di testo, ma non sono costituiti da un'area di testo. [nota 1]

Per adattare la dimensione di un Oggetto grafico al testo in esso contenuto, si può adottare il seguente codice: [nota 2]

Public Sub Form_Open()

 Dim hButton As Button

 With Me
   .W = 500
   .H = 500
 End With
 With hButton = New Button(Me) As "Button"
   .X = 10
   .Y = 100
   .W = 10
   .H = 10
   .AutoResize = True
   .Text = "Il testo del Button1"
 End With
 With hButton = New Button(Me) As "Button"
   .X = 10
   .Y = 200
   .W = 10
   .H = 10
   .AutoResize = True
   .Text = "Il testo di Button2\nSu\nTre righe"
 End With

End


Public Sub Button_Click()

 Dim i As Integer

 Last.Font.Size = 24
 Last.Font.Bold = True
 i = ControlHeight(Last.Text, Last.Font)
 If i > 0 Then Last.H = i

End

Private Function ControlHeight(sText As String, hFont As Font) As Integer

 Dim i As Integer
 Dim DS As Integer = Desktop.Scale

' Ottengo le righe meno 1:
 If String.InStr(sText, "\n") > 0 Then 
   i = Split(sText, "\n").Max
 Else If String.InStr(sText, "<br>") > 0 Then
   i = Split(sText, "<br>").Max
 Else 
   Return 0
 Endif
 Return ((i + 1) * hFont.Height) + (DS * 2)

End


Note

[1] Vedere anche questa pagina: Ridurre in un Oggetto grafico o di testo la dimensione del font se il testo ha una lunghezza superiore a quella dell'Oggetto

[2] Questo codice è stato proposto dal membro Gianluigi del forum www.gambas-it.org.