Imprimer plusieurs pages avec VB.Net

poilusduboux Messages postés 83 Date d'inscription jeudi 19 mai 2005 Statut Membre Dernière intervention 17 juin 2011 - 26 oct. 2006 à 08:59
Philou30290 Messages postés 1 Date d'inscription jeudi 22 juillet 2010 Statut Membre Dernière intervention 29 juillet 2010 - 29 juil. 2010 à 16:10
Bonjour à tous <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /??>






 






J’ai un petit problème avec l’impression de plusieurs pages





J’utilise la fonction e.HasMorePages.





Lorsque j’imprime seulement une page pas de problème.





Mais lorsque je dois imprimer plusieurs page,





Je passe  e.HasMorePages à true.





Mais la il ne s’arrete plus d’imprimer en plus toujours la même page.





Si vous avez une idée…





Je suis preneur





Merci





D’avance






 

5 réponses

tititrivis Messages postés 5 Date d'inscription vendredi 15 avril 2005 Statut Membre Dernière intervention 2 janvier 2007 1
26 oct. 2006 à 10:47
1
tititrivis Messages postés 5 Date d'inscription vendredi 15 avril 2005 Statut Membre Dernière intervention 2 janvier 2007 1
26 oct. 2006 à 10:26
pour comprendre otn pb il faut comprendre l'utilisation de hasMorePages.
Lorsque tu mets hasMorePages à vrai la procédure print de ton document va etre rapelée, donc si entre temps tu changes rien sa va tourné en boucle infiniment.
Pour résoudre ton pb regarde si il te reste des choses a imprimer et dans ce cas met hasmorepage a vrai, sinon met le à faux...
0
poilusduboux Messages postés 83 Date d'inscription jeudi 19 mai 2005 Statut Membre Dernière intervention 17 juin 2011
26 oct. 2006 à 10:37
Merci pour la réponse


Mais…


Tu n’aurais pas un petit exemple


Par ce que je plane !
0
poilusduboux Messages postés 83 Date d'inscription jeudi 19 mai 2005 Statut Membre Dernière intervention 17 juin 2011
27 oct. 2006 à 10:28
Bonjour à tous
je planne toujours
je dois présenter mon soft LUNDI...

voici mon code pour imprimer un Datagrid avec VB.net

Private oStringFormat
As StringFormat

Private oStringFormatComboBox
As StringFormat

Private oButton
As Button

Private oCheckbox
As CheckBox

Private oComboBox
As ComboBox

Private nTotalWidth
As Int16

Private nRowPos
As Int16

Private NewPage
As
Boolean

Private nPageNo
As Int16

Private Header
As
String =
"Liste des plans"

Private sUserName
As
String =
"MonSoft"

Private

Sub PrintDocument1_PrintPage(
ByVal sender
As System.Object,
ByVal e
As System.Drawing.Printing.PrintPageEventArgs)
Handles PrintDocument1.PrintPageoStringFormat New StringFormatoStringFormat.Alignment StringAlignment.Near

oStringFormat.LineAlignment = StringAlignment.Center

oStringFormat.Trimming = StringTrimming.EllipsisCharacter
'oStringFormatComboBox New StringFormatoStringFormatComboBox.LineAlignment StringAlignment.Center

oStringFormatComboBox.FormatFlags = StringFormatFlags.NoWrap

oStringFormatComboBox.Trimming = StringTrimming.EllipsisCharacter
oButton New ButtonoCheckbox
New CheckBoxoComboBox New ComboBoxnTotalWidth 0

For
Each oColumn
As DataGridViewColumn
In DataGridView1.ColumnsnTotalWidth += oColumn.Width

NextnPageNo = 1
NewPage TruenRowPos 0

 

Static oColumnLefts
As
New ArrayList

Static oColumnWidths
As
New ArrayList

Static oColumnTypes
As
New ArrayList

Static nHeight
As Int16

Dim nWidth, i, nRowsPerPage
As Int16

Dim nTop
As Int16 = e.MarginBounds.Top

Dim nLeft
As Int16 = e.MarginBounds.Left

If nPageNo = 1
Then

