 |
| Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 27th, 2011, 09:11 PM
|
|
Registered User
|
|
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Pass a control name as a string
I am using Visual Studio 2010. ( VB)
I am creating an array of information that will be passed to a Print Function. I want this print routine to be generic, to be used in any application I write.
This array is a text array. My question is: If I pass the name of an object such as a picture box, for example . . MyArray(1,3) = PictureBox1 . . OR pass the handle of the picture bos as a string....
Is there a way to convert the text of 'PictureBox1', or hwnd handle back into the Object PictureBox1 so that the print routine can then identify and use the object's image?
In other words, Is it possible to take a string and identify the object?
|
|

January 2nd, 2012, 10:16 PM
|
|
Registered User
|
|
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the reply... However, I am using VB.NET..
I finally came up with the answer though, and here it is:
WHAT IT IS: This is code that can used on any form class. I do a lot of form creation and printing. Data is collected and entered by users, then a resultant form can be printed including the data. It may not be everyone's solution, but it works well for my typical application. What it does, is allow you to build an easy 'print script' through code that easily places and sizes text, images, barcodes etc. on your page, and then a single command invokes the printing. THANK YOU! Mr. Stephens, as the research all came from your book - "Visual Basic 2010 Programmer's Reference". I commented it fairly well... I also know an 'x' before everything may not be the correct nomenclature, but I put it there to help an old man (me) keep track of what it what!
Code:
'To use this print script code, place ALL of the code within a form's class. (Except the Imports line)
'Edit the code in the PrintProcess() as desired.
'Have your code for a Print Button: Call PrintProcess()
Imports System.Drawing.Printing
Public Class Form1
Dim PrintScript(8, 1) As String
Dim PScriptLines As Integer
Private WithEvents PieceOfPaper As PrintDocument
Private Sub PrintProcess()
'Default Minimum And Maximum Page Limits 35,35,875,1065 (Can vary depending on your printer)
'You must use this line before each print routine starts. It resets the script.
Call xPrintInitialize()
'* * * * * * * * * * * * * * * HERE IS WHERE YOU PUT THE ITEMS YOU WANT TO PRINT * * * * * * * * * * * * * * * *
'Sample Print Code
Call xPrintText(" 0 1 2 3 4 5 6 7 8 9", 300, 400, MyFontStyles.BoldUnderline, "LEDFont", 30, MyColors.Green)
Call xPrintBarCode("BARCODE123456789", 50, 50, 40)
Call xPrintText("Another Print Test Line", 100, 200, MyFontStyles.Normal, "Times New Roman", 10, MyColors.Green)
Call xPrintLine(288, 588, 370, 100, 6, 0, MyColors.Purple, MyLineStyles.Solid)
Call xPrintPageNumber(1, 1, "Tahoma", 8)
Call xPrintImage(300, 700, 100, 100, "PictureBox1", "") 'The text is the NAME of the picture box.
Call xPrintNewPage()
Call xPrintImage(Inches(1, 1, 4), Inches(1, 1, 4), 170, 30, "PictureBox2", "") 'The text is the NAME of the picture box.
'The Inches pieces are inch/fraction. 3 and one half (Left) and 4 and three fourths (Top)
Call xPrintText("Hello World!", Inches(3, 1, 2), Inches(4, 3, 4), MyFontStyles.BoldItalic, "Algerian", 33, MyColors.Blue)
'to print a temporary grid overlay for aligning assistance, use this:
Call xPrintGrid()
'Example: To Print the items in a List Box
'The (40 * CTR) is the line spacing
'Dim CTR As Integer
'For CTR = 0 To ListBox1.Items.Count - 1
' ListBox1.SelectedIndex = CTR
' Call xPrintText(ListBox1.Text, 100, 200 + (40 * CTR), 0, "Tahoma", 12, MyColors.Red)
'Next
'* * * * * * * * * * * * NOTHING ELSE NEEDS TO BE CHANGED UNLESS YOU HAVE OTHER IDEAS * * * * * * * * * * * * *
PieceOfPaper = New PrintDocument
'To Print With Print Preview
dlgPrintPreview.Document = PieceOfPaper
dlgPrintPreview.ShowDialog()
'To Print Instantly To The Default Printer
'PieceOfPaper.Print()
'To Show The Print Dialog
'dlgPrint.Document = PieceOfPaper
'dlgPrint.ShowDialog()
End Sub
Private Enum MyColors
White
Black
Brown
Red
Orange
Yellow
Green
Blue
Violet
Gray
Cyan
DarkGray
LightGray
Purple
Salmon
LightGreen
LightBlue
End Enum
Private Enum MyFontStyles
Normal
Bold
Italic
BoldItalic
Underline
BoldUnderline
ItalicUnderline
BoldItalicUnderline
Strike
BoldStrike
ItalicStrike
BolItalicStrike
UnderlineStrike
BoldUnderlineStrike
ItalicUnderlineStrike
BoldItalicUnderlineStrike
End Enum
Private Enum MyLineStyles
Solid
Dash
Dot
DashDot
DashDotDash
End Enum
Private Sub PieceOfPaper_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PieceOfPaper.PrintPage
Dim CTR As Integer
Dim xPageString As String
Dim EndOfScript As Boolean
Dim xMyTop As Integer
Dim xMyLeft As Integer
Dim xMyWidth As Integer
Dim xMyHeight As Integer
Static PageNumber As Integer
Static LastCTR As Integer
EndOfScript = False
If LastCTR = 0 Then
PageNumber = 1
Else
PageNumber = PageNumber + 1
End If
For CTR = LastCTR To PScriptLines
If CTR = PScriptLines Then EndOfScript = True
Select Case PrintScript(0, CTR)
Case "1" ' Text
Using the_font As New Font(PrintScript(5, CTR), PrintScript(6, CTR), PrintScript(4, CTR), GraphicsUnit.Pixel)
Using string_format As New StringFormat
string_format.Alignment = StringAlignment.Near
string_format.LineAlignment = StringAlignment.Near
e.Graphics.DrawString(PrintScript(1, CTR), the_font, GetBrushColor(PrintScript(7, CTR)), PrintScript(2, CTR), PrintScript(3, CTR))
End Using
End Using
Case "2" ' Line
Dim the_Pen As Pen
the_Pen = SetupThePen(PrintScript(7, CTR), PrintScript(5, CTR), PrintScript(8, CTR))
If PrintScript(6, CTR) = 1 Then
Dim pts() As Point = {New Point(PrintScript(1, CTR), PrintScript(2, CTR)), New Point(PrintScript(3, CTR), PrintScript(2, CTR)), New Point(PrintScript(3, CTR), PrintScript(4, CTR)), New Point(PrintScript(1, CTR), PrintScript(4, CTR))}
e.Graphics.DrawPolygon(the_Pen, pts)
Else
Dim pt1 As Point = New Point(PrintScript(1, CTR), PrintScript(2, CTR))
Dim pt2 As Point = New Point(PrintScript(3, CTR), PrintScript(4, CTR))
e.Graphics.DrawLine(the_Pen, pt1, pt2)
End If
the_Pen.Dispose()
Case "3" ' Image
xMyLeft = Val(PrintScript(1, CTR))
xMyTop = Val(PrintScript(2, CTR))
xMyWidth = Val(PrintScript(3, CTR))
xMyHeight = Val(PrintScript(4, CTR))
If InStr(PrintScript(5, CTR), "\") > 0 Then
Dim MyPictureBox As New PictureBox
MyPictureBox.Load(PrintScript(5, CTR))
e.Graphics.DrawImage(MyPictureBox.Image, xMyLeft, xMyTop, xMyWidth, xMyHeight)
'* * * * * * * * * * * * * Use this if using an ImageStrip
'ElseIf PrintScript(6, CTR).Length > 0 Then
' Dim MyPictureBox As New PictureBox
' MyPictureBox.Image = ImageList1.Images(PrintScript(6, CTR))
' e.Graphics.DrawImage(MyPictureBox.Image, xMyLeft, xMyTop, xMyWidth, xMyHeight)
Else
For Each MyPictureBox As Control In Controls
If MyPictureBox.Name = PrintScript(5, CTR) Then
If TypeOf (MyPictureBox) Is PictureBox Then
e.Graphics.DrawImage(DirectCast(MyPictureBox, PictureBox).Image, xMyLeft, xMyTop, xMyWidth, xMyHeight)
End If
End If
Next
End If
Case ("4")
If PrintScript(1, CTR) = "1" Then
'Use The Word 'Page'
xPageString = "- Page " & PageNumber & " -"
Else
xPageString = "- " & PageNumber & " -"
End If
Using the_font As New Font(PrintScript(5, CTR), Val(PrintScript(6, CTR)), FontStyle.Regular, GraphicsUnit.Pixel)
Using string_format As New StringFormat
string_format.Alignment = StringAlignment.Near
string_format.LineAlignment = StringAlignment.Near
If PrintScript(1, CTR) = 0 Then
e.Graphics.DrawString(xPageString, the_font, Brushes.Black, 390, 35)
Else
e.Graphics.DrawString(xPageString, the_font, Brushes.Black, 390, 1040)
End If
End Using
End Using
Case "5"
LastCTR = CTR + 1
CTR = PScriptLines + 1
Case "6"
Dim xBarCode(90) As String
Dim CharCTR As Integer
Dim BarCTR As Integer
Dim RepeatIt As Integer
xBarCode(1) = "101331"
xBarCode(2) = "101331"
xBarCode(48) = "110221"
xBarCode(49) = "310113"
xBarCode(50) = "130113"
xBarCode(51) = "330111"
xBarCode(52) = "110313"
xBarCode(53) = "310311"
xBarCode(54) = "130311"
xBarCode(55) = "110133"
xBarCode(56) = "310131"
xBarCode(57) = "130131"
xBarCode(65) = "311013"
xBarCode(66) = "131013"
xBarCode(67) = "331011"
xBarCode(68) = "113013"
xBarCode(69) = "313011"
xBarCode(70) = "133011"
xBarCode(71) = "111033"
xBarCode(72) = "311031"
xBarCode(73) = "131031"
xBarCode(74) = "113031"
xBarCode(75) = "311103"
xBarCode(76) = "131103"
xBarCode(77) = "331101"
xBarCode(78) = "113103"
xBarCode(79) = "313101"
xBarCode(80) = "133101"
xBarCode(81) = "111303"
xBarCode(82) = "311301"
xBarCode(83) = "131301"
xBarCode(84) = "113301"
xBarCode(85) = "301113"
xBarCode(86) = "103113"
xBarCode(87) = "303111"
xBarCode(88) = "101313"
xBarCode(89) = "301311"
xBarCode(90) = "103311"
xBarCode(32) = "103131"
xBarCode(35) = ""
xBarCode(36) = "10101011"
xBarCode(37) = "11010101"
xBarCode(43) = "10110101"
xBarCode(45) = "101133"
xBarCode(47) = "10101101"
xBarCode(46) = "301131"
xBarCode(64) = ""
xBarCode(42) = "101331"
PrintScript(1, CTR) = UCase(PrintScript(1, CTR))
PrintScript(1, CTR) = Trim$(PrintScript(1, CTR))
PrintScript(1, CTR) = Trim(PrintScript(1, CTR))
Dim ThisChar As Integer
Dim BCPattern As String
Dim xPixel As Double
xPixel = PrintScript(2, CTR)
PrintScript(1, CTR) = Chr(1) & PrintScript(1, CTR) & Chr(1)
For CharCTR = 1 To Len(PrintScript(1, CTR))
ThisChar = Asc(Mid$(PrintScript(1, CTR), CharCTR, 1))
If ThisChar > 90 Then ThisChar = 0
BCPattern = xBarCode(ThisChar)
For BarCTR = 1 To Len(BCPattern)
Select Case Mid$(BCPattern, BarCTR, 1)
Case 0
Using the_pen As New Pen(Color.White, 1)
Dim pts() As Point = {New Point(xPixel + RepeatIt, PrintScript(3, CTR)), New Point(xPixel + RepeatIt, (Val(PrintScript(3, CTR)) + Val(PrintScript(4, CTR))))}
e.Graphics.DrawPolygon(the_pen, pts)
xPixel = xPixel + 1
End Using ' the_pen
Case Else
Using the_pen As New Pen(Color.Black, 1)
For RepeatIt = 0 To Mid$(BCPattern, BarCTR, 1) - 1
Dim pts() As Point = {New Point(xPixel, PrintScript(3, CTR)), New Point(xPixel, (Val(PrintScript(3, CTR)) + Val(PrintScript(4, CTR))))}
e.Graphics.DrawPolygon(the_pen, pts)
xPixel = xPixel + 1
Next
End Using ' the_pen
xPixel = xPixel + 1
End Select
Next
Next
End Select
Next
If EndOfScript = True Then
e.HasMorePages = False
Else
e.HasMorePages = True
End If
End Sub
Private Sub xPrintInitialize()
ReDim PrintScript(8, 1)
Dim CTR As Integer
For CTR = 0 To 8
PrintScript(CTR, 0) = ""
PrintScript(CTR, 1) = ""
Next
PScriptLines = 0
End Sub
Private Sub xPrintText(ByVal xString As String, ByVal xLeft As Integer, ByVal xTop As Integer, ByVal xfontstyle As MyFontStyles, ByVal xFontName As String, xFontSize As Integer, ByVal xColor As MyColors)
PScriptLines = PScriptLines + 1
ReDim Preserve PrintScript(8, PScriptLines)
PrintScript(0, PScriptLines) = "1"
PrintScript(1, PScriptLines) = xString
PrintScript(2, PScriptLines) = xLeft
PrintScript(3, PScriptLines) = xTop
PrintScript(4, PScriptLines) = xfontstyle
PrintScript(5, PScriptLines) = xFontName
PrintScript(6, PScriptLines) = xFontSize
PrintScript(7, PScriptLines) = xColor
PrintScript(8, PScriptLines) = ""
End Sub
Private Sub xPrintLine(ByVal xX1 As Integer, ByVal xY1 As Integer, ByVal xX2 As Integer, ByVal xY2 As Integer, ByVal xLineWidth As Integer, ByVal xBoxIt As Integer, ByVal xColor As MyColors, ByVal PenStyle As MyLineStyles)
PScriptLines = PScriptLines + 1
ReDim Preserve PrintScript(8, PScriptLines)
PrintScript(0, PScriptLines) = "2"
PrintScript(1, PScriptLines) = xX1
PrintScript(2, PScriptLines) = xY1
PrintScript(3, PScriptLines) = xX2
PrintScript(4, PScriptLines) = xY2
PrintScript(5, PScriptLines) = xLineWidth
PrintScript(6, PScriptLines) = xBoxIt
PrintScript(7, PScriptLines) = xColor
PrintScript(8, PScriptLines) = PenStyle
End Sub
Private Sub xPrintImage(xLeft As Double, xTop As Double, xWidth As Double, xHeight As Double, ByVal xContainer As String)
PScriptLines = PScriptLines + 1
ReDim Preserve PrintScript(8, PScriptLines)
PrintScript(0, PScriptLines) = "3"
PrintScript(1, PScriptLines) = xLeft
PrintScript(2, PScriptLines) = xTop
PrintScript(3, PScriptLines) = xWidth
PrintScript(4, PScriptLines) = xHeight
PrintScript(5, PScriptLines) = xContainer
PrintScript(6, PScriptLines) = ""
PrintScript(7, PScriptLines) = ""
PrintScript(8, PScriptLines) = ""
End Sub
Private Sub xPrintImage(ByVal xLeft As Double, ByVal xTop As Double, ByVal xWidth As Double, ByVal xHeight As Double, ByVal xContainer As String, ByVal xKeyName As String)
'Import: Imports System.Drawing.Printing
'Add at class start: Dim PrintScript(7, 1) As String, Dim PScriptLines As Integer, Private WithEvents PieceOfPaper As PrintDocument
PScriptLines = PScriptLines + 1
ReDim Preserve PrintScript(8, PScriptLines)
PrintScript(0, PScriptLines) = "3"
PrintScript(1, PScriptLines) = xLeft
PrintScript(2, PScriptLines) = xTop
PrintScript(3, PScriptLines) = xWidth
PrintScript(4, PScriptLines) = xHeight
PrintScript(5, PScriptLines) = xContainer
PrintScript(6, PScriptLines) = xKeyName
PrintScript(7, PScriptLines) = ""
PrintScript(8, PScriptLines) = ""
End Sub
Private Sub xPrintPageNumber(xAtTop0Bottom1 As String, xUseTheWordPage1 As Integer, xFontName As String, xFontSize As String)
PScriptLines = PScriptLines + 1
ReDim Preserve PrintScript(8, PScriptLines)
PrintScript(0, PScriptLines) = "4"
PrintScript(1, PScriptLines) = xAtTop0Bottom1
PrintScript(2, PScriptLines) = xUseTheWordPage1
PrintScript(3, PScriptLines) = ""
PrintScript(4, PScriptLines) = ""
PrintScript(5, PScriptLines) = xFontName
PrintScript(6, PScriptLines) = xFontSize
PrintScript(7, PScriptLines) = ""
PrintScript(8, PScriptLines) = ""
End Sub
Private Sub xPrintNewPage()
PScriptLines = PScriptLines + 1
ReDim Preserve PrintScript(8, PScriptLines)
PrintScript(0, PScriptLines) = "5"
PrintScript(1, PScriptLines) = ""
PrintScript(2, PScriptLines) = ""
PrintScript(3, PScriptLines) = ""
PrintScript(4, PScriptLines) = ""
PrintScript(5, PScriptLines) = ""
PrintScript(6, PScriptLines) = ""
PrintScript(7, PScriptLines) = ""
PrintScript(8, PScriptLines) = ""
End Sub
Private Sub xPrintBarCode(xString As String, xLeft As Integer, xTop As Integer, xHeight As Integer)
PScriptLines = PScriptLines + 1
ReDim Preserve PrintScript(8, PScriptLines)
PrintScript(0, PScriptLines) = "6"
PrintScript(1, PScriptLines) = xString
PrintScript(2, PScriptLines) = xLeft
PrintScript(3, PScriptLines) = xTop
PrintScript(4, PScriptLines) = xHeight
PrintScript(5, PScriptLines) = ""
PrintScript(6, PScriptLines) = ""
PrintScript(7, PScriptLines) = ""
PrintScript(8, PScriptLines) = ""
End Sub
Private Function GetBrushColor(ByRef SelectedCOlor As Integer) As System.Drawing.Brush
Select Case SelectedCOlor
Case 0
Return Brushes.White
Case 1
Return Brushes.Black
Case 2
Return Brushes.Brown
Case 3
Return Brushes.Red
Case 4
Return Brushes.Orange
Case 5
Return Brushes.Yellow
Case 6
Return Brushes.Green
Case 7
Return Brushes.Blue
Case 8
Return Brushes.Violet
Case 9
Return Brushes.Gray
Case 10
Return Brushes.Cyan
Case 11
Return Brushes.DarkGray
Case 12
Return Brushes.LightGray
Case 13
Return Brushes.Purple
Case 14
Return Brushes.Salmon
Case 15
Return Brushes.LightGreen
Case 16
Return Brushes.LightBlue
Case Else
Return Brushes.Black
End Select
End Function
Private Function SetupThePen(ByRef SelectedColor As Integer, ByRef MyThickness As Integer, ByRef MyLineStyle As MyLineStyles) As System.Drawing.Pen
Dim TempColor As Color
Select Case SelectedColor
Case 0
TempColor = Color.White
Case 1
TempColor = Color.Black
Case 2
TempColor = Color.Brown
Case 3
TempColor = Color.Red
Case 4
TempColor = Color.Orange
Case 5
TempColor = Color.Yellow
Case 6
TempColor = Color.Green
Case 7
TempColor = Color.Blue
Case 8
TempColor = Color.Violet
Case 9
TempColor = Color.Gray
Case 10
TempColor = Color.Cyan
Case 11
TempColor = Color.DarkGray
Case 12
TempColor = Color.LightGray
Case 13
TempColor = Color.Purple
Case 14
TempColor = Color.Salmon
Case 15
TempColor = Color.LightGreen
Case 16
TempColor = Color.LightBlue
Case Else
TempColor = Color.Black
End Select
Dim MyTempPen = New Pen(TempColor, MyThickness)
MyTempPen.DashStyle = MyLineStyle
Return MyTempPen
End Function
Private Function Inches(Optional ByVal InWhole As Integer = 0, Optional ByVal InNumerator As Integer = 0, Optional ByVal InDenominator As Integer = 32) As Single
If InDenominator = 0 Then InDenominator = 64
Return (InWhole * 100) + (InNumerator / InDenominator) * 100
End Function
Private Sub xPrintGrid()
Dim GridWidth As Single
Dim GridHeight As Single
Dim GridInches As Single
Dim InLineStyle As MyLineStyles
GridWidth = 8.5
GridHeight = 11
For GridInches = 0 To GridWidth Step 0.125
Select Case GridInches - Int(GridInches)
Case 0 ' Whole Inch
InLineStyle = MyLineStyles.Solid
Case 0.5 ' Half Inch
InLineStyle = MyLineStyles.Dash
Case 0.25, 0.75 ' Qharter / Three Quarter
InLineStyle = MyLineStyles.DashDot
Case 0.125, 0.375, 0.625, 0.875 ' Eigths
InLineStyle = MyLineStyles.Dot
End Select
xPrintLine(CInt(GridInches * 100), 0, CInt(GridInches * 100), 1100, 1, 0, MyColors.Black, InLineStyle)
Next
For GridInches = 0 To GridHeight Step 0.125
Select Case GridInches - Int(GridInches)
Case 0 ' Whole Inch
InLineStyle = MyLineStyles.Solid
Case 0.5 ' Half Inch
InLineStyle = MyLineStyles.Dash
Case 0.25, 0.75 ' Qharter / Three Quarter
InLineStyle = MyLineStyles.DashDot
Case 0.125, 0.375, 0.625, 0.875 ' Eigths
InLineStyle = MyLineStyles.Dot
End Select
xPrintLine(0, CInt(GridInches * 100), 850, CInt(GridInches * 100), 1, 0, MyColors.Black, InLineStyle)
Next
End Sub
Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
Call PrintProcess()
End Sub
End Class
|
|
 |