dimanche 11 novembre 2012

Exercice Visual Basic : Adhérant


Ecrire le programme VB qui permet de réaliser l'interface suivante:
  • La boutton "+" sert à afficher un nouveau Formulaire dans la même Interface qui permet d'ajouter un nouveau Adhérant et l'enregister dans une Base de donnée
  • La boutton avec l'icône de disquette sert à Enregestrer les modéfication et les nouveau information concernant un Adhérant dans une Base de donnée
  • Les bouttons avec les flèches serts à faire la navigation 
  • La Boutton "X" sert à supprimer les données d'un Adhérant
  • La Boutton Rechercher sert à afficher un formulaire dans le même Interface pour la recherche d'un Adhérant 
  • La Boutton "C" sert à annuler l'action en cour



Corrigé


Public Class Form2
    Inherits System.Windows.Forms.Form
    Dim rownumber As Integer
    Dim caption As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Odataset.Clear()
        Odataset.Reset()
        Connection()
        Ocommande.CommandText = "select * FROM ADHERANTS ;"
        Ocommande.Connection = Oconnexion
        Odadapter.SelectCommand = Ocommande
        Odadapter.Fill(Odataset)
        Otable = Odataset.Tables(0)
        Remplirchamps(rownumber)
        If Otable.Rows.Count < 1 Then
            PictureBox3.Enabled = False
            PictureBox4.Enabled = False
            PictureBox5.Enabled = False
            PictureBox6.Enabled = False
            PictureBox8.Enabled = False
            PictureBox9.Enabled = False
        End If
        PictureBox1.Image = Image.FromFile(Application.StartupPath & "\img\add.yo")
        PictureBox2.Image = Image.FromFile(Application.StartupPath & "\img\save.yo")
        PictureBox3.Image = Image.FromFile(Application.StartupPath & "\img\next.yo")
        PictureBox4.Image = Image.FromFile(Application.StartupPath & "\img\pre.yo")
        PictureBox5.Image = Image.FromFile(Application.StartupPath & "\img\del.yo")
        PictureBox6.Image = Image.FromFile(Application.StartupPath & "\img\search.yo")
        PictureBox7.Image = Image.FromFile(Application.StartupPath & "\img\cancel.yo")
        PictureBox8.Image = Image.FromFile(Application.StartupPath & "\img\first.yo")
        PictureBox9.Image = Image.FromFile(Application.StartupPath & "\img\last.yo")
 
    End Sub
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        'preparation d'ajout
        Viderchamps()
        Oligne = Odataset.Tables(0).NewRow
        PictureBox1.Enabled = False
        PictureBox3.Enabled = False
        PictureBox4.Enabled = False
        PictureBox5.Enabled = False
        PictureBox6.Enabled = False
        PictureBox8.Enabled = False
        PictureBox9.Enabled = False
        PictureBox2.Enabled = True
        PictureBox7.Enabled = True
        Panel1.Visible = True
        Panel2.Visible = False
        TxtNom.Focus()
    End Sub
    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        'ajout d'adherants
        If Me.TxtCode.Text = "" Or Me.TxtPrenom.Text = "" Or Me.TxtNom.Text = "" Or TxtDNais.TextLength < 8 Or TxtDNais.TextLength > 10 Or TxtDInsc.TextLength < 8 Or TxtDInsc.TextLength > 10 Then
            MsgBox("Il faut remplire les champs d'abord !§!", MsgBoxStyle.Information, "Smah lia")
        Else
            Oligne("code") = Me.TxtCode.Text
            Oligne("nom") = Me.TxtNom.Text
            Oligne("prenom") = Me.TxtPrenom.Text
            Oligne("typefor") = Me.ComboForm.Text
            Oligne("duree") = Me.ComboDuree.Text
            Oligne("niveauconn") = Me.ComboConn.Text
            Oligne("datenaissance") = Me.TxtDNais.Text
            Oligne("lieunaissance") = Me.TxtLNais.Text
            Oligne("niveauscol") = Me.TxtNScol.Text
            Oligne("institut") = Me.TxtInst.Text
            Oligne("adresse") = Me.TxtAdresse.Text
            Oligne("tel") = Me.TxtTel.Text
            Oligne("dateinscription") = Me.TxtDInsc.Text
            Odataset.Tables(0).Rows.Add(Oligne)
            Ocbuilder.DataAdapter = Odadapter
            Odadapter.Update(Odataset)
            Odataset.Clear()
            Odadapter.Fill(Odataset)
            Otable = Odataset.Tables(0)
            Remplirchamps(rownumber)
            PictureBox1.Enabled = True
            PictureBox3.Enabled = True
            PictureBox4.Enabled = True
            PictureBox5.Enabled = True
            PictureBox6.Enabled = True
            PictureBox8.Enabled = True
            PictureBox9.Enabled = True
            PictureBox2.Enabled = False
            PictureBox7.Enabled = False
            Panel1.Visible = False
            Panel2.Visible = True
        End If
    End Sub
    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        rownumber += 1
        If rownumber <= Otable.Rows.Count - 1 Then
            Remplirchamps(rownumber)
        Else
            rownumber -= 1
        End If
    End Sub
 
    Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
        rownumber -= 1
        If rownumber >= 0 Then
            Remplirchamps(rownumber)
        Else
            rownumber += 1
        End If
    End Sub
 
    Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
        PictureBox1.Enabled = True
        PictureBox3.Enabled = True
        PictureBox4.Enabled = True
        PictureBox5.Enabled = True
        PictureBox6.Enabled = True
        PictureBox8.Enabled = True
        PictureBox9.Enabled = True
        PictureBox2.Enabled = False
        PictureBox7.Enabled = False
        Panel1.Visible = False
        Panel3.Visible = False
        Panel2.Visible = True
    End Sub
    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        PictureBox1.Enabled = False
        PictureBox3.Enabled = False
        PictureBox4.Enabled = False
        PictureBox5.Enabled = False
        PictureBox6.Enabled = False
        PictureBox2.Enabled = False
        PictureBox8.Enabled = False
        PictureBox9.Enabled = False
        PictureBox7.Enabled = True
        Panel1.Visible = False
        Panel2.Visible = False
        Panel3.Visible = True
        TextBox1.Focus()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer
        For i = rownumber To Otable.Rows.Count - 1
            If TextBox1.Text = Otable.Rows(i).Item("code") Then
                Remplirchamps(i)
                Panel3.Visible = False
                Panel2.Visible = True
                Panel1.Visible = False
                PictureBox1.Enabled = True
                PictureBox3.Enabled = True
                PictureBox4.Enabled = True
                PictureBox5.Enabled = True
                PictureBox6.Enabled = True
                PictureBox8.Enabled = True
                PictureBox9.Enabled = True
                PictureBox2.Enabled = False
                PictureBox7.Enabled = False
                Exit For
            ElseIf i = Otable.Rows.Count - 1 Then
                MsgBox("Pas d'adhérant avec ce code la !§!", MsgBoxStyle.Information, "Smah lia")
            End If
        Next
    End Sub
    Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click
        rownumber = Otable.Rows.Count - 1
        Remplirchamps(rownumber)
    End Sub
    Private Sub PictureBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox8.Click
        rownumber = 0
        Remplirchamps(rownumber)
    End Sub
    Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
        Odataset.Tables(0).Rows(rownumber).Delete()
        Ocbuilder.DataAdapter = Odadapter
        Odadapter.Update(Odataset)
        Odataset.Clear()
        Odadapter.Fill(Odataset)
        rownumber = 0
        Otable = Odataset.Tables(rownumber)
        If Odataset.Tables(0).Rows.Count < 1 Then
            PictureBox5.Enabled = False
        End If
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim f4 As New Form3
        f4.Show()
    End Sub
 
    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        If (Label19.Left < Panel1.Width) Then
            Label19.Left += 6
        Else
            Label19.Left = -Panel1.Width
        End If
    End Sub
End Class
 
-----------------------------------------------------------------------------
 
 
Imports System.Data.OleDb
Module Module1
    Public Oconnexion As New OleDbConnection
    Public Ocommande As New OleDbCommand
    Public Odadapter As New OleDbDataAdapter
    Public Odataset As New DataSet
    Public Ocbuilder As New OleDbCommandBuilder
    Public Otable As New DataTable
    Public Oligne As DataRow
    Public strconn As String
    Public strsql As String
    Public comptopacity As Integer
    Public Sub Connection()
        strconn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=img\bd1.yo;"
        Try
            Oconnexion = New OleDbConnection
            Oconnexion.ConnectionString = strconn
            Oconnexion.Open()
        Catch ex As OleDbException
            MsgBox(ex.Message)
        End Try
    End Sub
End Module

Aucun commentaire:

Enregistrer un commentaire