الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيفية حساب عدد السطور في PrintDocument

بدأه iamsad في 26 ديسمبر 2011 · 11 رد · 792 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

حصلت على هذا الكود من msdn

  Dim linesPerPage As Integer = 0

    ' Sets the value of charactersOnPage to the number of characters 
    ' of stringToPrint that will fit within the bounds of the page.
    e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _
        StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

    ' Draws the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _
        e.MarginBounds, StringFormat.GenericTypographic)

    ' Remove the portion of the string that has been printed.
    stringToPrint = stringToPrint.Substring(charactersOnPage)

    ' Check to see if more pages are to be printed.
    e.HasMorePages = stringToPrint.Length > 0

وهو يقوم بحساب عدد الاسطر المطبوعة فيزيد عدد الصفحات بحسبها

مشكلتي هي ان الكود يقوم بالطباعة من ملف وانا اريد الطباعة من printdocument

فالسطر التالي من الكود هو محط اهتمامي

        e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black,  e.MarginBounds, StringFormat.GenericTypographic)

فالكلمة stringToPrint هي النص المراد طباعته فلو استطعت ان اضع في مكانه النص الموجود في printdocument ربما حلت المشكلة

ارجو المساعدة

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

#2

السلام عليكم ورحمه الله وبركاته

اخي يمكنك وضع الكود في الحدث PrintPage

.:: Wish to become better and better ::.

#3

نعم اخي لكن ما هي قيمة stringToPrint فلا بد من ان تكون مشحونة بقيمة ولابد ان تكون هذه القيمة هي ما يفترض طبعه من printdocument 1

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

#4

السلام عليكم ورحمه الله وبركاته

اخي بالفعل هذا هو النص المراد طباعته, لمزيد ارجو مراجعة الرابط الذي قرأته سابقا واذا واجهتك مشكلة ارجو ذكرها وذكر الربط معا ..

.:: Wish to become better and better ::.

#5

وعليكم السلام

اخي مشكلتي هي كالاتي

الكود السابق يقوم بطباعة نص وهو يحسب الاسطر ليزيد عدد الصفحات اذا زادت عن حجم الصفحة

في الكود السابق النص هو النص المفترض تخزينه في stringToPrint

اما النص الذي اريد طباعته هو كل النصوص المكتوبة في PrintDocument1_PrintPage event

باحداثيات مختلفة وهذا جزء من النصوص التي اريد طباعتها لتكتمل الصورة

  For Each ctrl In Me.TabControl1.TabPages(3).Controls
            If (TypeOf ctrl Is TextBox) Then
                If CType(ctrl, TextBox).Text.Length > 0 Then
                    C4 = C4 + 10
                End If

            End If
        Next
        If C4 > 0 Then
            e.Graphics.DrawString(Label51.Text, F2, b, 20, c)
            c = c + 30
        End If

        Dim ControlStack4 As New Stack(Of TextBox)
        For Each ctrl In Me.TabControl1.TabPages(3).Controls 'To handle issue of taborder' 
            If (TypeOf ctrl Is TextBox) Then
                ControlStack4.Push(ctrl)
            End If
        Next
        For Each ctrl In ControlStack4
            If (TypeOf ctrl Is TextBox) Then
                If CType(ctrl, TextBox).Text.Length > 0 Then
                    name = ctrl.Name
                    Dim labelName As String
                    labelName = "L" & Mid(name, 8)
                    Dim labelControl As Label
                    Dim controlsFound() As Control
                    controlsFound = Controls.Find(labelName, True)
                    If controlsFound.Length > 0 Then
                        labelControl = controlsFound(0)
                    End If
                    '========
                    Dim COMBONAME1 As String
                    COMBONAME1 = "CO" & Mid(name, 8)
                    Dim COMBOCONTROL1 As MaskedTextBox
                    Dim CONTROLFOUND1() As Control
                    CONTROLFOUND1 = Controls.Find(COMBONAME1, True)
                    If CONTROLFOUND1.Length > 0 Then
                        COMBOCONTROL1 = CONTROLFOUND1(0)
                    End If
                    '========
                    e.Graphics.DrawString(labelControl.Text, f, b, 5, c)
                    e.Graphics.DrawString(ctrl.Text, f, b, 250, c)
                    e.Graphics.DrawString(COMBOCONTROL1.Text, f, b, 400, c)
                    c = c + 30
                    e.Graphics.DrawLine(p, 8, c, 540, c)
                    c = c + 5
                End If
            End If

        Next

