The code has changed since I first posted and the error is in the same line as before on line 66.
The error says: 'Object reference not set to an instance of an object.' The line is shown below:
Code:
tf.DrawString(Ftext, New XFont(CFont, FSize, XFontStyle.Regular), XBrushes.Black, rect, XStringFormats.TopLeft)
Its not with reference to the color as I had other errors which needed eliminating before I tackled that problem.
I still need to know how to declare a variable to represent one of the 141 colors. The variable name for the color I am using is 'TxtColorValue'
Here is the complete form code:
Code:
Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Fonts
Imports PdfSharp.PageOrientation
Imports PdfSharp.PageSize
Imports PdfSharp.Pdf
Imports PdfSharp.Internal
Imports PdfSharp.Drawing.Layout
Imports System.Data.SqlClient
Enum pageOrientation
Landscape
Portrait
End Enum
Enum Pagesize
A4
A5
End Enum
Public Class PrintFrm
Private connectionString As String = "Data Source=DESKTOP-S7FRNAL\SQLEXPRESS;Initial Catalog=Verses_Find;Integrated Security=True"
Public Property dt As Object
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
MessageBox.Show(Replace(txbVerse.Text, Chr(13) & Chr(10), " VBCrLf "))
Dim boxX As Integer
Dim boxY As Integer
Dim cellw As Integer
Dim cellh As Integer
Dim ort As String = Nothing
Dim FSize As Integer
Dim CFont As String = Nothing
Dim TxtColorValue As String
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 gfx As XGraphics
gfx = XGraphics.FromPdfPage(page)
Dim tf As XTextFormatter
tf = New XTextFormatter(gfx)
Dim rect As XRect
rect = New XRect(boxX, boxY, cellw, cellh)
gfx.DrawRectangle(XBrushes.SeaShell, rect)
tf.Alignment = XParagraphAlignment.Center
tf.DrawString(Ftext, New XFont(CFont, FSize, XFontStyle.Regular), XBrushes.Black, 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 btnPaste_Click(sender As Object, e As EventArgs) Handles btnPaste.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 PrnForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ColorDataSet.TxtColor' table. You can move, or remove it, as needed.
Me.TxtColorTableAdapter.Fill(Me.ColorDataSet.TxtColor)
'TODO: This line of code loads data into the 'Verses_Find_Color_DataSet.TxtColor' table. You can move, or remove it, as needed.
Me.CSizeTableAdapter.Fill(Me.Verses_FindDataSet.CSize)
Dim Print As New PrintFrm
Me.TopMost = True
Me.WindowState = FormWindowState.Normal
For Each oFont As FontFamily In FontFamily.Families 'This line populates the font combo with the system installed fonts
cboFont.Items.Add(oFont.Name)
Next
End Sub
Private Sub cboCSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCSize.SelectedIndexChanged
Me.CSizeTableAdapter.Fill(Me.Verses_FindDataSet.CSize)
Dim cboCSize As Integer
Dim CSizeValue As Integer = cboCSize
End Sub
Private Sub cboColor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboColor.SelectedIndexChanged
' Me.TxtColorTableAdapter.Fill(Me.ColorDataSet.TxtColor)
Dim cboColor As Integer
Dim TxtColorValue As Integer = cboColor
End Sub
Private Sub nudTop_ValueChanged(sender As Object, e As EventArgs)
Dim nudTop As Integer
Dim CTop As Integer = nudTop
End Sub
Private Sub nudFSize_ValueChanged(sender As Object, e As EventArgs) Handles nudFSize.ValueChanged
Dim nudFSize As Integer
Dim FSize As Integer = nudFSize
End Sub
Private Sub cboFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboFont.SelectedIndexChanged
Dim CFont As String = cboFont.Text
End Sub
End Class