Differenze tra le versioni di "Inserire un ulteriore testo in una GridView sempre nell'ultima riga"

Da Gambas-it.org - Wikipedia.
Riga 48: Riga 48:
 
====2° codice====  
 
====2° codice====  
 
<SUP>&#091;[[#Note|nota 2]]&#093;</sup>
 
<SUP>&#091;[[#Note|nota 2]]&#093;</sup>
Private $aTotale As New String[]
+
  Private $aTotale As New String[]
 +
  Private $hText As TextBox
 +
  Private $hGrid As GridView
 +
 
 +
  Static Public Sub _init()
 +
 
 +
  Application.Theme = "Gambas"
 +
 
 +
  End
 +
 
 +
  Public Sub Form_Open()
 +
 
 +
  Dim hControl As HBox
 +
  Dim hButton As ToolButton
 +
  Dim hLabel As Label
 +
 
 +
  With Me
 +
    .H = 500
 +
    .W = 500
 +
    .Arrangement = Arrange.Vertical
 +
    .Margin = True
 +
    .Resizable = False
 +
    .Spacing = True
 +
  End With
 +
 
 +
  Me.Show
 +
 
 +
  With $hGrid = New GridView(Me)
 +
    .Columns.Count = 2
 +
    .Expand = True
 +
    .Columns[0].Expand = True
 +
    .Columns[1].Width = 150
 +
    .Columns[1].Alignment = Align.Right
 +
    .Grid = False
 +
  End With
 +
  With hControl = New HBox(Me)
 +
    .H = 35
 +
    .Spacing = True
 +
  End With
 +
  With hButton = New ToolButton(hControl) As "btnAdd"
 +
    .W = 35
 +
    .Picture = Picture["icon:/22/add"]
 +
  End With
 +
  With hLabel = New Label(hControl)
 +
    .Alignment = Align.Right
 +
    .Text = "TOTALE DOVUTO :"
 +
    .Expand = True
 +
  End With
 +
  With $hText = New TextBox(hControl)
 +
    .Width = 150
 +
  End With
 +
  With $hGrid
 +
    .Rows.Count = 1
 +
    Print .H
 +
    Print .Rows.H
 +
    .Rows.Count = (.H \ .Rows.H)
 +
  End With
 +
 
 +
  End
 +
 
 +
  Public Sub btnAdd_Click()
 +
 
 +
  Dim aConsumazioni As String[] = ["PANINO FARCITO", "ACQUA FRIZZANTE 1/2", "ACQUA NATURALE 1/2", "BIBITA GASATA", "BIRRA", "FETTA DI TORTA", "COPPA GELATO", "BICCHIERE VINO"]
 +
  Dim aPrezzi As Float[] = [3.5, 1.0, 0.95, 2.5, 3.0, 2.5, 1.9, 1.8]
 +
  Dim r, p As Integer
 +
  <FONT Color=gray> ' un numero a caso</font>
 +
  p = Rand(0, 7)
 +
  <FONT Color=gray> ' Se la griglia è piena aggiunge una riga e la riempe altrimenti riempe la prima riga e con swap la posiziona</font>
 +
  With $hGrid
 +
    If $hGrid[0, 0].Text <> "" Then
 +
      .Rows.Count += 1
 +
      $hGrid[.Rows.Max, 0].Text = aConsumazioni[p]
 +
      $hGrid[.Rows.Max, 1].Text = Format(aPrezzi[p], "$,0.00")
 +
      $aTotale.Push($hGrid[.Rows.Max, 1].Text)
 +
    Else
 +
      $hGrid[0, 0].Text = aConsumazioni[p]
 +
      $hGrid[0, 1].Text = Format(aPrezzi[p], "$,0.00")
 +
      $aTotale.Push($hGrid[0, 1].Text)
 +
      For r = 1 To .Rows.Max
 +
        Swap $hGrid[r, 0].Text, $hGrid[r - 1, 0].Text
 +
        Swap $hGrid[r, 1].Text, $hGrid[r - 1, 1].Text
 +
      Next
 +
    Endif
 +
  End With
 +
  $hText.Text = ReturnAddString($aTotale)
 +
 
 +
  End
 +
 
 +
  Private Function ReturnAddString(value As String[]) As String
 +
 
 +
  <FONT Color=gray> ' Ritorna la somma dei numeri 'stringa' come una stringa formattata currency</font>
 +
  Dim sCurr As String
 +
  Dim iCurr As Long
 +
 
 +
  For i As Integer = 0 To value.Max
 +
    sCurr = ReturnStringInteger(value[i])
 +
    If IsNull(sCurr) Then Continue
 +
    iCurr += CLong(sCurr)
 +
  Next
 +
  Return Format((iCurr / 100), "$,0.00")
 +
 
 +
  End
 +
 
 +
  Private Function ReturnStringInteger(value As String) As String
 +
 
 +
  <FONT Color=gray> ' Ritorna una stringa in forma di numero intero</font>
 +
  value = Trim(Replace(value, "€", ""))
 +
  value = Trim(Replace(value, "$", ""))
 +
  value = Trim(Replace(value, "£", ""))
 +
  value = Replace(value, ".", "")
 +
  value = Replace(value, " ", "")
 +
  value = Replace(value, ",", "")
 +
  Return value
 +
 
 +
  End
 
   
 
   