For
Each oColumn
As DataGridViewColumn In DataGridView1.ColumnsnWidth CType(Math.Floor(oColumn.Width / nTotalWidth * nTotalWidth * (e.MarginBounds.Width / nTotalWidth)), Int16)nHeight e.Graphics.MeasureString(oColumn.HeaderText, oColumn.InheritedStyle.Font, nWidth).Height + 11

oColumnLefts.Add(nLeft)

oColumnWidths.Add(nWidth)

oColumnTypes.Add(oColumn.GetType)

nLeft += nWidth

Next

End
If

Do
While nRowPos < DataGridView1.Rows.Count - 1

Dim oRow
As DataGridViewRow = DataGridView1.Rows(nRowPos)

If nTop + nHeight >= e.MarginBounds.Height + e.MarginBounds.Top
ThenDrawFooter(e, nRowsPerPage)

NewPage =

TruenPageNo += 1

e.HasMorePages =

True

Exit
Sub

Else

If NewPage
Then

' Draw Header

'e.Graphics.DrawString(Header, New Font(DataGridView1.Font, FontStyle.Bold), Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top - e.Graphics.MeasureString(Header, New Font(DataGridView1.Font, FontStyle.Bold), e.MarginBounds.Width).Height - 13)

'Draw(Columns)nTop = e.MarginBounds.Top

i = 0

For
Each oColumn
As DataGridViewColumn
In DataGridView1.Columnse.Graphics.FillRectangle(

New SolidBrush(Drawing.Color.LightGray),
New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))e.Graphics.DrawRectangle(Pens.Black,

New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))e.Graphics.DrawString(oColumn.HeaderText, oColumn.InheritedStyle.Font,

New SolidBrush(oColumn.InheritedStyle.ForeColor),
New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)i += 1

NextNewPage =

False

End
IfnTop += nHeight

i = 0

For
Each oCell
As DataGridViewCell
In oRow.Cells

If oColumnTypes(i)
Is
GetType(DataGridViewTextBoxColumn)
OrElse oColumnTypes(i)
Is
GetType(DataGridViewLinkColumn)
Thene.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font,

New SolidBrush(oCell.InheritedStyle.ForeColor),
New RectangleF(oColumnLefts(i), nTop, oColumnWidths(i), nHeight), oStringFormat)

ElseIf oColumnTypes(i)
Is
GetType(DataGridViewButtonColumn)
ThenoButton.Text = oCell.Value.ToString

oButton.Size =

New Size(oColumnWidths(i), nHeight)

Dim oBitmap
As
New Bitmap(oButton.Width, oButton.Height)oButton.DrawToBitmap(oBitmap,

New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))e.Graphics.DrawImage(oBitmap,

New Point(oColumnLefts(i), nTop))

ElseIf oColumnTypes(i)
Is
GetType(DataGridViewCheckBoxColumn) ThenoCheckbox.Size New Size(14, 14)oCheckbox.Checked

CType(oCell.Value,
Boolean)

Dim oBitmap
As
New Bitmap(oColumnWidths(i), nHeight)

Dim oTempGraphics
As Graphics = Graphics.FromImage(oBitmap)oTempGraphics.FillRectangle(Brushes.White,

New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))oCheckbox.DrawToBitmap(oBitmap,

New Rectangle(
CType((oBitmap.Width - oCheckbox.Width) / 2, Int32),
CType((oBitmap.Height - oCheckbox.Height) / 2, Int32), oCheckbox.Width, oCheckbox.Height))e.Graphics.DrawImage(oBitmap,

New Point(oColumnLefts(i), nTop))

ElseIf oColumnTypes(i)
Is
GetType(DataGridViewComboBoxColumn)
ThenoComboBox.Size =

New Size(oColumnWidths(i), nHeight)

Dim oBitmap
As
New Bitmap(oComboBox.Width, oComboBox.Height)oComboBox.DrawToBitmap(oBitmap,

New Rectangle(0, 0, oBitmap.Width, oBitmap.Height))e.Graphics.DrawImage(oBitmap,

New Point(oColumnLefts(i), nTop))e.Graphics.DrawString(oCell.Value.ToString, oCell.InheritedStyle.Font,

New SolidBrush(oCell.InheritedStyle.ForeColor),
New RectangleF(oColumnLefts(i) + 1, nTop, oColumnWidths(i) - 16, nHeight), oStringFormatComboBox)

