メモ帳を作ろう!後編VB.NET入門


それでは前回の続きです。
印刷と印刷プレビューです。
ダイヤログとドキメントですデータですね。
PrintPreviewDialog1
PrintDialog1
PrintDocument1

Dim printData As String 受け渡し用の箱です。

PrintDocument1_PrintPageでデータを設定してます。
e.Graphics.DrawStringで文字を設定して書いてます。

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
printData = TextBox1.Text
e.Graphics.DrawString(printData.ToString, New Font(“msゴシック”, 16), Brushes.BurlyWood, 0, 0)
End Sub

Private Sub 印刷PToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 印刷PToolStripMenuItem.Click
printData = TextBox1.Text
PrintDialog1.PrinterSettings = New System.Drawing.Printing.PrinterSettings
PrintDialog1.ShowDialog()
PrintDialog1.Document = PrintDocument1
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub

Private Sub 印刷プレビューVToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 印刷プレビューVToolStripMenuItem.Click
printData = TextBox1.Text
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub

編集は実は簡単に出来ます。
Private Sub すべて選択AToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles すべて選択AToolStripMenuItem.Click
TextBox1.SelectAll()
End Sub

Private Sub コピーCToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles コピーCToolStripMenuItem.Click
TextBox1.Copy()
End Sub

Private Sub 貼り付けPToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 貼り付けPToolStripMenuItem.Click
TextBox1.Paste()
End Sub

Private Sub 切り取りTToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 切り取りTToolStripMenuItem.Click
TextBox1.Cut()
End Sub

Private Sub 元に戻すUToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 元に戻すUToolStripMenuItem.Click
TextBox1.Undo()
End Sub

Private Sub やり直しRToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles やり直しRToolStripMenuItem.Click
TextBox1.Undo()
End Sub

結構調べながらなので無駄な処理やダイヤログの初期値とか気になるところはあると思いますが仕様ですw
検索とか置換からまだまだ実装する機能があるとは思います。
気が向いたら機能追加します。

全体
Public Class Form1
Dim filepath As String
Dim printData As String

Private Sub 終了XToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 終了XToolStripMenuItem.Click
End
End Sub

Private Sub 新規作成NToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 新規作成NToolStripMenuItem.Click
TextBox1.Text = “”
End Sub

Private Sub 開くOToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 開くOToolStripMenuItem.Click
OpenFileDialog1.ShowDialog()
End Sub

Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
filepath = OpenFileDialog1.FileName
Dim sr As New System.IO.StreamReader(filepath, _
System.Text.Encoding.GetEncoding(“shift_jis”))
TextBox1.Text = sr.ReadToEnd()
sr.Close()
End Sub

Private Sub 名前を付けて保存AToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 名前を付けて保存AToolStripMenuItem.Click
SaveFileDialog1.ShowDialog()
End Sub

Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
filepath = SaveFileDialog1.FileName
Call save()
End Sub

Private Sub 上書き保存SToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 上書き保存SToolStripMenuItem.Click
If filepath = Nothing Then
SaveFileDialog1.ShowDialog()
Call save()
Else
Call save()
End If

End Sub
Private Sub save()
Dim sw As New System.IO.StreamWriter(filepath, _
False, _
System.Text.Encoding.GetEncoding(“shift_jis”))
sw.Write(TextBox1.Text)
sw.Close()

End Sub

Private Sub 印刷PToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 印刷PToolStripMenuItem.Click
printData = TextBox1.Text
PrintDialog1.PrinterSettings = New System.Drawing.Printing.PrinterSettings
PrintDialog1.ShowDialog()
PrintDialog1.Document = PrintDocument1
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub

Private Sub 印刷プレビューVToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 印刷プレビューVToolStripMenuItem.Click
printData = TextBox1.Text
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
printData = TextBox1.Text
e.Graphics.DrawString(printData.ToString, New Font(“msゴシック”, 16), Brushes.BurlyWood, 0, 0)
End Sub

Private Sub すべて選択AToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles すべて選択AToolStripMenuItem.Click
TextBox1.SelectAll()
End Sub

Private Sub コピーCToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles コピーCToolStripMenuItem.Click
TextBox1.Copy()
End Sub

Private Sub 貼り付けPToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 貼り付けPToolStripMenuItem.Click
TextBox1.Paste()
End Sub

Private Sub 切り取りTToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 切り取りTToolStripMenuItem.Click
TextBox1.Cut()
End Sub

Private Sub 元に戻すUToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 元に戻すUToolStripMenuItem.Click
TextBox1.Undo()
End Sub

Private Sub やり直しRToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles やり直しRToolStripMenuItem.Click
TextBox1.Undo()
End Sub

End Class

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

Translate »