Quantcast
Channel: Visual Basic Tips and Tricks
Viewing all articles
Browse latest Browse all 2212

Stampa

$
0
0

Ciao a tutti.
Uso database Sql Server 2008 R2 con Monagement Studio, Visual Studio Community
e per la stampa PrintDocument

Stò cercando di sviluppare un piccolo applicativo per la gestione del condominio
(visto che faccio l'amministratore del e solo del condominio dove risiedo) e diciamo
che per uso personale quello che sono riuscito a fare può andare benino. Purtroppo
mi sono infancato sulla stampa del registro dei verbali.

Posto la porzione di codice interessato e mi scuso se magari potevo farne a meno di
qualche passaggio. Ho eliminato i commenti per non renderlo ancora più lungo.
Ora cerco di spiegare il mio problema, sperando di riuscire a farlo nel miglior modo possibile.

Con il codice seguente, fino al punto contrassegnato con '****************************, diciamo
che va bene in quanto la stampa è su variabili stringa di lunghezza limitata, ma a seguire e nel
mezzo con altre variabili di lunghezza dichiarata, si ha il bisogno di stampare una variabile
stringa 'Testo' VarCharMAX e quindi usare MeasureString. Siccome su un altra form con una sola
variabile normale e una 'Testo' VarCharMAX, stampo senza nessun problema. Ho copiato quel codice
e lo inserito nel punto contrassegnato in cui mi dovrebbe stampare la variabile 'Testo'. Purtroppo
stampato le variabili fino a quel punto, fa il salto pagina e inizia la stampa del testo, ma
stampata la prima pagina di quel testo quale è solo una parte, fa il salto pagina e invece di
stampare il rimanente testo, presenta una pagina vuota e termina anche se non sarebbe bastato
nemmeno quella pagina per completare la stampa del testo.
 
Dim X As Integer = CInt(NumericUpDown1.Value)
Dim Y As Integer = CInt(NumericUpDown2.Value)
Static RighePerPagina As Integer = CInt(NumericUpDown3.Value) + 1
Static AltezzaRiga As Integer = Convert.ToInt32(Font1.GetHeight(e.Graphics) + 5)
Static righeStampate As Integer = 0

Static i As Integer
Static dt As DataTable

Dim cmd2 As SqlCommand

cmd2 = New SqlCommand()
cmd2.Connection = SqlHelper.ConnessioneDatabase

If IsNothing(dt) Then

    cmd2.Connection.Open()
    Using cmd As New SqlCommand("SELECT * FROM Verbali_dettaglio WHERE Condominio=@condominio and ata=@data">Data=@data", cmd2.Connection)
          cmd.Parameters.Add(New SqlParameter("@condominio", SqlDbType.VarChar))
          cmd.Parameters("@condominio").Value = condominio
          cmd.Parameters.Add(New SqlParameter("@data", SqlDbType.Date))
          cmd.Parameters("@data").Value = data
          Using dr As SqlDataReader = cmd.ExecuteReader()
              dt = New DataTable
              dt.Load(dr)
          End Using
          cmd.Connection.Close()
    End Using
    cmd2.Connection.Close()

    i = 0 ' Puntatore al primo record

End If

Do

   If i < dt.Rows.Count Then

      giorno = dt.Rows(i)(0).ToString
      mese = dt.Rows(i)(1).ToString
      anno = dt.Rows(i)(2).ToString
      oreDal = dt.Rows(i)(3).ToString
      oreAl = dt.Rows(i)(4).ToString
      presso = dt.Rows(i)(5).ToString
      indirizzo = dt.Rows(i)(6).ToString
      comune = dt.Rows(i)(7).ToString
      convocazione = dt.Rows(i)(8).ToString
      assemblea = dt.Rows(i)(9).ToString
      cognome = dt.Rows(i)(10).ToString
      nome = dt.Rows(i)(11).ToString
      millesimi = CDec(CType(dt.Rows(i)(12), Decimal).ToString)
      pres_ass = dt.Rows(i)(13).ToString
      millesimiPres = CDec(CType(dt.Rows(i)(14), Decimal).ToString)
      delegato = dt.Rows(i)(15).ToString
      nomeDelegato = dt.Rows(i)(16).ToString
      presidente = dt.Rows(i)(17).ToString
      nomePresidente = dt.Rows(i)(18).ToString
      segretario = dt.Rows(i)(19).ToString
      nomeSegretario = dt.Rows(i)(20).ToString
      numTot = CInt(CType(dt.Rows(i)(21), Integer).ToString)
      numPres = CInt(CType(dt.Rows(i)(22), Integer).ToString)
      contar = CInt(CType(dt.Rows(i)(23), Integer).ToString)
      ordineGiorno = dt.Rows(i)(24).ToString
      ordineGiorno2 = dt.Rows(i)(25).ToString
      testo = dt.Rows(i)(26).ToString
      cognomePresente = dt.Rows(i)(27).ToString
      nomePresente = dt.Rows(i)(28).ToString

      If righeStampate < RighePerPagina Then

         If giorno >"" Then

            e.Graphics.DrawString("Il giorno " + giorno + "" + mese + "" + anno + " alle ore " + oreDal + " presso " + presso, Font5, Brushes.Black, X + 10, Y + y2)
            righeStampate += 1
            Y += AltezzaRiga

            e.Graphics.DrawString("di   " + indirizzo + "   " + comune + "   si è riunita in  " + convocazione + "  convocazione l'assemblea ", Font5, Brushes.Black, X + 10, Y + y2)
            righeStampate += 1
            Y += AltezzaRiga

            e.Graphics.DrawString(assemblea + "   dei condomini. Sono presenti o rappresentati per delega i Signori Condomini:", Font5, Brushes.Black, X + 10, Y + y2)
            righeStampate += 1
            Y += AltezzaRiga
         End If

         contaCond += numTot

         If cognome <>"" Then

            If contaCond = totaleCondomini / 2 + 1 Then
               X = 375 + X
               Y = Y - ((contaCond - 1) * AltezzaRiga)
            ElseIf contaCond - 1 = totaleCondomini Then
               X = CInt(NumericUpDown1.Value)
            End If

            Y += AltezzaRiga
            righeStampate += 1

            e.Graphics.DrawString(CStr(contaCond), Font7, Brushes.Black, X + 13, Y + y2 + 1, allineaV)
            e.Graphics.DrawString(cognome, Font7, Brushes.Black, X + 30, Y + y2 + 1)
            e.Graphics.DrawString(nome, Font7, Brushes.Black, X + 160, Y + y2 + 1)
            e.Graphics.DrawString(CStr(millesimi), Font7, Brushes.Black, X + 310, Y + y2 + 1, allineaO)
           
            If pres_ass = "Assente" Then
               e.Graphics.DrawString(pres_ass, Font7, Brushes.Black, X + 372, Y + y2 + 1, allineaO)
            Else
               e.Graphics.DrawString(CStr(millesimiPres), Font7, Brushes.Black, X + 370, Y + y2 + 1, allineaO)
            End If

            If contaCond = totaleCondomini Or Pagina > 1 Then
               X = CInt(NumericUpDown1.Value)
            End If

            If presidente <>"" Then

               Y += AltezzaRiga
               righeStampate += 1
               e.Graphics.DrawString("Vengono eletti  a Presidente:  il/la Sig./ra  " + presidente + "" + nomePresidente, Font5, Brushes.Black, X + 10, Y + y2)

               Y += AltezzaRiga
               righeStampate += 1
               e.Graphics.DrawString("                   a Segretario:  il/la Sig./ra  " + segretario + "" + nomeSegretario, Font5, Brushes.Black, X + 40, Y + y2)

               Y += AltezzaRiga
               righeStampate += 1
               e.Graphics.DrawString("Sono intervenuti o rappresentati per delega   N. " + CStr(totaleCondominiPresenti) + "   condomini su un totale di   N. " + CStr(totaleCondomini), Font5, Brushes.Black, X + 10, Y + y2)

               Y += AltezzaRiga
               righeStampate += 1
               e.Graphics.DrawString("per complessivi millesimi   " + Formatta(CStr(totaleMillesimiCondominiPresenti), TipoFor.Migliaia_con_decimali) + "   del valore totale.  Il Presidente, constatata la regolarità", Font5, Brushes.Black, X + 10, Y + y2)

               Y += AltezzaRiga
               righeStampate += 1
               e.Graphics.DrawString("della convocazione dell'assemblea e il raggiungimento del quorum per la validità, dichiara", Font5, Brushes.Black, X + 10, Y + y2)

               Y += AltezzaRiga
               righeStampate += 1
               e.Graphics.DrawString("validamente costituita l'assemblea, ed apre la discussione sul seguente:", Font5, Brushes.Black, X + 10, Y + y2)

               Y += AltezzaRiga
               righeStampate += 1
               e.Graphics.DrawString("ORDINE DEL GIORNO", Font2, Brushes.Black, X + 365, Y + 2, allineaV)

               Y += AltezzaRiga
               righeStampate += 1

            End If

            If ordineGiorno <>"" Then

               e.Graphics.DrawString(ordineGiorno, Font5, Brushes.Black, X + 10, Y + y2)
               Y += AltezzaRiga
               righeStampate += 1

            End If

            If ordineGiorno2 <>"" Then

               e.Graphics.DrawString(ordineGiorno2, Font2, Brushes.Black, X + 10, Y + y2)
               Y += AltezzaRiga
               righeStampate += 1

            End If

'***********************************************************************

            If testo <>"" Then

               Static intCurrentChar As Int32

               Dim font As New Font("Arial", 12)
               Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32

               With PrintDocument1.DefaultPageSettings
                    intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
                    intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
                    marginLeft = .Margins.Left - (X + 20) ' X coordinate
                    marginTop = .Margins.Top + (Y - (AltezzaRiga * 3) + 10) ' Y coordinate
               End With

               If PrintDocument1.DefaultPageSettings.Landscape Then
                  Dim intTemp As Int32
                  intTemp = intPrintAreaHeight
                  intPrintAreaHeight = intPrintAreaWidth
                  intPrintAreaWidth = intTemp
               End If

               Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
               Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth + 130, intPrintAreaHeight - 40)

               Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
               Dim intLinesFilled, intCharsFitted As Int32
               e.Graphics.MeasureString(Mid(testo, intCurrentChar + 1), font,
               New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, intCharsFitted, intLinesFilled)

               e.Graphics.DrawString(Mid(testo, intCurrentChar + 1), font, Brushes.Black, rectPrintingArea, fmt)

               intCurrentChar += intCharsFitted

               If intCurrentChar < testo.Length Then
                  e.HasMorePages = True
               Else
                  e.HasMorePages = False
                  intCurrentChar = 0
               End If

            End If

'************************************************************************************************************

            i += 1

         Else
           
            e.HasMorePages = True
            righeStampate = 0
            Exit Do

         End If

         e.HasMorePages = True
     Else
         ' e.HasMorePages = False
         Exit Do
     End If

Loop

If e.HasMorePages = False Then
   dt = Nothing
End If

Chiedo scusa ancora una volta per il giornale scritto, ma è da qualche giorno che sbatto la testa e chiedo se
gentilmente potreste darmi una retta. Certo della vostra disponibilità come sempre, ringrazio anticipatamente per un
eventuale interessamento da parte vostra al mio problema.
Ciao. Peppino.


Viewing all articles
Browse latest Browse all 2212