IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

My.Setting rendre persistant des types perso


Sujet :

Windows Forms

  1. #1
    Futur Membre du Club
    Inscrit en
    Novembre 2011
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Novembre 2011
    Messages : 8
    Points : 6
    Points
    6
    Par défaut My.Setting rendre persistant des types perso
    Bonjour à tous,

    Je suis confronté à un problème que je n'arrive pas à résoudre aprés maintes recherche sur le net.

    Je veux enregistrer dans le fichier config de mon application en utilisant la classe settings.settings les positions de mes formulaires enfants de mon application MDI. J'ai beaucoup de formulaire et je ne veux pas créer plusieurs propriétés pour chacun. J'ai donc implémenté une collection de classe qui stocke dynamiquement les propriétés. Tout fonctionnement parfaitement, mais, lorsque l'application se ferme, les données ne se sauvegardent pas.

    Voici mon code :

    Classes :

    Code VB.NET : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Imports System.Windows.Forms.Form
     
    Namespace SettingUser
        <System.Serializable()> Public Class PositionFormCollection
            Inherits System.Collections.CollectionBase
            Implements Runtime.Serialization.ISerializable
     
            Sub New()
                MyBase.New()
            End Sub
            Public Sub Add(ByVal pForm As PositionForm)
                List.Add(pForm)
            End Sub
            Public Sub Remove(ByVal index As Integer)
                If index > Count - 1 Or index < 0 Then
                    Throw New Exception("Index not valid")
                Else
                    List.RemoveAt(index)
                End If
            End Sub
            Public ReadOnly Property Item(ByVal index As Integer) As PositionForm
                Get
                    Return CType(List.Item(index), PositionForm)
                End Get
            End Property
     
            Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
                If (info Is Nothing) Then Throw New ArgumentNullException("info")
                info.AddValue("List", Me.List)
            End Sub
     
        End Class
        <System.Serializable()> Public Class PositionForm
            Private _height As Integer
            Private _width As Integer
            Private _top As Integer
            Private _left As Integer
            Private _windowState As Windows.Forms.FormWindowState
            Private _name As String
            Sub New()
                '
            End Sub
            Sub New(ByVal name As String, ByVal height As Integer, ByVal width As Integer, ByVal top As Integer, ByVal left As Integer, ByVal windowState As Windows.Forms.FormWindowState)
                _height = height
                _width = width
                _top = top
                _left = left
                _windowState = WindowState
                _name = name
            End Sub
     
            Property Name() As String
                Get
                    Return _name
                End Get
                Set(ByVal value As String)
                    _name = value
                End Set
            End Property
            Property Height() As Integer
                Get
                    Return _height
                End Get
                Set(ByVal value As Integer)
                    _height = value
                End Set
            End Property
            Property Width() As Integer
                Get
                    Return _width
                End Get
                Set(ByVal value As Integer)
                    _width = value
                End Set
            End Property
            Property Top() As Integer
                Get
                    Return _top
                End Get
                Set(ByVal value As Integer)
                    _top = value
                End Set
            End Property
            Property Left() As Integer
                Get
                    Return _left
                End Get
                Set(ByVal value As Integer)
                    _left = value
                End Set
            End Property
            Property WindowState() As Windows.Forms.FormWindowState
                Get
                    Return _windowState
                End Get
                Set(ByVal value As Windows.Forms.FormWindowState)
                    _windowState = value
                End Set
            End Property
        End Class
    End Namespace

    Dans le settings.disigner.vb :

    Code VB.NET : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <Global.System.Configuration.UserScopedSettingAttribute(),  _
             Global.System.Diagnostics.DebuggerNonUserCodeAttribute()>  _
            Public Property FormP() As Global.UserClass.SettingUser.PositionFormCollection
                Get
                    Return CType(Me("FormP"),Global.UserClass.SettingUser.PositionFormCollection)
                End Get
                Set
                    Me("FormP") = value
                End Set
            End Property

    dans un form :

    Code VB.NET : Sélectionner tout - Visualiser dans une fenêtre à part
    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
        Private Sub baseForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Try
                If CType(My.Settings("FormP"), UserClass.SettingUser.PositionFormCollection) Is Nothing Then
                    CType(My.Settings("FormP"), UserClass.SettingUser.PositionFormCollection).Add(New UserClass.SettingUser.PositionForm("Form_" & Me.Name, Me.Height, Me.Width, Me.Top, Me.Left, Me.WindowState))
                Else
                    Dim find As Boolean = False
                    For Each elem As UserClass.SettingUser.PositionForm In CType(My.Settings("FormP"), UserClass.SettingUser.PositionFormCollection)
                        If elem.Name = "Form_" & Me.Name Then
                            With elem
                                .Width = Me.Width
                                .Height = Me.Height
                                .Top = Me.Top
                                .Left = Me.Left
                                .WindowState = Me.WindowState
                            End With
                            find = True
                            Exit For
                        End If
                    Next
                    If Not find Then
                        CType(My.Settings("FormP"), UserClass.SettingUser.PositionFormCollection).Add(New UserClass.SettingUser.PositionForm("Form_" & Me.Name, Me.Height, Me.Width, Me.Top, Me.Left, Me.WindowState))
                    End If
                End If
            Catch ex As Exception
     
            End Try
     
        End Sub
     
        Private Sub baseForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                   Try
                        For Each elem As UserClass.SettingUser.PositionForm In CType(My.Settings("FormP"), UserClass.SettingUser.PositionFormCollection)
                    If elem.Name = "Form_" & Me.Name Then
                        Me.Left = elem.Left
                        Me.Top = elem.Top
                        If elem.Height > 0 Then Me.Height = elem.Height
                        If elem.Width > 0 Then Me.Width = elem.Width
                        Me.WindowState = elem.WindowState
                        Exit For
                    End If
                Next
     
     
            Catch ex As Exception
     
            End Try
     
        End Sub

    Jusque là tout fonctionne parfaitement. A la ferméture de l'application les autres proriété de mon Settings s'enregistrent bien, mais pas celle la.

    Avez-vous une idée, un attribut de sérialisation oublié ???
    Images attachées Images attachées  

  2. #2
    Membre éprouvé
    Avatar de dkmix
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    619
    Détails du profil
    Informations personnelles :
    Localisation : Jamaïque

    Informations forums :
    Inscription : Septembre 2007
    Messages : 619
    Points : 924
    Points
    924
    Par défaut
    Bonjour,

    voir ici :

    http://msdn.microsoft.com/fr-fr/libr....80).aspx#EEAA

    notamment çà :

    Pour écrire et conserver des paramètres utilisateur lors de l'exécution

    Accédez au paramètre utilisateur et affectez-lui une nouvelle valeur, comme indiqué dans l'exemple suivant :

    Properties.Settings.Default.myColor = Color.AliceBlue;
    Si vous voulez conserver des modifications de paramètres utilisateur d'une session à une autre, appelez la méthode Save, comme indiqué dans le code suivant :

    Properties.Settings.Default.Save();

  3. #3
    Futur Membre du Club
    Inscrit en
    Novembre 2011
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Novembre 2011
    Messages : 8
    Points : 6
    Points
    6
    Par défaut Non cela ne fonctionne pas
    Salut,

    merci pour ta réponse, j'avais essayé , mais sans résultats. Cela ne conserve les données uniquement pour la session active. Une fois le programme fermé, il n'y a plus rien.

    Je poursuis me recherches

  4. #4
    Membre à l'essai
    Homme Profil pro
    Ingénieur software (débutant)
    Inscrit en
    Mars 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Ingénieur software (débutant)

    Informations forums :
    Inscription : Mars 2010
    Messages : 25
    Points : 22
    Points
    22
    Par défaut
    Bonjour,

    Je vous propose ce lien. vous pourrez peut être y trouver des informations utiles.

    Bonne chance.

  5. #5
    Futur Membre du Club
    Inscrit en
    Novembre 2011
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Novembre 2011
    Messages : 8
    Points : 6
    Points
    6
    Par défaut Presque ça!
    Salut,

    Merci pour ta réponse. Le seul problème qui persiste est que ma variable est une collection. Je n'arrive toujours pas à l'enregistrer comme telle.

    Mais je vais suivre ton idée.

    A+

  6. #6
    Membre à l'essai
    Homme Profil pro
    Ingénieur software (débutant)
    Inscrit en
    Mars 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Ingénieur software (débutant)

    Informations forums :
    Inscription : Mars 2010
    Messages : 25
    Points : 22
    Points
    22
    Par défaut Solution pas très 'Catholique'..
    Loin d'être une solution modèle, j'ai préféré pour ma part créer mes paramètres dans des Objets c# simples, que je sérialise/désérialise de XML. j'enregistre / ouvre depuis AppData (un special Folder de widows).

    Je sais que c'est loin d'être la meilleure solution ou la plus propre mais tu peux essayer ça aussi si tu bloques encore à faire persister tes valeurs.

  7. #7
    Membre chevronné
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2009
    Messages
    1 048
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Suisse

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2009
    Messages : 1 048
    Points : 2 201
    Points
    2 201
    Par défaut
    A priori je pense que les objets doivent implementer l'interface ISerializable afin de pouvoir être stocké à l'aide de ce moyen.

    Ce qui n'est peut être pas le cas de ta collection.

  8. #8
    Futur Membre du Club
    Inscrit en
    Novembre 2011
    Messages
    8
    Détails du profil
    Informations forums :
    Inscription : Novembre 2011
    Messages : 8
    Points : 6
    Points
    6
    Par défaut Problème de serialization ?
    Citation Envoyé par sinople Voir le message
    A priori je pense que les objets doivent implementer l'interface ISerializable afin de pouvoir être stocké à l'aide de ce moyen.

    Ce qui n'est peut être pas le cas de ta collection.
    Oui, je pense aussi que cela vienne de là. Voici la structure de mes classes, sans l'impémentation, si qq peut me corriger, se serait bien venu.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Namespace SettingUser
        <System.Serializable()> Public Class PositionFormCollection
            Inherits System.Collections.CollectionBase
            Implements Runtime.Serialization.ISerializable
     
            Sub New()
                MyBase.New()
            End Sub
            Public Sub Add(ByVal pForm As PositionForm)
                List.Add(pForm)
            End Sub
            Public Sub Remove(ByVal index As Integer)
                If index > Count - 1 Or index < 0 Then
                    Throw New Exception("Index not valid")
                Else
                    List.RemoveAt(index)
                End If
            End Sub
            Public ReadOnly Property Item(ByVal index As Integer) As PositionForm
                Get
                    Return CType(List.Item(index), PositionForm)
                End Get
            End Property
     
            Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
                If (info Is Nothing) Then Throw New ArgumentNullException("info")
                info.AddValue("List", Me.List)
            End Sub
     
        End Class
        <System.Serializable()> Public Class PositionForm
     
            Sub New()
            End Sub
            Sub New(ByVal name As String, ByVal height As Integer, ByVal width As Integer, ByVal top As Integer, ByVal left As Integer, ByVal windowState As Windows.Forms.FormWindowState)
            End Sub
     
            Property Name() As String
            End Property
            Property Height() As Integer
            End Property
            Property Width() As Integer
            End Property
            Property Top() As Integer
                        End Property
            Property Left() As Integer
                      End Property
            Property WindowState() As Windows.Forms.FormWindowState
            End Property
        End Class
    End Namespace

Discussions similaires

  1. Persistance des settings utilisateurs
    Par Kropernic dans le forum VB.NET
    Réponses: 3
    Dernier message: 29/05/2015, 10h32
  2. choix des types
    Par cali dans le forum Langage SQL
    Réponses: 3
    Dernier message: 10/08/2004, 13h16
  3. [Debutant]reallocation de memoire d'un tableau de type perso
    Par killerjeff dans le forum Débuter
    Réponses: 3
    Dernier message: 04/08/2004, 17h09
  4. Réponses: 2
    Dernier message: 22/09/2003, 11h23
  5. [ADO] Constantes des types de champ
    Par SpaceFrog dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 05/09/2002, 11h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo