Der Pc-Blog ist umgezogen. Du findest ihn ab jetzt unter www.meinpc-blog.de

Sonntag, 30. Januar 2011

Visual Basic Tutorialreihe - Teil 7 - Notepad für Reiche

In diesem Teil meiner Tutorialreihe zeige ich euch, wie man mit einem OpenFileDialog dateien öffnet, mit einem SaveFileDialog Dateien speichert und Dateien druckt.



Imports System.IO
Imports System.Drawing

Public Class Form1

Private Sub ÖffnenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ÖffnenToolStripMenuItem.Click

OpenFileDialog1.ShowDialog()

End Sub

Private Sub SpeichernToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SpeichernToolStripMenuItem.Click

SaveFileDialog1.ShowDialog()

End Sub

Private Sub ÜberToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ÜberToolStripMenuItem.Click
Form2.Show()
End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

If Not RichTextBox1.SelectionFont.Style = FontStyle.Bold Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Bold)
Else
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
End If

End Sub

Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click

If Not RichTextBox1.SelectionFont.Style = FontStyle.Italic Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Italic)
Else
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
End If

End Sub

Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click

If Not RichTextBox1.SelectionFont.Style = FontStyle.Underline Then
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Underline)
Else
RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Regular)
End If

End Sub


Private Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged

Try
RichTextBox1.SelectionFont = New Font(New FontFamily(ToolStripComboBox1.Text), Convert.ToInt32(ToolStripComboBox2.Text), FontStyle.Regular)
Catch ex As Exception
End Try

RichTextBox1.Focus()

End Sub

Private Sub ToolStripComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripComboBox2.SelectedIndexChanged

Try
RichTextBox1.SelectionFont = New Font(New FontFamily(ToolStripComboBox1.Text), Convert.ToInt32(ToolStripComboBox2.Text), FontStyle.Regular)
Catch ex As Exception
End Try

RichTextBox1.Focus()

End Sub

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

RichTextBox1.Text = ""

Dim sr As New StreamReader(OpenFileDialog1.FileName)

While Not sr.EndOfStream
RichTextBox1.Text = RichTextBox1.Text & sr.ReadLine & vbCrLf
End While

sr.Close()

End Sub

Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk

Dim sw As New StreamWriter(SaveFileDialog1.FileName)

For Each line As String In RichTextBox1.Lines
sw.WriteLine(line)
Next

sw.Close()

End Sub

Private Sub DruckenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DruckenToolStripMenuItem.Click

PrintDocument1.Print()

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

Dim Schrift As Drawing.Font = New Drawing.Font("Arial", 14, FontStyle.Regular)

e.Graphics.DrawString(RichTextBox1.Text, Schrift, Brushes.Black, 50, 50)

End Sub
End Class

Keine Kommentare:

Kommentar veröffentlichen