Public Class Form1
Inherits System.Windows.Forms.Form
Private filename As String 'indien opgeslagen
bestandsnaam
Private hasbeenchanged As Boolean 'is bestand gewijzigd sinds laatst opgeslagen?
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFileOpen.Click
Try
Me.OpenFileDialog1.Filter = "RTF bestanden|*.rtf|Tekstbestanden|*.txt"
Me.OpenFileDialog1.ShowDialog()
filename = OpenFileDialog1.FileName
Select Case LCase(Microsoft.VisualBasic.Right(filename, 3)) 'wat is de extensie?
Case "rtf"
rtf.LoadFile(filename, RichTextBoxStreamType.RichText)
Case "txt"
rtf.LoadFile(filename, RichTextBoxStreamType.PlainText)
End Select
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub mnuFileSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFileSave.Click
Try
If filename = "" Then mnuFileSaveAs_Click(sender, e) 'opslaan als indien geen
bestandsnaam
Select Case LCase(Microsoft.VisualBasic.Right(filename, 3)) 'wat is de extensie?
Case "rtf"
rtf.SaveFile(filename, RichTextBoxStreamType.RichText)
Case "txt"
rtf.SaveFile(filename, RichTextBoxStreamType.PlainText)
End Select
hasbeenchanged = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub mnuFileSaveAs_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFileSaveAs.Click
Try
Me.SaveFileDialog1.Filter = "RTF bestanden|*.rtf|Tekstbestanden|*.txt"
Me.SaveFileDialog1.ShowDialog()
filename = SaveFileDialog1.FileName
Select Case LCase(Microsoft.VisualBasic.Right(filename, 3)) 'wat is de extensie?
Case "rtf"
rtf.SaveFile(filename, RichTextBoxStreamType.RichText)
Case "txt"
rtf.SaveFile(filename, RichTextBoxStreamType.PlainText)
End Select
hasbeenchanged = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub mnuFileClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFileClose.Click
Me.Close()
End Sub
Private Sub mnuFileNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFileNew.Click
Dim f As New Form1
f.Show()
End Sub
Private Sub mnuFormatBold_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatBold.Click
'andere oplossing: http://www.vbcity.com/forums/topic.asp?tid=3554&#RID131263
'ook http://www.dotnetforums.net/showthread.php?t=87752 en http://msdn.microsoft.com/smartclient/community/wffaq/ctrlsp.aspx
If rtf.SelectionFont.Bold Then 'als het vet is, vet uitzetten
rtf.SelectionFont = New Font(rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size, FontStyle.Regular)
Else
rtf.SelectionFont = New Font(rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size, FontStyle.Bold)
End If
End Sub
Private Sub mnuFormatItalic_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatItalic.Click
If rtf.SelectionFont.Italic Then 'als het cursief is, cursief uitzetten
rtf.SelectionFont = New Font(rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size, FontStyle.Regular)
Else
rtf.SelectionFont = New Font(rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size, FontStyle.Italic)
End If
End Sub
Private Sub mnuFormatUnderline_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatUnderline.Click
If rtf.SelectionFont.Underline Then 'als het onderlijnd is, onderlijnd uitzetten
rtf.SelectionFont = New Font(rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size, FontStyle.Regular)
Else
rtf.SelectionFont = New Font(rtf.SelectionFont.FontFamily,
rtf.SelectionFont.Size, FontStyle.Bold)
End If
End Sub
Private Sub mnuFormatLeft_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatLeft.Click
rtf.SelectionAlignment = HorizontalAlignment.Left
End Sub
Private Sub mnuFormatCenter_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatCenter.Click
rtf.SelectionAlignment = HorizontalAlignment.Center
End Sub
Private Sub mnuFormatJustify_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatJustify.Click
'rtf.SelectionAlignment= helaas geen justify
End Sub
Private Sub mnuFormatRight_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatRight.Click
rtf.SelectionAlignment = HorizontalAlignment.Right
End Sub
Private Sub mnuFormatNumbered_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatNumbered.Click
'helaas niet ingebouwd
End Sub
Private Sub mnuFormatBulleted_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatBulleted.Click
rtf.SelectionBullet = Not rtf.SelectionBullet
End Sub
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
Select Case e.Button.Tag
'Opletten: dit zijn strings, dus het wél case-sensitive
Case "New"
mnuFileNew_Click(sender, e)
Case "Open"
Me.mnuFileOpen_Click(sender, e)
Case "Save"
Me.mnuFileSave_Click(sender, e)
Case "Copy"
Me.mnuCopy_Click(sender, e)
Case "Paste"
Me.mnuPaste_Click(sender, e)
Case "Cut"
Me.mnuCut_Click(sender, e)
Case "Delete"
Me.mnuDelete_Click(sender, e)
Case "Bold"
Me.mnuFormatBold_Click(sender, e)
Case "Italic"
Me.mnuFormatItalic_Click(sender, e)
Case "Underline"
Me.mnuFormatItalic_Click(sender, e)
Case "Left"
Me.mnuFormatLeft_Click(sender, e)
Case "Justify"
Me.mnuFormatJustify_Click(sender, e)
Case "Center"
Me.mnuFormatCenter_Click(sender, e)
Case "Right"
Me.mnuFormatRight_Click(sender, e)
End Select
End Sub
Private Sub rtf_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles rtf.TextChanged
hasbeenchanged = True
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'onthouden waar het scherm staat
'opgelet: dit kan alleen in Windows, omdat het register gebruikt wordt
SaveSetting("RTFEditor", "Startup", "Top", Me.Top)
SaveSetting("RTFEditor", "Startup", "Left", Me.Left)
SaveSetting("RTFEditor", "Startup", "Width", Me.Width)
SaveSetting("RTFEditor", "Startup", "Height", Me.Height)
'een alternatief staat op http://aspalliance.com/448
If hasbeenchanged = True Then
Select Case MsgBox("Het bestand is gewijzigd. Opslaan?", MsgBoxStyle.YesNoCancel)
Case MsgBoxResult.Yes
Me.mnuFileSave_Click(sender, e)
Case MsgBoxResult.Cancel
e.Cancel = True
End Select
End If
End Sub
Private Sub mnuCopy_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuCopy.Click
Clipboard.SetDataObject(rtf.SelectedText)
End Sub
Private Sub mnuCut_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuCut.Click
Clipboard.SetDataObject(rtf.SelectedText)
rtf.SelectedText = ""
End Sub
Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuDelete.Click
rtf.SelectedText = ""
End Sub
Private Sub mnuPaste_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuPaste.Click
'code van devx.com
' get the data currently in the Clipboard
Dim data As IDataObject = Clipboard.GetDataObject()
' check whether there is any data in RTF format,
' without attempting a conversion
If data.GetDataPresent(DataFormats.Rtf, False) Then
' if available, paste into the RTF selection
rtf.SelectedRtf = data.GetData(DataFormats.Rtf).ToString()
ElseIf data.GetDataPresent(DataFormats.Text, True) Then
' otherwise attempts to get data in plaintext format
rtf.SelectedText = data.GetData(DataFormats.Text, True).ToString()
End If
End Sub
Private Sub mnuFormatFont_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuFormatFont.Click
FontDialog1.Font = rtf.SelectionFont
FontDialog1.ShowDialog()
rtf.SelectionFont = FontDialog1.Font
End Sub
Private Sub rtf_SelectionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles rtf.SelectionChanged
'even herinneren: de nummers van de knoppen
'7 bold, 8 italic, 9 underline, 10 left, 11 justify, 12 center, 13 right
'is het vet?
Me.mnuFormatBold.Checked = rtf.SelectionFont.Bold
Me.ToolBar1.Buttons(7).Pushed = rtf.SelectionFont.Bold
'is het cursief?
Me.mnuFormatItalic.Checked = rtf.SelectionFont.Italic
Me.ToolBar1.Buttons(8).Pushed = rtf.SelectionFont.Italic
'is het onderlijnd?
Me.mnuFormatUnderline.Checked = rtf.SelectionFont.Underline
Me.ToolBar1.Buttons(9).Pushed = rtf.SelectionFont.Underline
'is het links uitgelijnd?
If rtf.SelectionAlignment = HorizontalAlignment.Left Then
mnuFormatLeft.Checked = True
ToolBar1.Buttons(10).Pushed = True
Else
mnuFormatLeft.Checked = False
ToolBar1.Buttons(10).Pushed = False
End If
'is het rechts uitgelijnd?
If rtf.SelectionAlignment = HorizontalAlignment.Right Then
mnuFormatLeft.Checked = True
ToolBar1.Buttons(10).Pushed = True
Else
mnuFormatLeft.Checked = False
ToolBar1.Buttons(10).Pushed = False
End If
'is het gecentreerd?
If rtf.SelectionAlignment = HorizontalAlignment.Center Then
mnuFormatLeft.Checked = True
ToolBar1.Buttons(10).Pushed = True
Else
mnuFormatLeft.Checked = False
ToolBar1.Buttons(10).Pushed = False
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
'functietoetsen simuleren bij de klembordoperaties
Me.mnuCopy.Text &= Chr(9) & "Ctrl+C"
Me.mnuPaste.Text &= Chr(9) & "Ctrl+V"
Me.mnuCut.Text &= Chr(9) & "Ctrl+X"
Me.mnuDelete.Text &= Chr(9) & "Del"
'opvragen waar het scherm stond bij laatste afsluiten
'opgelet: dit kan alleen in Windows, omdat het register gebruikt wordt
Try
Me.Top = CInt(GetSetting("RTFEditor", "Startup", "Top"))
Me.Left = CInt(GetSetting("RTFEditor", "Startup", "Left"))
Me.Width = CInt(GetSetting("RTFEditor", "Startup", "Width"))
Me.Height = CInt(GetSetting("RTFEditor", "Startup", "Height"))
'een alternatief staat op http://aspalliance.com/448
Catch
'niets doen: een fout betekent gewoon dat er geen entries zijn in het register
End Try
End Sub
End Class
[Terug naar het project] - [VB Web] - [hlrnet] - [copyright]