Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13
  1. #1

    [VB.NET]Stampante non torna a capo con txtBox

    Mi scuso con i moderatori per aver riproposto questo topic ma purtroppo sono nei guai seri.
    non riesco a completare questo progetto e purtroppo lo dovevo consegnare oggi.
    Ho bisogno di qualcuno che mi da una mano.
    Vi prego di cuore di aiutarmi a scrivere il codice con i passi di Bryanjar.
    Ripropongo il mio problema.
    Quando stampo e il contenuto della txtBox e maggiore della lunghezza del foglio la stampante non va a capo e quindi non mi stampa tutta la stringa del txBox.
    come devo fare?
    Bryanjar mi ha suggerito queste soluzioni solo che non so costruirle.
    qualcuno sa farlo
    vi ringrazio


    Brianjar:
    Ti devi costruire una funzione di parsing delle stringe contenute
    nel textBox.
    Una volta che stabilisci quanti caratteri nR per riga devi stampare
    fai così :

    1) Rimpiazza (con il metodo Replace) tutti i CRLF eventualmente
    presenti nella stringa textbox con uno spazio (copiala in una nuova stringa).
    2) Estrai (con Substring) la prima riga di nR caratteri
    3) Se l'ultimo carattere non è spazio ripeti il passo 2 con nR
    decrementato di uno (usa una variabile che aumenti ad ogni tentativo.)
    4) Se l'ultimo carattere è uno spazio allora aggiungi la prima riga
    alla stringa da stampare. Decrementa la stringa della textbox.
    5) ripeti il tutto fino a quando la stringa della text non è
    vuota ed hai aggiunto tutti i pezzi alla stringa da stampare.
    La funzione dovrebbe essere invocata in questo punto :


    vi ricordo che il codice è


    code:--------------------------------------------------------------------------------
    Public Class Form2

    Imports System.IO
    Imports System.Drawing.Printing

    Public Class Form3

    Private printFont As Font
    Private streamToPrint As StreamReader
    Private Shared filePath As String

    ' Print the file.
    Private Sub Printing()
    Try
    streamToPrint = New StreamReader("C:\App.txt")
    Try
    printFont = New Font("Arial", 10)
    Dim pd As New PrintDocument()
    AddHandler pd.PrintPage, AddressOf pd_PrintPage
    ' Print the document.
    pd.Print()
    Finally
    streamToPrint.Close()
    End Try
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub

    ' The PrintPage event is raised for each page _
    to be printed.
    Private Sub pd_PrintPage(ByVal sender As Object, _
    ByVal ev As PrintPageEventArgs)

    Dim linesPerPage As Single = 0
    Dim yPos As Single = 0
    Dim count As Integer = 0
    Dim leftMargin As Single = ev.MarginBounds.Left
    Dim topMargin As Single = ev.MarginBounds.Top
    Dim line As String = Nothing

    ' Calculate the number of lines per page.
    linesPerPage = _
    ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

    ' Iterate over the file, printing each line.
    While count < linesPerPage
    line = streamToPrint.ReadLine()
    If line Is Nothing Then
    Exit While
    End If
    yPos = _
    topMargin + count * printFont.GetHeight(ev.Graphics)
    ev.Graphics.DrawString(line, printFont, Brushes.Black, _
    leftMargin, yPos, New StringFormat())
    count += 1
    End While

    ' If more lines exist, print another page.
    If Not (line Is Nothing) Then
    ev.HasMorePages = True
    Else
    ev.HasMorePages = False
    End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim fFile As StreamWriter
    Dim strDoc As String = ""

    strDoc = strDoc + TextBox21.Text + vbCrLf + vbCrLf
    strDoc = strDoc + TextBox24.Text + vbCrLf + vbCrLf
    strDoc = strDoc + TextBox22.Text + vbCrLf + vbCrLf
    strDoc = strDoc + TextBox23.Text + vbCrLf + vbCrLf + vbCrLf


    strDoc = strDoc + TextBox1.Text
    strDoc = strDoc + " " + TextBox11.Text + vbCrLf + vbCrLf

    strDoc = strDoc + TextBox2.Text
    strDoc = strDoc + " " + TextBox12.Text + vbCrLf + vbCrLf

    strDoc = strDoc + TextBox3.Text
    strDoc = strDoc + " " + TextBox13.Text + vbCrLf + vbCrLf

    strDoc = strDoc + TextBox4.Text
    strDoc = strDoc + " " + TextBox14.Text + vbCrLf + vbCrLf

    strDoc = strDoc + TextBox5.Text
    strDoc = strDoc + TextBox15.Text + vbCrLf + vbCrLf

    strDoc = strDoc + TextBox6.Text
    strDoc = strDoc + " " + TextBox16.Text + vbCrLf + vbCrLf

    strDoc = strDoc + TextBox7.Text
    strDoc = strDoc + " " + TextBox17.Text + vbCrLf + vbCrLf

    strDoc = strDoc + TextBox8.Text
    strDoc = strDoc + " " + TextBox18.Text + vbCrLf + vbCrLf

    strDoc = strDoc + TextBox9.Text
    strDoc = strDoc + " " + TextBox19.Text + vbCrLf + vbCrLf

    strDoc = strDoc + TextBox10.Text
    strDoc = strDoc + " " + TextBox20.Text + vbCrLf + vbCrLf



    fFile = File.CreateText("C:\App.txt")
    fFile.WriteLine(strDoc)
    fFile.Close()

    Printing()
    File.Delete("C:\App.txt")
    End Sub
    --------------------------------------------------------------------------------

    raga mi date qusto aiuto per favore
    io non sono tanto bravo sto imparando.
    scusatemi
    ringrazio tutti quelli che mi stanno aiutando in particolare ladyblu e brianjar

  2. #2
    mi dispiace ma non ho mai stampato niente in questo modo... ma solo attraverso report di crystal...
    I database... la mia passione + o -

  3. #3
    non preoccuparti spero che qualcuno mi aiuterà

  4. #4
    Raga aiutatemi! Anche con i crystal reports va bene!
    Basta che riesco a stampare il contenuto dei textbox e riesca ad andare da capo automaticamente!

    Se potete datemi qualche esempio di codice!
    Grazie anticipatamente!

  5. #5
    Mi aiutate per favore!

  6. #6
    Utente di HTML.it L'avatar di Brainjar
    Registrato dal
    Nov 2004
    Messaggi
    1,162
    Ho scritto velocemente la funzione di parsing.
    Ho utilizzato le funzioni della componente
    Microsoft.VisualBasic per fare prima.

    Ragion per cui devi includere questa camponente in testa al
    progetto con :

    Imports Microsoft.VisualBasic

    codice:
    Public Function ParseTextBox(RowLen As Integer, RowText As String ) As String
    	
    	Dim sRet, sToken As String
    	Dim nC As Integer
    	Dim sCR as string = Microsoft.VisualBasic.Constants.vbCrLf 
    
    	Dim mRowText As String
    	
    	' Rimpiazza tutti i CRLF con degli spazi
    	' --------------------------------------
    	mRowText = Microsoft.VisualBasic.Replace(RowText, sCR," ") 
    	sRet = ""
    	nC = 0  
    	
    	Do While Len(mRowText) > 0
    
    		' Se la lunghezza dela strimga rimanente è minore
    		' della larghezza massima, aggiungo la romanenza ed esco 
    		If Microsoft.VisualBasic.Len(mRowText) < RowLen Then
    			sRet = sRet + sCR + mRowText
    			Exit Do
    		End If 
    
    		if RowLen > nC then 				
    			sToken =Microsoft.VisualBasic.Mid(mRowText,1,(RowLen - nC))
    		Else
    			exit do 
    		End if
    
    		If Microsoft.VisualBasic.Right(sToken,1) <> " " Then
    			nC = nC + 1
    		Else
    			sRet = sRet + sCR + sToken
    			mRowText = Microsoft.VisualBasic.Mid(mRowText, (RowLen - nC))
    			nC = 0
    		End If
    	Loop
    	
    	Return sRet
    
    End Function
    La funzione la chiami così :

    codice:
     
    ' Cambia il 40 con la larghezza, in caratteri, che vuoi 
    ' al massimo per riga
    strDoc = strDoc + ParseTextBox(40, TextBox21.Text)  + vbCrLf + vbCrLf
    Ho provato la funzione e ha un risultato soddisfacente.
    Ciao,
    Brainjar.
    Ciao, Brainjar

  7. #7
    Bryanjar sei un genio. Grazie di cuore alla fine sempre gli stessi restano i migliori e aiutano davvero le persone .
    grazie

    Però c'è solo una cosa. no mi segnala errori solo che va in blocco.
    non so perchè.
    cioè si incanta la form

    questo è il codice completo compreso il tuo gia sostituito
    codice:
    Imports System.IO
    Imports System.Drawing.Printing
    Imports Microsoft.VisualBasic
    Public Class Form3
        Inherits System.Windows.Forms.Form
        Private printFont As Font
        Private streamToPrint As StreamReader
        Private Shared filePath As String
    
        ' Print the file. 
        Private Sub Printing()
            Try
                streamToPrint = New StreamReader("C:\App.txt")
                Try
                    printFont = New Font("Arial", 10)
                    Dim pd As New PrintDocument
                    AddHandler pd.PrintPage, AddressOf pd_PrintPage
                    ' Print the document. 
                    pd.Print()
                Finally
                    streamToPrint.Close()
                End Try
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        'The PrintPage event is raised for each page _ to be printed. 
        Private Sub pd_PrintPage(ByVal sender As Object, _
        ByVal ev As PrintPageEventArgs)
    
            Dim linesPerPage As Single = 0
            Dim yPos As Single = 0
            Dim count As Integer = 0
            Dim leftMargin As Single = ev.MarginBounds.Left
            Dim topMargin As Single = ev.MarginBounds.Top
            Dim line As String = Nothing
    
            ' Calculate the number of lines per page. 
            linesPerPage = _
            ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
    
            ' Iterate over the file, printing each line. 
            While count < linesPerPage
                line = streamToPrint.ReadLine()
                If line Is Nothing Then
                    Exit While
                End If
                yPos = _
                topMargin + count * printFont.GetHeight(ev.Graphics)
                ev.Graphics.DrawString(line, printFont, Brushes.Black, _
                leftMargin, yPos, New StringFormat)
                count += 1
            End While
    
            ' If more lines exist, print another page. 
            If Not (line Is Nothing) Then
                ev.HasMorePages = True
            Else
                ev.HasMorePages = False
            End If
        End Sub
        ' The PrintPage event is raised for each page _to be printed.
        Public Function ParseTextBox(ByVal RowLen As Integer, ByVal RowText As String) As String
    
            Dim sRet, sToken As String
            Dim nC As Integer
            Dim sCR As String = Microsoft.VisualBasic.Constants.vbCrLf
    
            Dim mRowText As String
    
            ' Rimpiazza tutti i CRLF con degli spazi
            ' --------------------------------------
            mRowText = Microsoft.VisualBasic.Replace(RowText, sCR, " ")
            sRet = ""
            nC = 0
    
            Do While Len(mRowText) > 0
    
                ' Se la lunghezza dela strimga rimanente è minore
                ' della larghezza massima, aggiungo la rimanenza ed esco 
                If Microsoft.VisualBasic.Len(mRowText) < RowLen Then
                    sRet = sRet + sCR + mRowText
                    Exit Do
                End If
    
                If RowLen > nC Then
                    sToken = Microsoft.VisualBasic.Mid(mRowText, 1, (RowLen - nC))
                Else
                    Exit Do
                End If
    
                If Microsoft.VisualBasic.Right(sToken, 1) <> " " Then
                    nC = nC + 1
                Else
                    sRet = sRet + sCR + sToken
                    mRowText = Microsoft.VisualBasic.Mid(mRowText, (RowLen - nC))
                    nC = 0
                End If
            Loop
    
            Return sRet
    
        End Function
    
    
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
    
        Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    
    
            Dim fFile As StreamWriter
            Dim strDoc As String = ""
    
            strDoc = strDoc + TextBox21.Text + vbCrLf + vbCrLf
            strDoc = strDoc + TextBox24.Text + vbCrLf + vbCrLf
            strDoc = strDoc + TextBox22.Text + vbCrLf + vbCrLf
    
    
            strDoc = strDoc + ParseTextBox(120, TextBox21.Text) + vbCrLf + vbCrLf
    
    
    
    
    
    
    
            strDoc = strDoc + ParseTextBox(40, TextBox1.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + "     " + TextBox11.Text + vbCrLf + vbCrLf
    
            strDoc = strDoc + ParseTextBox(120, TextBox2.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + "     " + TextBox12.Text + vbCrLf + vbCrLf
    
            strDoc = strDoc + ParseTextBox(120, TextBox3.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + "     " + TextBox13.Text + vbCrLf + vbCrLf
    
            strDoc = strDoc + ParseTextBox(120, TextBox4.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + "     " + TextBox14.Text + vbCrLf + vbCrLf
    
            strDoc = strDoc + ParseTextBox(120, TextBox5.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + TextBox15.Text + vbCrLf + vbCrLf
    
            strDoc = strDoc + ParseTextBox(120, TextBox6.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + "     " + TextBox16.Text + vbCrLf + vbCrLf
    
            strDoc = strDoc + ParseTextBox(120, TextBox7.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + "     " + TextBox17.Text + vbCrLf + vbCrLf
    
            strDoc = strDoc + ParseTextBox(120, TextBox8.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + "     " + TextBox18.Text + vbCrLf + vbCrLf
    
            strDoc = strDoc + ParseTextBox(120, TextBox9.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + "     " + TextBox19.Text + vbCrLf + vbCrLf
    
            strDoc = strDoc + ParseTextBox(120, TextBox10.Text) + vbCrLf + vbCrLf
            strDoc = strDoc + "     " + TextBox20.Text + vbCrLf + vbCrLf
    
    
    
            fFile = File.CreateText("C:\App.txt")
            fFile.WriteLine(strDoc)
            fFile.Close()
    
            Printing()
            File.Delete("C:\App.txt")
    
    
        End Sub
    
    
    
    End Class
    sei puoi dacci un occhiata quando non hai nulla da fare. di sicuro ci sarà qualche errore banale.
    mahhhh
    grazieeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeeee

  8. #8
    bryanjar ho capito l'errore forse
    secondo me è 40 cioè la larghezza della parola perchè se imposto ad esempio ad 80 il textbox1 che contiene una frase abbastanza lunga va in blocco.
    invece se imposto ad 80 il textbox 2 che contiene però meno di 80 caratteri stampa tranquillamente. come dobbiamo fare?

  9. #9
    Utente di HTML.it L'avatar di Brainjar
    Registrato dal
    Nov 2004
    Messaggi
    1,162
    Sostituisci il blocco Do ..... While con questo :

    codice:
    Do While Len(mRowText) > 0
    	If Microsoft.VisualBasic.Len(mRowText) < RowLen Then
     	    sRet = sRet + sCR + mRowText
    	    Exit Do
    	End If 
    
    	sToken =Microsoft.VisualBasic.Mid(mRowText,1,RowLen)
    	sRet = sRet + sCR + sToken
    	mRowText = Microsoft.VisualBasic.Mid(mRowText,RowLen + 1)
    Loop
    Con questa modifica, però, ti andrà a capo esattamente al
    carattere specificato.

    Ciao,
    Brainjar.

    PS: Il mio nick è BRAINJAR non Bryanjar o Brianjar.
    Ciao, Brainjar

  10. #10
    ok funziona finalmente ma non c'è un modo per andare da capo a fine parola. aiutami solo in questo e non ti darò più fastidio.
    almeno finisco sto cavolo di prog.
    è da 10 giorni che ci sbatto sopra.
    io ho trovato anche questo cod
    vedi se ti puo essere utile
    grazie ancora
    codice:
    Dim Font As New Font("Arial", 15)
    
            'altezza linea
            Dim LineHeight As Single = Font.GetHeight(e.Graphics)
    
            ' variabili posizione del testo nella pagina
            Dim x As Single = e.MarginBounds.Left
            Dim y As Single = e.MarginBounds.Top
    
            'soglia per l'interruzione della parola
            Dim Threshold As Integer = 200
    
            'recupero il testo
            Dim TextToPrint As New System.Text.StringBuilder(txtData.Text)
    
            ' linea singola da stampare
            Dim Line As New System.Text.StringBuilder()
    
            Do
                'prendo un carattere e lo aggiungo alla linea corrente
                Line.Append(TextToPrint.Chars(0))
                TextToPrint = TextToPrint.Remove(0, 1)
    
                ' controllo se c'e un'interruzione di parola
                If Line.Chars(Line.Length - 1) = " " Then
    
                    ' decido se stampare la linea o aggiungere una parola
                    Dim LineString As String = Line.ToString()
                    If e.Graphics.MeasureString(LineString, Font).Width > (e.PageBounds.Width - Threshold) Then
                        e.Graphics.DrawString(LineString, Font, Brushes.Black, x, y)
                        y += LineHeight
                        Line = New System.Text.StringBuilder()
                    End If
    
                End If
            Loop While TextToPrint.Length > 0
    
            ' stampo l'ultima linea.
            e.Graphics.DrawString(Line.ToString(), Font, Brushes.Black, x, y)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.