Results 1 to 2 of 2

Thread: Hangs when Paste Button is pressed!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2015
    Location
    Hastings, UK
    Posts
    137

    Hangs when Paste Button is pressed!

    I am working on a solution which uses PDFSharp to print greetings on A4 paper.
    When the paste button is clicked the Messagebox shows the verse with the added VBCrLf in it, so the paste code is OK, but the it runs the 'txbVerse_TextChanged' which I think it is where it hangs.

    Here is the code for the form:
    Code:
    Imports System.Data.SqlClient
    Imports System.Drawing
    Imports PdfSharp
    Imports PdfSharp.Drawing
    Imports PdfSharp.Pdf
    Imports PdfSharp.Drawing.Layout
    Enum pageOrientation
        Landscape
        Portrait
    End Enum
    Enum pagesize
        A4
        A5
    End Enum
    
    Public Class PrintFrm
        Private Const Lf As String = vbCrLf
        Private connectionString As String = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
        Public Property dt As Object
    
        Private Sub PrintFrm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim Print As New PrintFrm
            Me.TopMost = True
            Me.WindowState = FormWindowState.Maximized
            'TODO: This line of code loads data into the 'Verses_FindDataSet4.TxtColor' table. You can move, or remove it, as needed.
            Me.TxtColorTableAdapter.Fill(Me.Verses_FindDataSet4.TxtColor)
            'TODO: This line of code loads data into the 'Verses_FindDataSet3.CSize' table. You can move, or remove it, as needed.
            Me.CSizeTableAdapter.Fill(Me.Verses_FindDataSet3.CSize)
    
            For Each oFont As FontFamily In FontFamily.Families
                cboFont.Items.Add(oFont.Name)
            Next
    
    
    
    
        End Sub
        Private Sub btnPasteVerse_Click(sender As Object, e As EventArgs) Handles btnPasteVerse.Click
            ' Determine if there is any text in the Clipboard to paste into the text box.
            If Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) = True Then
                ' Determine if any text is selected in the text box.
                If txbVerse.SelectionLength > 0 Then
                    ' Ask user if they want to paste over currently selected text.
                    If MessageBox.Show("Do you want to paste over current selection?",
                        "Cut Example", MessageBoxButtons.YesNo) = DialogResult.No Then
                        ' Move selection to the point after the current selection and paste.
                        txbVerse.SelectionStart = txbVerse.SelectionStart +
                            txbVerse.SelectionLength
                    End If
                End If
                ' Paste current text in Clipboard into text box.
                txbVerse.Paste()
            End If
        End Sub
    
        Private Sub txbVerse_TextChanged(sender As Object, e As EventArgs) Handles txbVerse.TextChanged
            Dim FSize As Single
            FSize = 24
            txbVerse.Font = New Font(cboFont.Text, FSize, FontStyle.Regular)
            MessageBox.Show(Replace(txbVerse.Text, Chr(13) & Chr(10), " VBCrLf "))
            Dim cboVal1 As String
            Dim lstVal2 As Integer
            Dim cboVal3 As String
            Dim boxX As Integer
            Dim boxY As Integer
            Dim cellw As Integer
            Dim cellh As Integer
            Dim sql As String = Nothing
            Dim sqlAdaptor As SqlDataAdapter
            Dim dt As New DataTable
            Dim CID As Integer
            Dim ort As String
            Dim Narrative As String
            Narrative = Nothing
            ort = Nothing
    
            Using connection As New SqlConnection(connectionString)
                connection.Open()
                Dim selectStatement = $"Select CID, BoxX, BoxY, Cellw, Cellh, Size, ort, Narrative From CSize Where CID = {cboCSize}"
                sqlAdaptor = New SqlDataAdapter(selectStatement, connection)
                dt = New DataTable()
                sqlAdaptor.Fill(dt)
                MessageBox.Show(Narrative, "Card Size")
            End Using
    
            Dim document As PdfDocument
            ' Create a new PDF document
            document = New PdfDocument()
            document.Info.Title = "Created with PDFsharp"
    
            ' Create an empty page
            Dim page As PdfPage = document.AddPage
            If ort = "L" Then
                page.Orientation = CType(pageOrientation.Landscape, PdfSharp.PageOrientation)
                page.Width = XUnit.FromMillimeter(297)
                page.Height = XUnit.FromMillimeter(210)
            Else
                page.Orientation = CType(pageOrientation.Portrait, PdfSharp.PageOrientation)
                page.Width = XUnit.FromMillimeter(210)
                page.Height = XUnit.FromMillimeter(297)
    
            End If
    
            ' Draw the text
            Dim Ftext As String = txbVerse.Text
            Dim Font As String = Nothing
    
            cboVal1 = CStr(cboFont.SelectedValue)
            lstVal2 = CInt(lstFSize.SelectedValue)
            cboVal3 = CStr(cboColor.SelectedValue)
    
            Dim gfx As XGraphics
            gfx = XGraphics.FromPdfPage(page)
            Dim Cfont As XFont = New XFont("French Script MT", 20, XFontStyle.Regular)
            Dim tf As XTextFormatter
            tf = New XTextFormatter(gfx)
            Dim FColor As String = cboVal3
            Dim rect As XRect
            rect = New XRect(boxX, boxY, cellw, cellh)
            gfx.DrawRectangle(XBrushes.SeaShell, rect)
            tf.Alignment = XParagraphAlignment.Center
            tf.DrawString(Ftext, Cfont, XBrushes.Blue, rect, XStringFormats.TopLeft)
    
            ' Save the document
            Dim filename As String = "verse.pdf"
            document.Save(filename)
    
            ' ...and start a viewer.
            Process.Start(filename)
        End Sub
    
        Private Sub btnClose3_Click(sender As Object, e As EventArgs) Handles btnClose3.Click
            Close()
        End Sub
    End Class
    Can anyone see why it hangs?
    Last edited by Rocky48; Jun 2nd, 2019 at 12:24 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Hangs when Paste Button is pressed!

    It's not for us to read your code and tell you where it hangs. That's what debugging is for. Run the code and step through it and then it will be obvious where it hangs... because it will hang. Code hanging is either an infinite loop or a method that doesn't return and either will be obvious if you do what you should have done already and debug your code. If you don't know how to debug using breakpoints, now would be the perfect time to learn. If you can't debug then you can't write software.

    Once you know where the issue is and what it is, if you don't know how to fix, THEN would be the time to ask a question. You can then provide ALL the relevant information, including what data was in use at the time, which you will also be able to determine from a proper debugging session.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width