Static Public Sub _init()
 
 
  Application.Theme = "Gambas"
 
 
End
 
 
Public Sub Form_Open()
 
 
  Me.Show
 
  TextBox1.Width = 150
 
  With GridView1
 
    .Columns.Count = 2
 
    .Rows.Count = 1
 
    Print .H
 
    Print .Rows.H
 
    .Rows.Count = (.H \ .Rows.H)
 
    .Columns[0].Expand = True
 
    .Columns[1].Width = 150
 
    .Columns[1].Alignment = Align.Right
 
  End With
 
 
End
 
 
Public Sub btnAdd_Click()
 
 
  Dim aConsumazioni As String[] = ["PANINO FARCITO", "ACQUA FRIZZANTE 1/2", "ACQUA NATURALE 1/2", "BIBITA GASATA", "BIRRA", "FETTA DI TORTA", "COPPA GELATO", "BICCHIERE VINO"]
 
  Dim aPrezzi As Float[] = [3.5, 1.0, 0.95, 2.5, 3.0, 2.5, 1.9, 1.8]
 
  Dim r, p As Integer
 
<FONT Color=gray>' ''un numero a caso''</font>
 
  p = Rand(0, 7)
 
<FONT Color=gray>' ''Se la griglia è piena aggiunge una riga e la riempe altrimenti riempe la prima riga e con swap la posiziona''</font>
 
  With GridView1
 
    If GridView1[0, 0].Text <> "" Then
 
      .Rows.Count += 1
 
      GridView1[.Rows.Max, 0].Text = aConsumazioni[p]
 
      GridView1[.Rows.Max, 1].Text = Format(aPrezzi[p], "$,0.00")
 
      $aTotale.Push(GridView1[.Rows.Max, 1].Text)
 
    Else
 
      GridView1[0, 0].Text = aConsumazioni[p]
 
      GridView1[0, 1].Text = Format(aPrezzi[p], "$,0.00")
 
      $aTotale.Push(GridView1[0, 1].Text)
 
      For r = 1 To .Rows.Max
 
        Swap GridView1[r, 0].Text, GridView1[r - 1, 0].Text
 
        Swap GridView1[r, 1].Text, GridView1[r - 1, 1].Text
 
      Next
 
    Endif
 
  End With
 
  TextBox1.Text = ReturnAddString($aTotale)
 
 
End
 
 
Private Function ReturnAddString(value As String[]) As String
 
 
<FONT Color=gray>' ''Ritorna la somma dei numeri 'stringa' come una stringa formattata currency''</font>
 
  Dim sCurr As String
 
  Dim iCurr As Long
 
 
  For i As Integer = 0 To value.Max
 
    sCurr = ReturnStringInteger(value[i])
 
    If IsNull(sCurr) Then Continue
 
    iCurr += CLong(sCurr)
 
  Next
 
  Return Format((iCurr / 100), "$,0.00")
 
 
End
 
 
Private Function ReturnStringInteger(value As String) As String
 
 
<FONT Color=gray>' ''Ritorna una stringa in forma di numero intero''</font>
 
  value = Trim(Replace(value, "€", ""))
 
  value = Trim(Replace(value, "$", ""))
 
  value = Trim(Replace(value, "£", ""))
 
  value = Replace(value, ".", "")
 
  value = Replace(value, " ", "")
 
  value = Replace(value, ",", "")
 
  Return value
 
 
End
 
  
  

Versione delle 14:50, 3 lug 2023

Il caso è quello in cui, ogni qual volta deve essere inserito del testo in una GridView, esso viene inserito nell'ultima riga spostando verso le righe superiori quello preesistente. [nota 1]

Mostriamo due possibili codici.

1° codice

Private i As Integer


Public Sub Form_Open()

 With GridView1
   .Columns.Count = 3
   .Rows.Count = 3
   .Rows.Height = 21
   .Columns[0].Width = GridView1.W * 0.45
   .Columns[1].Width = GridView1.W * 0.45
   .Columns[2].Width = GridView1.W * 0.05
   .Columns[2].Alignment = Align.Center
 End With

End


Public Sub Button1_Click()

 Inc i

 If i < 4 Then 
   For c As Short = 0 To GridView1.Rows.Max - 1
     GridView1[c, 0].Text = GridView1[c + 1, 0].Text
     GridView1[c, 1].Text = GridView1[c + 1, 1].Text
     GridView1[c, 2].Text = GridView1[c + 1, 2].Text
   Next 
 Else 