ElseIf oColumnTypes(i)
Is
GetType(DataGridViewImageColumn)
Then

Dim oCellSize
As Rectangle =
New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight)

Dim oImageSize
As Size =
CType(oCell.Value, Image).Sizee.Graphics.DrawImage(oCell.Value,

New Rectangle(oColumnLefts(i) +
CType(((oCellSize.Width - oImageSize.Width) / 2), Int32), nTop +
CType(((oCellSize.Height - oImageSize.Height) / 2), Int32),
CType(oCell.Value, Image).Width,
CType(oCell.Value, Image).Height))

End
Ife.Graphics.DrawRectangle(Pens.Black,

New Rectangle(oColumnLefts(i), nTop, oColumnWidths(i), nHeight))i += 1

Next

End
IfnRowPos += 1

nRowsPerPage += 1

LoopDrawFooter(e, nRowsPerPage)

e.HasMorePages =

False

End
Sub

Private
Sub DrawFooter(
ByVal e
As System.Drawing.Printing.PrintPageEventArgs,
ByVal RowsPerPage
As Int32)

Dim sPageNo
As
String = nPageNo.ToString +
" of " + Math.Ceiling(DataGridView1.Rows.Count / RowsPerPage).ToString

' Right Align - User Namee.Graphics.DrawString(sUserName, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width), e.MarginBounds.Top + e.MarginBounds.Height + 7)

' Left Align - Date/Timee.Graphics.DrawString(Now.ToLongDateString +

" " + Now.ToShortTimeString, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + e.MarginBounds.Height + 7)

' Center - Page No. Infoe.Graphics.DrawString(sPageNo, DataGridView1.Font, Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(sPageNo, DataGridView1.Font, e.MarginBounds.Width).Width) / 2, e.MarginBounds.Top + e.MarginBounds.Height + 31)

 

 

 

 
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Philou30290 Messages postés 1 Date d'inscription jeudi 22 juillet 2010 Statut Membre Dernière intervention 29 juillet 2010
29 juil. 2010 à 16:10
Salut à tous,
j'ai galèré un peu pour utiliser la fonction Hasmorepages. J'ai quand même réussi à imprimer plusieurs pages d'une lisbox. L'astuce consistant à sauvegarder les compteurs de boucles dans des variables qui, quand la routine est rappelée par la fonction HasmorePages recharge les compteurs. Je vous joins mon bout de code sachant qu'avant l'appel de la routine d'impression deux variables sont initialisées, à savoir:
pageencours = 0
CompteurItem = 0
impression.Print()


Bon courage !!!!


Private Sub impression_page(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

' routine d'impression a propremment dit


Dim TailleMax, HauteurLigne As Single
Dim LigneParpage As Int32
Dim Police As New Font("Arial", 12)
Dim Xpos As Single = 50.0F
Dim Ypos As Single
Dim Yposhaut As Single = 50.0F
Dim CompteurLigne As Int32
Dim cmpt As Int32
HauteurLigne = e.Graphics.MeasureString("E", Police).Height
LigneParpage = CInt(e.MarginBounds.Height / HauteurLigne) - 1

For Each Element As Object In ListBox1.Items
TailleMax = CSng(IIf(TailleMax < e.Graphics.MeasureString(Element.ToString, Police).Width, e.Graphics.MeasureString(Element.ToString, Police).Width, TailleMax))
Next
Ypos = Yposhaut
e.Graphics.DrawString("AAAAAAA - Impression de la sélection", Police, Brushes.Black, Xpos, Ypos)
Yposhaut = 100
CompteurLigne = 0
pageencours = pageencours + 1

For cmpt = CompteurItem To ListBox1.Items.Count - 1
Ypos = CompteurLigne * HauteurLigne + Yposhaut
affichage = cmpt.ToString + " - " + ListBox1.Items(cmpt).ToString
e.Graphics.DrawString(affichage, Police, Brushes.Black, Xpos, Ypos)

CompteurLigne = CompteurLigne + 1
If CompteurLigne > LigneParpage Then
CompteurLigne = 0
e.HasMorePages = True
CompteurItem = cmpt + 1
Exit For
Else
e.HasMorePages = False
End If


Next



End Sub
0
Rejoignez-nous