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 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
'AJOUTER 3 CONTROLES:
'- DataGridView (pour voir l'ensemble des enregistrements)
'- BindingSource1(pour lier les controles au navigateur)
'- BindingNavigator1 (pour naviguer dans les TextBox avec un Click)
'NB: les ComboBox sont inutiles
'NB: btnAppend est supprime (a remplacer par un DataGridView)
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Reflection
Public Class Form1
'Enum definit tes largeurs de colonne dans l'ordre d'apparition(en nombre de caracteres)
Public Enum largCol
col1 = 3 '=largeur colon capital initial+espace
col2 = 3 '=colonne NbreAnnees+espace
col3 = 2 '=colonne Taux+espace
col4 = 5 '=colonne CapitalFinal+espace
End Enum
Private PATHFICHIER As String = Directory.GetCurrentDirectory & "\Resultat.txt"
Private PATHRESULTAT As String = Directory.GetCurrentDirectory & "\datResultat.txt"
Dim dtCapital As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Label5.Visible = False
Me.Label5.ForeColor = Color.Red
'RAZ DES TEXTBOX
Me.txtCapitalInitial.Text = ""
Me.txtNbreAnnees.Text = ""
Me.txtTaux.Text = ""
Me.txtCapitalFinal.Text = ""
'INIT UN DataTable dtCapital
dtCapital = New DataTable
'ET CREE LES 4 COLONNES DANS L'ORDRE
Dim colType As DataColumn = New DataColumn
colType = dtCapital.Columns.Add("CapitalInitial", GetType(Double))
colType.DefaultValue = 0.0
colType = dtCapital.Columns.Add("NbreAnnees", GetType(Integer))
colType.DefaultValue = 0
colType = dtCapital.Columns.Add("Taux", GetType(Integer))
colType.DefaultValue = 0
colType = dtCapital.Columns.Add("CapitalFinal", GetType(Double))
colType.DefaultValue = 0
End Sub
Private Sub QuitterToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitterToolStripMenuItem.Click
If MsgBox("Souhaitez-vous vraiment quitter ce programme ?", 36, "Quitter") = MsgBoxResult.Yes Then
End
End If
End Sub
'-----------------Lecture utilise TextFieldParser---------------------
Private Sub btnRAZ_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRAZ.Click
Me.txtCapitalInitial.Text = ""
Me.txtNbreAnnees.Text = ""
Me.txtTaux.Text = ""
Me.txtCapitalFinal.Text = ""
End Sub
'-----------------Lecture utilise TextFieldParser---------------------
Private Sub btnReadAll_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadAll.Click
If Not File.Exists(PATHFICHIER) Then
MessageBox.Show(PATHFICHIER & " n'existe pas...")
Return
End If
'ligne texte
Dim ligneLue() As String
Using monReader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser(PATHFICHIER)
'Fichier Fixe (SDF)
monReader.TextFieldType = FileIO.FieldType.FixedWidth
'Largeur colonne
monReader.SetFieldWidths(largCol.col1, largCol.col2, largCol.col3, largCol.col4)
'ligne DataTable
Dim dr As DataRow
While Not monReader.EndOfData
Try
ligneLue = monReader.ReadFields()
dr = dtCapital.NewRow
dr.ItemArray = ligneLue
dtCapital.Rows.Add(dr)
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Ligne Invalide " & ex.Message & _
"sera sautee...")
End Try
'ligne suivante
End While
MessageBox.Show("Fin de fichier Atteinte...")
monReader.Close()
End Using
Me.BindingSource1.DataSource = dtCapital
Me.BindingSource1.DataMember = dtCapital.TableName
Me.BindingNavigator1.BindingSource = Me.BindingSource1
Me.DataGridView1.DataSource = Me.BindingSource1
'Lie les controles a BindingSource1
Me.txtCapitalInitial.DataBindings.Add(New Binding("Text", Me.BindingSource1, dtCapital.Columns(0).ColumnName, True))
Me.txtNbreAnnees.DataBindings.Add(New Binding("Text", Me.BindingSource1, dtCapital.Columns(1).ColumnName, True))
Me.txtTaux.DataBindings.Add(New Binding("Text", Me.BindingSource1, dtCapital.Columns(2).ColumnName, True))
Me.txtCapitalFinal.DataBindings.Add(New Binding("Text", Me.BindingSource1, dtCapital.Columns(3).ColumnName, True))
'Desactive
btnReadAll.Enabled = False
End Sub
'-----------------Ecriture utilise StreamWriter---------------------
Private Sub btnWriteALL_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteALL.Click
Using MonWriter As New StreamWriter(PATHRESULTAT, False)
Try
Dim strLigne As String = ""
Dim Espace = " "
For Each dr As DataRow In dtCapital.Rows
For Each dc As DataColumn In dtCapital.Columns
strLigne = strLigne & dr(dc) & Espace
Next
'strLigne = strLigne & Strings.RSet(DR.Item(0).ToString, 2) & Espace & Strings.RSet(DR.Item(1).ToString, 2) & Espace & Strings.RSet(DR.Item(2).ToString, 1) & Espace & Strings.RSet(DR.Item(3).ToString, 5)
MonWriter.WriteLine(strLigne)
' ligne suivante
strLigne = ""
Next
Catch ex As Exception
MessageBox.Show("Erreur d'ecriture fichier " & ex.Message)
End Try
MessageBox.Show("Fichier Sortie Resultat.dat...")
MonWriter.Close()
End Using
'----------------------NOUVELLE SESSION RESET DES CONTROLES-------------
'VIDE LA TABLE
dtCapital.Clear()
'SUPPRIME LES LIAISONS DES CONTROLES TEXTBOX
Me.txtCapitalInitial.DataBindings.Clear()
Me.txtNbreAnnees.DataBindings.Clear()
Me.txtTaux.DataBindings.Clear()
Me.txtCapitalFinal.DataBindings.Clear()
Me.DataGridView1.Refresh()
'ACTIVE btnReadAll
btnReadAll.Enabled = True
End Sub
Private Sub btnCalcul_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcul.Click
If Verification() Then
Try
Dim valCalc As Double = Calcul(Double.Parse(Me.txtCapitalInitial.Text), Double.Parse(Me.txtTaux.Text), Double.Parse(Me.txtNbreAnnees.Text))
Me.txtCapitalFinal.Text = Math.Round(valCalc, 2).ToString
Catch ex As Exception
MessageBox.Show("erreur donnee..." & ex.ToString)
End Try
Else
Me.Label5.Visible = True
End If
End Sub
Function Verification() As Boolean
Dim Bon As Boolean = True
If Me.txtCapitalInitial.Text Is Nothing Or Not IsNumeric(Me.txtCapitalInitial.Text) Then
Bon = False
End If
Return Bon
End Function
Function Calcul(ByVal VALtxtCapitalInitial As Double, ByVal VALcboTaux As Double, ByVal VALcboNbreAnnees As Double) As Double
Dim D As Double = VALtxtCapitalInitial
Dim z As Integer = 0
While z <> VALcboNbreAnnees
D = D * (1 + VALcboTaux / 100)
z = z + 1
End While
Return D
End Function
End Class |
Partager