Exercice Visual Basic : Bloc Note

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

Interface Graphique:



123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161Imports System.IOImports 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 SubEnd Class
Article publié le 07 Janvier 2012