1 pièce(s) jointe(s)
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:
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:
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:
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é ???
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
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.
Problème de serialization ?
Citation:
Envoyé par
sinople
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:
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 |