' Se tutte e tre le righe iniziali della "GridView" sono state popolate con il testo, viene aggiunta una riga in ultimo:
   GridView1.Rows.Count += 1 
 Endif

 GridView1[GridView1.Rows.Max, 0].Text = "Testo 1"
 GridView1[GridView1.Rows.Max, 1].Text = "Testo 2"
 GridView1[GridView1.Rows.Max, 2].Text = CStr(i)

' Fa sì che sia sempre visibile l'ultima riga della "GridView":
 GridView1.ScrollY = i * GridView1.Rows.H

End

2° codice

[nota 2]

 Private $aTotale As New String[]
 Private $hText As TextBox
 Private $hGrid As GridView
 
 Static Public Sub _init()
 
  Application.Theme = "Gambas"
 
 End
 
 Public Sub Form_Open()
 
  Dim hControl As HBox
  Dim hButton As ToolButton
  Dim hLabel As Label
 
  With Me
    .H = 500
    .W = 500
    .Arrangement = Arrange.Vertical
    .Margin = True
    .Resizable = False
    .Spacing = True
  End With
 
  Me.Show
 
  With $hGrid = New GridView(Me)
    .Columns.Count = 2
    .Expand = True
    .Columns[0].Expand = True
    .Columns[1].Width = 150
    .Columns[1].Alignment = Align.Right
    .Grid = False
  End With
  With hControl = New HBox(Me)
    .H = 35
    .Spacing = True
  End With
  With hButton = New ToolButton(hControl) As "btnAdd"
    .W = 35
    .Picture = Picture["icon:/22/add"]
  End With
  With hLabel = New Label(hControl)
    .Alignment = Align.Right
    .Text = "TOTALE DOVUTO :"
    .Expand = True 
  End With
  With $hText = New TextBox(hControl)
    .Width = 150
  End With
  With $hGrid
    .Rows.Count = 1
    Print .H
    Print .Rows.H
    .Rows.Count = (.H \ .Rows.H)
  End With
 
 End
 
 Public Sub btnAdd_Click()
 
  Dim aConsumazioni As String[] = ["PANINO FARCITO", "ACQUA FRIZZANTE 1/2", "ACQUA NATURALE 1/2", "BIBITA GASATA", "BIRRA", "FETTA DI TORTA", "COPPA GELATO", "BICCHIERE VINO"]
  Dim aPrezzi As Float[] = [3.5, 1.0, 0.95, 2.5, 3.0, 2.5, 1.9, 1.8]
  Dim r, p As Integer
  ' un numero a caso
  p = Rand(0, 7)
  ' Se la griglia è piena aggiunge una riga e la riempe altrimenti riempe la prima riga e con swap la posiziona
  With $hGrid
    If $hGrid[0, 0].Text <> "" Then 
      .Rows.Count += 1
      $hGrid[.Rows.Max, 0].Text = aConsumazioni[p]
      $hGrid[.Rows.Max, 1].Text = Format(aPrezzi[p], "$,0.00")
      $aTotale.Push($hGrid[.Rows.Max, 1].Text)
    Else 
      $hGrid[0, 0].Text = aConsumazioni[p]
      $hGrid[0, 1].Text = Format(aPrezzi[p], "$,0.00")
      $aTotale.Push($hGrid[0, 1].Text)
      For r = 1 To .Rows.Max
        Swap $hGrid[r, 0].Text, $hGrid[r - 1, 0].Text 
        Swap $hGrid[r, 1].Text, $hGrid[r - 1, 1].Text 
      Next
    Endif
  End With
  $hText.Text = ReturnAddString($aTotale)
 
 End
 
 Private Function ReturnAddString(value As String[]) As String
 
  ' Ritorna la somma dei numeri 'stringa' come una stringa formattata currency
  Dim sCurr As String
  Dim iCurr As Long
 
  For i As Integer = 0 To value.Max
    sCurr = ReturnStringInteger(value[i])
    If IsNull(sCurr) Then Continue
    iCurr += CLong(sCurr)
  Next
  Return Format((iCurr / 100), "$,0.00")
 
 End
 
 Private Function ReturnStringInteger(value As String) As String
 
  ' Ritorna una stringa in forma di numero intero
  value = Trim(Replace(value, "€", ""))
  value = Trim(Replace(value, "$", ""))
  value = Trim(Replace(value, "£", ""))
  value = Replace(value, ".", "")
  value = Replace(value, " ", "")
  value = Replace(value, ",", "")
  Return value
 
 End


Note

[1] Vedere anche questa pagina: Aggiungere righe in una GridView da codice mediante un tasto

[2] Questo codice è stato suggerito da Gianluigi, membro del forum www.gambas-it.org