Exercice Visual Basic : Bloc Note

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

Interface Graphique:

ExerciceVB-id2139


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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