Thursday, December 22, 2011

Thursday 12.22.11

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim PrintDoc As New Printing.PrintDocument
AddHandler PrintDoc.PrintPage, AddressOf Me.PrintText
PrintDoc.Print()

Catch ex As Exception
MessageBox.Show("Sorry - there is a problem printing", ex.ToString())
End Try
End Sub

'sub for printing text
Private Sub PrintText(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
'user drawstring to create text in a Graphics object
ev.Graphics.DrawString(TextBox1.Text, New Font("Arial", 11, FontStyle.Regular), Brushes.Black, 120, 120)
'specify that this is the last page to print
ev.HasMorePages = False
End Sub
End Class


mports System.IO
Imports System.Drawing.Printing

Public Class Form1

Private PrintPageSettings As New PageSettings
Private StringToPrint As String
Private PrintFont As New Font("Arial", 10)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
Dim FilePath As String
'display Open dialog box and select text file
OpenFileDialog1.Filter = "Text files (*.txt) | *.txt"
OpenFileDialog1.ShowDialog()
'if cancel button not selected, load FilePath variable
If OpenFileDialog1.FileName <> "" Then
FilePath = OpenFileDialog1.FileName
Try
'read text file and laod into RichTextBox1
Dim myFileStream As New FileStream(FilePath, FileMode.Open)
RichTextBox1.LoadFile(myFileStream, RichTextBoxStreamType.PlainText)
myFileStream.Close()
'initialize string to print
StringToPrint = RichTextBox1.Text
'enable print button
btnPrint.Enabled = True
btnSetup.Enabled = True
btnPreview.Enabled = True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
'specify the current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'specify document for print dialog box and show
StringToPrint = RichTextBox1.Text
PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = PrintDialog1.ShowDialog()
'if click OK, print docuemnt to printer
If result = DialogResult.OK Then
PrintDocument1.Print()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim numChars As Integer
Dim numLines As Integer
Dim stringForPage As String
Dim strFormat As New StringFormat
'based on page setup, define drawable rectangle on page
Dim rectDraw As New RectangleF( _
e.MarginBounds.Left, e.MarginBounds.Top, _
e.MarginBounds.Width, e.MarginBounds.Height)
'define area to determine how much text can fit on a page
'make height one line shorter to ensure text doesn't clip
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, _
e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))

'when drawing long strings, break between words
strFormat.Trimming = StringTrimming.Word
'computer how many chars and lines can fit based on sizeMeasure
e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
'compute string that will fit on a page
stringForPage = StringToPrint.Substring(0, numChars)
'print string on curent page
e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
'if there is more text, indicate there are more pages
If numChars < StringToPrint.Length Then
'subtract text from string that has been printed
StringToPrint = StringToPrint.Substring(numChars)
e.HasMorePages = True
Else
e.HasMorePages = False
'all text has been printed, so restore string
StringToPrint = RichTextBox1.Text

End If
End Sub

Private Sub btnSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetup.Click
Try
'load page settings and display page setup dialog box
PageSetupDialog1.PageSettings = PrintPageSettings
PageSetupDialog1.ShowDialog()
Catch ex As Exception
'display error message
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
Try
'specify current page settings
PrintDocument1.DefaultPageSettings = PrintPageSettings
'Specify document for print preview dialog box an dshow
StringToPrint = RichTextBox1.Text
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
'display error message
MessageBox.Show(ex.Message)
End Try
End Sub
End Class


Imports System.Reflection
Imports System
Imports System.Threading

Module Module1

Sub main()
'get and display the friendly name fo the default app domain
Dim callingDomainName As String = Thread.GetDomain().FriendlyName
Console.WriteLine(callingDomainName)

'get and display the full name of the EXE assembly
Dim exeAssembly As String = [Assembly].GetEntryAssembly().FullName
Console.WriteLine(exeAssembly)

'construct and initialize settings for a second appDomain
Dim ads As New AppDomainSetup()
ads.ApplicationBase = System.Environment.CurrentDirectory
ads.DisallowBindingRedirects = False
ads.DisallowCodeDownload = True
ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
' ads.ConfigurationFile = appDomain.CurrentDomain.SetupInformation.ConfigurationFile
Dim ad2 As AppDomain = AppDomain.CreateDomain("AD #2", Nothing, ads)
createNewDomain()
End Sub



Public Sub createNewDomain()
Console.WriteLine("Creating new AppDomain")
Dim domain As AppDomain = AppDomain.CreateDomain("MyDomain")

Console.WriteLine("Host Domain: " + AppDomain.CurrentDomain.FriendlyName)
Console.WriteLine("Child Domain: " + domain.FriendlyName)

End Sub
End Module


Imports System.Threading

'our custom delegate
Public Delegate Function BinaryOp(ByVal x As Integer, ByVal y As Integer) As Integer


Module Module1

Sub Main()
Console.WriteLine("***** Synch Delegate Review *****")
Console.WriteLine()

'print out the ID of the executing thread
Console.WriteLine("Main() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId)

'invoke Add() in a synchronous manner
Dim b As BinaryOp = AddressOf Add
Dim answer As Integer = b(10, 10)

'these lines will not execute until the Add() method has completed
Console.WriteLine("Doing more work in Main()!")
Console.WriteLine("10 + 10 is {0}.", answer)
Console.ReadLine()
End Sub


Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
'print out the ID of the executing thread
Console.WriteLine("Add() invoked on thread {0}.", Thread.CurrentThread.ManagedThreadId)
'pause to simualte a lengthy operation
Thread.Sleep(5000)
Return x + y
End Function

End Module

No comments: