dimanche 11 novembre 2012

Exercice Visual Basic : Bloc Note

Ecrire un programme qui permet de réaliser un Bloc Note simplifié.

Interface Graphique
:





Corrigé

Imports System.IO
Imports System.Drawing.Printing
 

Public Class BlocNot
    'FolderBrowserDialog 
    'Dim fbd As FolderBrowserDialog
    'fbd = New FolderBrowserDialog
    'fbd.RootFolder = Environment.SpecialFolder.DesktopDirectory
    'fbd.ShowDialog()
 
    Private Sub SupprimerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' If txtuser.Text = "" Then
        'SupprimerToolStripMenuItem.Enabled = True
        ' ElseIf txtuser.Text  "" Then
        'SupprimerToolStripMenuItem.Enabled = False
        'txtuser.SelectedText = ""
        ' End If
    End Sub
    Private Sub PoliceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PoliceToolStripMenuItem.Click
        FontDialog1.ShowDialog()
        txtuser.Font = FontDialog1.Font
    End Sub
 
    Private Sub FontColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontColorToolStripMenuItem.Click
        ColorDialog1.ShowDialog()
        txtuser.ForeColor = ColorDialog1.Color
    End Sub
 
    Private Sub BackColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackColorToolStripMenuItem.Click
        ColorDialog1.ShowDialog()
        txtuser.BackColor = ColorDialog1.Color
    End Sub
    Private Sub saveas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SaveFileDialog1.ShowDialog()
 
    End Sub
    Private Sub CollerToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CollerToolStripMenuItem.Click
        txtuser.Paste()
    End Sub
    Private Sub CouperToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CouperToolStripMenuItem.Click
        txtuser.Cut()
    End Sub
 
    Private Sub CopierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopierToolStripMenuItem.Click
        txtuser.Copy()
    End Sub
 
    Private Sub Quitter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Quitter.Click
        Dim rep As DialogResult
        If txtuser.Text  "" Then
            rep = MessageBox.Show("Voulez-vous enregistrer les modifications  ", "Quitter Bloc-notes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning)
        End If
        If txtuser.Text = "" Then
            rep = MessageBox.Show("Voulez-vous vraiment quitter ", "Quitter Bloc-notes", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If rep = Windows.Forms.DialogResult.Yes Then
                Application.Exit()
            End If
            If rep = Windows.Forms.DialogResult.No Then
            End If
        End If
    End Sub
 
    Private Sub AnnulerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnnulerToolStripMenuItem.Click
        txtuser.Undo()
    End Sub
 
    Private Sub SélectionnertoutToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SélectionnertoutToolStripMenuItem.Click
        txtuser.SelectAll()
    End Sub
 
    Private Sub Imprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Imprimer.Click
        PrintDialog1.ShowDialog()
 
    End Sub
 
    Private Sub Ouvrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ouvrir.Click
        ' on paramètre la boîte de dialogue openfileDialog1
        OpenFileDialog1.InitialDirectory = Application.ExecutablePath
        OpenFileDialog1.Filter = "Fichiers texte (*.txt)|*.txt|Tous les fichiers (*.*)|*.*"
        OpenFileDialog1.FilterIndex = 0
        ' on affiche la boîte de dialogue et on récupère son résultat
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ' on récupère le nom du fichier
            Dim nomFichier As String = OpenFileDialog1.FileName
            Dim fichier As StreamReader = Nothing
            Try
                ' on ouvre le fichier en lecture
                fichier = New StreamReader(nomFichier)
                ' on lit tout le fichier et on le met dans le TextBox
                txtuser.Text = fichier.ReadToEnd
            Catch ex As Exception
                ' problème
                MessageBox.Show("Problème à la lecture du fichier (" & ex.Message & ")", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return
            Finally
                ' on ferme le fichier
                Try
                    fichier.Close()
                Catch
                End Try
            End Try
        End If
    End Sub
 
    Private Sub supprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles supprimer.Click
        txtuser.SelectedText = ""
    End Sub
 
    Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
 
        SaveFileDialog1.InitialDirectory = Application.ExecutablePath
        SaveFileDialog1.Filter = "Fichier Texte (*.txt)|*.txt|Tous les fichiers(*.*)|*.*"
        SaveFileDialog1.FilterIndex = 0
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim nomFichier As String = SaveFileDialog1.FileName
            Dim fichier As StreamWriter = Nothing
            Try
                fichier = New StreamWriter(nomFichier)
                fichier.Write(txtuser.Text)
            Catch ex As Exception
                MessageBox.Show("Problème à l'écriture du fichier (" & ex.Message & ")", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return
            Finally
                Try
                    fichier.Close()
                Catch
                End Try
            End Try
        End If
    End Sub
 
    Private Sub AperçuavantimpressionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AperçuavantimpressionToolStripMenuItem.Click
        PrintPreviewDialog1.Show()
 
    End Sub
 
    Private Sub ÀproposdeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ÀproposdeToolStripMenuItem.Click
        Beep()
        propo.Show()
    End Sub
 
    Private Sub Nouveau_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Nouveau.Click
        txtuser.Text = ""
        'txtuser .Clear 
    End Sub
 
    Private Sub MiseEnPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MiseEnPageToolStripMenuItem.Click
        'Dim txt As New PageSetupDialog
        'txt.Document = New System.Drawing.Printing.PrintDocument()
        PageSetupDialog1.Document = New PrintDocument
        If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtuser.Text = PrintDialog1.ShowDialog
 
        End If
 
 
 
    End Sub
End Class

Aucun commentaire:

Enregistrer un commentaire