Meilleur auteur de réponses
Peut-on sauvegarder et imprimer le contenu de textboxes avec VB 2010 Express

Question
-
Bonjour à tous
Pour information, je me suis mis récemment à étudier la programmation avec VB 2010 Express.
Je viens de terminer un petit programme de calculs constitué de nombreux labels et textboxes sur plusieurs forms. Je cherche à ce que l'utilisateur puisse sauvegarder chaque condition étudiée et imprimer les contenus de ces contrôles.
Aprés de nombreuses recherches en tentatives d'impression et d'exportations vers les cellules d'un fichier Excel, je n'ai réussi qu'à pouvoir imprimer le texte d'un seul textbox. Je ne souhaite pas utiliser les richtextboxes qui ne conviennent pas à ce projet.
Question: VB 2010 Express autorise-t-il cette démarche?
Merci par avance pour vos réponses.
vendredi 23 septembre 2011 10:41
Réponses
-
Bonjour à tous
Finalement, l'impression Bitmap ne correspondait pas à mon projet. Après recherches, j'ai enfin utilisé la série de codes qui suivents qui peut intéresser des débutants comme moi. Ils permettent de sauvegarder le projet, de l'ouvrir pour créer un autre projet et sauvegarder ce dernier à son tour, de les imprimer. L'impression est très simpliste et je cherche maintenant à pouvoir sauvegarder le rapport imprimé sous forme de fichier texte ou autre.
Ci-dessous les codes en trois parties, en espérant que je n'ai rien oublié.
Créer un Form, y placer trois labels "Nom Compagnie:", "Nom Transporteur:", "Voyage Numéro:", puis trois textboxes (renommées respectivement "txtCompagnie", "txtTransp" et "txtVoyNb", plus un boutton.
'1) OUVRIR UN FICHIER Private Sub mnuOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpenFile.Click dlgOpen.InitialDirectory = "C:\ProjetX\Dossier" If dlgOpen.ShowDialog() = Windows.Forms.DialogResult.OK Then Dim fleProjetX As FileStream = New FileStream(dlgOpen.FileName, FileMode.Open, FileAccess.Read, FileShare.Read) Dim ProjetXReader As BinaryReader = New BinaryReader(fleRoro) Try txtCompanie.Text = ProjetXReader.ReadString() txtNomTransp.Text = ProjetXReader.ReadString() txtVoyNr.Text = ProjetXReader.ReadString() Finally ProjetXReader.Close() fleProjetX.Close() End Try End If End Sub '2) SAUVEGARDER UN FICHIER Private Sub mnuSaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveFile.Click ' Chiffre pour créer incrémentalement les spus-dossiers par leur nom Dim Incremental As Integer ' Vérifie le dossier ci-dessus. S'il existe, ne pas le créer, sinon le créer. Dim FolderName As String = "C:\Projet X\Sous-Dossier" Dim FolderInformation As DirectoryInfo = Directory.CreateDirectory(FolderName) ' Obtenir la liste des sous-dossiers si il y en a dans le dossier. Dim DataFile() As FileInfo = FolderInformation.GetFiles() ' S'il n'y a aucun sous-dossier dans le dossier, Prêt pour créer le premier. If DataFile.Length = 0 Then ' Prêt pour 'afficher dans la Save Dialog Box dlgSave.FileName = Incremental.ToString() & ".sav" End If ' Affiche la Save Dialog Box dans le dossier créé ci-dessus. dlgSave.InitialDirectory = FolderInformation.FullName ' Vérifie si l'utilisateur a clicqué OK après affichage de la Save Dialog Box. If dlgSave.ShowDialog() = Windows.Forms.DialogResult.OK Then ' Crée un nouveau fichier utilisant le nom de la Save Dialog Box Dim fleProjetX As FileStream = New FileStream(dlgSave.FileName, FileMode.Create, FileAccess.Write, FileShare.Write) Dim bnrProjetX As BinaryWriter = New BinaryWriter(fleRoro) Try bnrProjetX.Write(txtCompanie.Text) bnrProjetX.Write(txtNomTransp.Text) bnrProjetX.Write(txtVoyNr.Text) Finally bnrProjetX.Close() fleProjetX.Close() End Try End If End Sub '3) IPRIMER LE FICHIER Private Sub docPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docPrint.PrintPage Dim fntString As Font = New Font("Tomaha", 8, FontStyle.Regular) e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 60, 20) e.Graphics.DrawLine(New Pen(Color.Black, 1), 60, 40, 710, 40) 'Titre strDisplay = "RAPPORT DE VOYAGE" fntString = New Font("Tomaha", 14, FontStyle.Bold) e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 60, 80) 'Descriptif du projet fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString("Nom Compagnie:", fntString, Brushes.Black, 60, 120) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString(txtCompanie.Text, fntString, Brushes.Black, 360, 120) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString("Nom Transporteur:", fntString, Brushes.Black, 60, 140) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString(txtTransp.Text, fntString, Brushes.Black, 360, 140) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString("Voyage Numéro", fntString, Brushes.Black, 60, 160) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString(txtVoyNb.Text, fntString, Brushes.Black, 360, 140) End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click If dlgPrint.ShowDialog() = Windows.Forms.DialogResult.OK Then Try dlgPreview.Document = docPrint dlgPageSetup.PageSettings = docPrint.DefaultPageSettings If dlgPageSetup.ShowDialog() = Windows.Forms.DialogResult.OK Then docPrint.DefaultPageSettings = dlgPageSetup.PageSettings dlgPreview.ShowDialog() End If Catch ex As Exception MsgBox("L'impression a échoué" & vbCrLf) End Try docPrint.Print() End If End Sub
- Marqué comme réponse Ciprian Duduiala jeudi 27 octobre 2011 11:31
lundi 10 octobre 2011 18:00
Toutes les réponses
-
Bonjour vous pouvez utiliser comme base le code suivant :
private Sub PrintDocument(graphics As Graphics, bounds As Rectangle, documentContainer As Control) Dim bitmap As New Bitmap(documentContainer.Width, documentContainer.Height) documentContainer.DrawToBitmap(bitmap, New Rectangle(0, 0, bitmap.Width, bitmap.Height)) Dim target = New Rectangle(0, 0, bounds.Width, bounds.Height) graphics.PageUnit = GraphicsUnit.Display graphics.DrawImage(bitmap, target) End Sub
En suite, si vous utilisez un objet PrintDocument (Soit PrintDocument1) vous inserez le code suivant dans son handler PrintPage :
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage PrintDocument(e.Graphics, e.MarginBounds, Panel1) End Sub
Avec Panel1 comme container de l'ensemble de vos contrôles, ça peut être n'importe quel Container.Désolé s'il y a un manque de clarté car je ne sais pas coder en VB .NET.
- Proposé comme réponse Ould Mourad jeudi 29 septembre 2011 14:40
vendredi 23 septembre 2011 12:25 -
Merci beaucoup pour votre réponse rapide. Je n'avais pas pensé à la formule Bitmap. L'impression du projet fonctionne donc très bien aux dimensions près de la page qu'il reste à régler.
vendredi 23 septembre 2011 16:20 -
C'est bien. Pour ajuster les dimensions, vous devez jouez sur les dimensions du rectangle "target" de la solution.
Si vous conciderez que votre problème est résolu n'oublier pas de proposer la solution comme réponse.
Bon week-end.
- Modifié Ould Mourad vendredi 23 septembre 2011 23:36
vendredi 23 septembre 2011 23:33 -
Bonjour à tous
Finalement, l'impression Bitmap ne correspondait pas à mon projet. Après recherches, j'ai enfin utilisé la série de codes qui suivents qui peut intéresser des débutants comme moi. Ils permettent de sauvegarder le projet, de l'ouvrir pour créer un autre projet et sauvegarder ce dernier à son tour, de les imprimer. L'impression est très simpliste et je cherche maintenant à pouvoir sauvegarder le rapport imprimé sous forme de fichier texte ou autre.
Ci-dessous les codes en trois parties, en espérant que je n'ai rien oublié.
Créer un Form, y placer trois labels "Nom Compagnie:", "Nom Transporteur:", "Voyage Numéro:", puis trois textboxes (renommées respectivement "txtCompagnie", "txtTransp" et "txtVoyNb", plus un boutton.
'1) OUVRIR UN FICHIER Private Sub mnuOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpenFile.Click dlgOpen.InitialDirectory = "C:\ProjetX\Dossier" If dlgOpen.ShowDialog() = Windows.Forms.DialogResult.OK Then Dim fleProjetX As FileStream = New FileStream(dlgOpen.FileName, FileMode.Open, FileAccess.Read, FileShare.Read) Dim ProjetXReader As BinaryReader = New BinaryReader(fleRoro) Try txtCompanie.Text = ProjetXReader.ReadString() txtNomTransp.Text = ProjetXReader.ReadString() txtVoyNr.Text = ProjetXReader.ReadString() Finally ProjetXReader.Close() fleProjetX.Close() End Try End If End Sub '2) SAUVEGARDER UN FICHIER Private Sub mnuSaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveFile.Click ' Chiffre pour créer incrémentalement les spus-dossiers par leur nom Dim Incremental As Integer ' Vérifie le dossier ci-dessus. S'il existe, ne pas le créer, sinon le créer. Dim FolderName As String = "C:\Projet X\Sous-Dossier" Dim FolderInformation As DirectoryInfo = Directory.CreateDirectory(FolderName) ' Obtenir la liste des sous-dossiers si il y en a dans le dossier. Dim DataFile() As FileInfo = FolderInformation.GetFiles() ' S'il n'y a aucun sous-dossier dans le dossier, Prêt pour créer le premier. If DataFile.Length = 0 Then ' Prêt pour 'afficher dans la Save Dialog Box dlgSave.FileName = Incremental.ToString() & ".sav" End If ' Affiche la Save Dialog Box dans le dossier créé ci-dessus. dlgSave.InitialDirectory = FolderInformation.FullName ' Vérifie si l'utilisateur a clicqué OK après affichage de la Save Dialog Box. If dlgSave.ShowDialog() = Windows.Forms.DialogResult.OK Then ' Crée un nouveau fichier utilisant le nom de la Save Dialog Box Dim fleProjetX As FileStream = New FileStream(dlgSave.FileName, FileMode.Create, FileAccess.Write, FileShare.Write) Dim bnrProjetX As BinaryWriter = New BinaryWriter(fleRoro) Try bnrProjetX.Write(txtCompanie.Text) bnrProjetX.Write(txtNomTransp.Text) bnrProjetX.Write(txtVoyNr.Text) Finally bnrProjetX.Close() fleProjetX.Close() End Try End If End Sub '3) IPRIMER LE FICHIER Private Sub docPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docPrint.PrintPage Dim fntString As Font = New Font("Tomaha", 8, FontStyle.Regular) e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 60, 20) e.Graphics.DrawLine(New Pen(Color.Black, 1), 60, 40, 710, 40) 'Titre strDisplay = "RAPPORT DE VOYAGE" fntString = New Font("Tomaha", 14, FontStyle.Bold) e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 60, 80) 'Descriptif du projet fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString("Nom Compagnie:", fntString, Brushes.Black, 60, 120) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString(txtCompanie.Text, fntString, Brushes.Black, 360, 120) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString("Nom Transporteur:", fntString, Brushes.Black, 60, 140) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString(txtTransp.Text, fntString, Brushes.Black, 360, 140) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString("Voyage Numéro", fntString, Brushes.Black, 60, 160) fntString = New Font("Tomaha", 10, FontStyle.Regular) e.Graphics.DrawString(txtVoyNb.Text, fntString, Brushes.Black, 360, 140) End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click If dlgPrint.ShowDialog() = Windows.Forms.DialogResult.OK Then Try dlgPreview.Document = docPrint dlgPageSetup.PageSettings = docPrint.DefaultPageSettings If dlgPageSetup.ShowDialog() = Windows.Forms.DialogResult.OK Then docPrint.DefaultPageSettings = dlgPageSetup.PageSettings dlgPreview.ShowDialog() End If Catch ex As Exception MsgBox("L'impression a échoué" & vbCrLf) End Try docPrint.Print() End If End Sub
- Marqué comme réponse Ciprian Duduiala jeudi 27 octobre 2011 11:31
lundi 10 octobre 2011 18:00 -
Bonjour,
Merci d’avoir partagé avec nous votre solution.
Bonne journée,
Cipri
Ciprian DUDUIALA, MSFT  
•Nous vous prions de considérer que dans le cadre de ce forum on n’offre pas de support technique et aucune garantie de la part de Microsoft ne peut être offerte.
jeudi 27 octobre 2011 11:31