' classe que ten funções de entrada e saida de dados
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
' para avisar guardar antes de fexar
Dim mudou As Boolean = False
Private Sub AbrirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbrirToolStripMenuItem.Click
' variavel para saber qual o botão que clicou na msg box
Dim r As Integer
If mudou Then
r = MsgBox("Não guardou as Alterações, pretende Guardar?", MsgBoxStyle.YesNoCancel)
If r = MsgBoxResult.Yes Then
GuardarToolStripMenuItem_Click(sender, e)
ElseIf r = MsgBoxResult.No Then
abrir()
End If
Else
abrir()
End If
End Sub
Private Sub GuardarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuardarToolStripMenuItem.Click
If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim guardou As Boolean = guardar_file(Me.txt_principal.Text, SaveFileDialog1.FileName)
If guardou Then
Me.StatusBar_notepad.Text = "ficheiro" & Me.SaveFileDialog1.FileName & _
"Guardado com sucesso"
mudou = False
Else
Me.StatusBar_notepad.Text = "ERRO: Ficheiro Não Guardado"
End If
End If
End Sub
' ' opfile objecto de dialogo para escolher o ficheiro
Public Sub abrir()
If Opfile.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.txt_principal.Text = abrir_file(Opfile.FileName)
Me.Text = "notepad.net plus - " & Opfile.FileName
Me.StatusBar_notepad.Text = ""
End If
End Sub
' função para abrir um file
Public Function abrir_file(ByVal fullpath As String, _
Optional ByRef errinfo As String = "") As String
Dim strfile As String
Dim objreader As StreamReader
Try
objreader = New StreamReader(fullpath)
strfile = objreader.ReadToEnd()
objreader.Close()
Return strfile
Catch ex As Exception
errinfo = ex.Message
End Try
Return ""
End Function
' função guardar file
Public Function guardar_file(ByVal doc As String, ByVal fullpath As String, _
Optional ByVal errinfo As String = "") As Boolean
' stream writer , escrever num determinado ficheiro
Dim guardou As Boolean = False
Dim objwriter As StreamWriter
Try
objwriter = New StreamWriter(fullpath)
objwriter.Write(doc)
objwriter.Close()
guardou = True
Catch ex As Exception
errinfo = ex.Message
End Try
Return guardou
End Function
' ficheiro novo
Private Sub NovoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NovoToolStripMenuItem.Click
Dim r As Integer
If mudou Then
r = MsgBox("Não Guadoru as alterações, pertende Guardar ?", MsgBoxStyle.YesNoCancel, "Guardar")
If r = MsgBoxResult.Yes Then
Me.GuardarToolStripMenuItem_Click(sender, e)
ElseIf r = MsgBoxResult.No Then
Me.txt_principal.Text = ""
End If ' caso clique cancel fiva na mesma
Else
Me.txt_principal.Text = ""
End If
End Sub
Private Sub txt_principal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_principal.TextChanged
mudou = True
End Sub
' menu opção sair
Private Sub SairToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SairToolStripMenuItem.Click
Dim r As Integer
' ao clicar em sair sem salvar dá um aviso
If mudou Then
r = MsgBox("Não Guadoru as alterações, pertende Guardar ?", MsgBoxStyle.YesNoCancel, "Guardar")
If r = MsgBoxResult.Yes Then
Me.GuardarToolStripMenuItem_Click(sender, e)
Close()
ElseIf r = MsgBoxResult.No Then
Me.txt_principal.Text = ""
End If ' caso clique cancel fiva na mesma
Else
Me.txt_principal.Text = ""
End If
End Sub
' formclosing quando esta a fechar
' formclose quando esta fexado
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim r As Integer
' ao clicar em sair sem salvar dá um aviso
If mudou Then
r = MsgBox("Não Guardou as alterações, pertende Guardar ?", MsgBoxStyle.YesNoCancel, "Guardar")
If r = MsgBoxResult.Yes Then
If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim guardou As Boolean = guardar_file(Me.txt_principal.Text, SaveFileDialog1.FileName)
If Not guardou Then MsgBox("erro:ficheiro não guardado")
Else
' cancela o close por isso não fecha a aplicação
e.Cancel = True
End If
ElseIf r = MsgBoxResult.Cancel Then
e.Cancel = True
End If ' caso clique cancel fiva na mesma
End If
End Sub
' status bar
Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusBar_status.Click
Me.StatusBar_notepad.Visible = Not Me.StatusBar_notepad.Visible
' DESCHECAR A OPÇÃO DO MENU
Me.StatusBar_status.Checked = Not Me.StatusBar_status.Checked
End Sub
Private Sub Statusbar_transparente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Statusbar_transparente.Click
' se a opção transparebte for activada por a tranaparencia
If Me.Statusbar_transparente.Checked Then
Me.TransparencyKey = Color.Brown
Me.Statusbar_transparente.Checked = False
Else
Me.TransparencyKey = Me.txt_principal.BackColor
Me.Statusbar_transparente.Checked = True
End If
End Sub
Private Sub TipoDeLetraToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TipoDeLetraToolStripMenuItem.Click
' mostra o dialog de mudança de fonte
If Me.FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.txt_principal.Font = Me.FontDialog1.Font
End If
End Sub
Private Sub CorDeTextoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CorDeTextoToolStripMenuItem.Click
' cor do texto
If Me.ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.txt_principal.ForeColor = Me.ColorDialog1.Color
End If
End Sub
Private Sub CorDeFundoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CorDeFundoToolStripMenuItem.Click
'cor do fundo
If Me.ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.txt_principal.BackColor = Me.ColorDialog1.Color
Me.Statusbar_transparente.Checked = False
End If
End Sub
End Class