وهذا الرابط الذي فيه الـcode sample

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_fxmancli/html/362427f8-03d4-4826-b49f-60ab066ad322.htm

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

#6

السلام عليكم

لقد حصلت على كود فيه كل ما أريد تقربا من الرابط التالي

http://www.vb-helper.com/howto_net_print_long_text.html

إلا انني لم اعالج مشكلتي بعد

فأنا في الاصل اريد طباعة النص الموجود في print document والذي قد يطول وقد يقصر حسب الtxtboxes الممتلئة

في الفورم واذا طالت تزداد عدد الصفحات

ارجو لمن يريد المساعدة الاطلاع على الرابط والمثال المرفق

general - Copy.rar

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

#7

هل هناك طريقة لتخزين كل النص المطبوع من printdocument1_printpage فيي متغير نصي ومن ثم استخدامه في e.Graphics.MeasureString ؟

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

#8

ايها الاخوة لقد تجمدت الافكار عندي ولكن بعد ان اوشكت الوصول الى خط النهاية على ما اظن. عموما هذا اخر ماتوصلت اليه

Dim ControlStack3 As New Stack(Of ComboBox)
        For Each ctrl In Me.TabControl1.TabPages(2).Controls 'To handle issue of taborder' 
            If (TypeOf ctrl Is ComboBox) Then
                ControlStack3.Push(ctrl)
            End If
        Next
        Dim controlsFounds() As Control
        For Each ctrl In ControlStack3
            Dim labelControl As Label
            If (TypeOf ctrl Is ComboBox) Then
                If CType(ctrl, ComboBox).Text.Length > 0 Then

                    '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
                    '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
                    layout_rect = New RectangleF(e.MarginBounds.Left, ymin, e.MarginBounds.Width, e.MarginBounds.Bottom - ymin)

                    layout_rectT = New RectangleF(280, ymin, e.MarginBounds.Width, e.MarginBounds.Bottom - ymin)

                    '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
                    '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
                    name = ctrl.Name
                    Dim labelName As String
                    labelName = "Label" & Mid(name, 9)


                    controlsFounds = Controls.Find(labelName, True)
                    If controlsFounds.Length > 0 Then
                        labelControl = controlsFounds(0)

                    End If

                    'هذا هو الجزء الذي اعياني جدا
                    '#################################

                    text_size = e.Graphics.MeasureString( _
               labelControl.Text, f, _
               New SizeF(layout_rect.Width, layout_rect.Height), _
               string_format, characters_fitted, lines_filled)


                    text_size = e.Graphics.MeasureString( _
         ctrl.Text, f, _
         New SizeF(layout_rect.Width, layout_rect.Height), _
         string_format, characters_fitted, lines_filled)

                    e.Graphics.DrawString(labelControl.Text, f, b, layout_rect, string_format)

                    e.Graphics.DrawString(ctrl.Text, f, b, layout_rectT, string_format)

                    ymin += CInt(text_size.Height + 60)


                    '########################################




                End If
            End If
            If labelControl.Text <> "OTHER:" Then
                e.HasMorePages = True
            End If
        Next

مشكلتي الان هي ان النص الذي اطبعه اكبر من ان يطبع في صفحة واحدة لكن المتبقي من النص لايظهر في الصفحات التالية وانما فقط الصفحة الاولى مكررة

فما الذي يجعل ماتبقى من النص يطبع في الصفحة التالية هل هناك امر اخر يحتاج لضبط غير e.HasMorePages

ارجوكم المساعدة فقد بلغت حد علمي هنا

رجاءً

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

#9

السلام عليكم

لقد ادركت ان الصفحة تطبع مكررة لان الـprintpage event يقوم باعادة الكرة من البداية كل ما كان هناك نص زائد عن الصفحة فبالتالي احتاج لوسيلة لمسح ماطبع في المرة الاولى وعدم تكراره ثانية

تم تعديل هذه المشاركة بواسطة iamsad في 31 ديسمبر 2011 في 03:29

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

#10

لقد قمت بتعديل الكود وهو يعمل والحمد لله لكن فيه مشكلة واحدة

 'THIS STEP TO PREVENT THIS SECTION FROM BEING REPEATED
        'SO THE COLLECTIONS WILL BE FILLED ONCE 
        If ACCESS_3 = 1 Then

            Dim controlsFounds() As Control
            For Each ctrl In ControlStack3
                Dim labelControl As Label
                If (TypeOf ctrl Is ComboBox) Then
                    If CType(ctrl, ComboBox).Text.Length > 0 Then

                        name = ctrl.Name
                        Dim labelName As String
                        labelName = "Label" & Mid(name, 9)

                        controlsFounds = Controls.Find(labelName, True)
                        If controlsFounds.Length > 0 Then
                            labelControl = controlsFounds(0)

                            'coll3 asnd collt are collections
                            coll3.Add(labelControl)
                            collt3.Add(ctrl)

                        End If

                    End If
                End If

                'e.HasMorePages = True

            Next
            'ACCESS_3 += 1
        End If
        ' here iam closing this section
        If NO_PAGES = 1 Then
            c = 50
        End If
        Dim t1 As Integer
        t1 = coll3.Count

        e.Graphics.DrawString(t1, f, Brushes.Black, 120, 0)
        Do While coll3.Count > 0

            'Dim I As Integer = 1
            Dim L As Control
            Dim T As Control

            L = coll3.Item(1)
            T = collt3.Item(1)

            ' c as integer
            e.Graphics.DrawString(L.Text, f, b, 125, c)
            e.Graphics.DrawString(T.Text, f, b, 225, c)
            c += 60

المشكلة هي ان الـcollection coll3 تكون فارغة عندما يعود البرنامج في الصفحة التالية فلماذا لا تحفظ الـcollection ماتبقى منها ؟

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

#11

اخر تعديل للنص ومازلت انتظر المساعدة

        Dim ControlStack3 As New Stack(Of ComboBox)
        For Each ctrl In Me.TabControl1.TabPages(2).Controls 'To handle issue of taborder' 
            If (TypeOf ctrl Is ComboBox) Then
                ControlStack3.Push(ctrl)
            End If
        Next

        'THIS STEP TO PREVENT THIS SECTION FROM BEING REPEATED
        'SO THE COLLECTIONS WILL BE FILLED ONCE 
        If ACCESS_3 = True Then

            Dim controlsFounds() As Control
            For Each ctrl In ControlStack3
                Dim labelControl As Label
                If (TypeOf ctrl Is ComboBox) Then
                    If CType(ctrl, ComboBox).Text.Length > 0 Then

                        name = ctrl.Name
                        Dim labelName As String
                        labelName = "Label" & Mid(name, 9)

                        controlsFounds = Controls.Find(labelName, True)
                        If controlsFounds.Length > 0 Then
                            labelControl = controlsFounds(0)

                            'coll3 asnd collt are collections
                            coll3.Add(labelControl)
                            collt3.Add(ctrl)

                        End If

                    End If
                End If

            Next
        End If
        ACCESS_3 = False
        ' here iam closing this section
        If NO_PAGES = 1 Then
            c = 50
        End If
        Dim t1 As Integer
        t1 = coll3.Count

        e.Graphics.DrawString(t1, f, Brushes.Black, 120, 0)
        If INTER = 1 Then
            coll3 = SAV
        End If
        Dim CO As Integer
        Do While coll3.Count > 0

            e.Graphics.DrawString(coll3.Count, f, Brushes.Black, CO, 0)
            CO += 20
            'Dim CONVCOL As ParagraphInfo
            'CONVCOL = DirectCast(coll3(1), ParagraphInfo)

            'Dim I As Integer = 1
            Dim L As Control
            Dim T As Control

            L = coll3.Item(1)
            T = collt3.Item(1)

            ' c as integer
            e.Graphics.DrawString(L.Text, f, b, 125, c)
            e.Graphics.DrawString(T.Text, f, b, 225, c)
            c += 60

            'delete the first item so when loop is retun will find an other item in 
            'top of collections 
            collt3.Remove(1)
            coll3.Remove(1)

            If c > 720 Then

                Exit Do

            End If
        Loop

        SAV = coll3
        INTER += 1
        e.HasMorePages = (coll3.Count > 0)


        e.Graphics.DrawString(coll3.Count, f, Brushes.Black, 120, 720)

المشكلة الان ان الصفحة التالية لاتظهر ولكن عندما اغلق الPREVIEW ثم افتحه ثانية يطبع ما تبقى من النص في الصفحة الاولى

المسااااااااااااعدة

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

#12
   If ACCESS_3 = True Then
            If CHECK3 = 1 Then


                For Each ctrl In Me.TabControl1.TabPages(2).Controls
                    If (TypeOf ctrl Is ComboBox) Then
                        If CType(ctrl, ComboBox).Text.Length > 0 Then
                            C3 = C3 + 10
                        End If

                    End If
                Next
                If C3 > 0 Then
                    Dim fill1 As Rectangle = New Rectangle(130, c, 530, 25) 'create a new recatangle
                    Dim FmyBrushl22 As Brush = New Drawing.Drawing2D.LinearGradientBrush(fill1, Color.Silver, Color.DimGray, Drawing.Drawing2D.LinearGradientMode.Vertical)
                    e.Graphics.FillRectangle(FmyBrushl22, fill1)
                    e.Graphics.DrawRectangle(p, 130, c, 530, 25)

                    c = c + 2

                    e.Graphics.DrawString(Label39.Text, F2, wb, 150, c)
                    c = c + 30
                End If



                Dim ControlStack3 As New Stack(Of ComboBox)
                For Each ctrl In Me.TabControl1.TabPages(2).Controls 'To handle issue of taborder' 
                    If (TypeOf ctrl Is ComboBox) Then
                        ControlStack3.Push(ctrl)
                    End If
                Next

                'THIS STEP TO PREVENT THIS SECTION FROM BEING REPEATED
                'SO THE COLLECTIONS WILL BE FILLED ONCE 

                Dim controlsFounds() As Control
                For Each ctrl In ControlStack3
                    Dim labelControl As Label
                    If (TypeOf ctrl Is ComboBox) Then
                        If CType(ctrl, ComboBox).Text.Length > 0 Then
                            name = ctrl.Name
                            Dim labelName As String
                            labelName = "Label" & Mid(name, 9)

                            controlsFounds = Controls.Find(labelName, True)
                            If controlsFounds.Length > 0 Then
                                labelControl = controlsFounds(0)
                                coll3.Add(labelControl)
                                collt3.Add(ctrl)
                            End If
                        End If
                    End If
                Next
            End If
        End If
        ' here iam closing this section

        ACCESS_3 = False

        Do While coll3.Count > 0
            Dim L As Control
            Dim T As Control

            L = coll3.Item(1)
            T = collt3.Item(1)

            ' c as integer
            e.Graphics.DrawString(L.Text, f, b, 150, c)
            e.Graphics.DrawString(T.Text, f, b, 300, c)
            c += 30

            'delete the first item so when loop is retun will find an other item in 
            'top of collections 
            collt3.Remove(1)
            coll3.Remove(1)

            If c > 780 Then
                c = 20
                e.HasMorePages = True
                If CHECK4 Or CHECK5 Or CHECK6 Or CHECK7 Or CHECK8 > 0 Then
                    e.HasMorePages = True
                End If
                Return
                Exit Do

            End If
            e.Graphics.DrawString(c, f, b, 10, c)
        Loop

        'here i tacking the y dimention an -30 is to be sure that line will'nt be 
        'cut from the middle 



        e.Graphics.DrawString(coll3.Count, f, Brushes.Black, 120, 720)

مع تحياتي.....

يا أهل سوريا اعلنوها خلافة اسلامية لتكون الشام عقر دار المسلمين وخلاص ارض فلسطين

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…