Hello,
My question is not so simple:
I have a WindowsForm with a diffrent controls like: textboxes, comboboxes, checkboxes, datetimepickers, listview and so on...
How can i create temporary data at runtime in an array /I dont want to use My.Settings/ which will save all control values and when needed to reload them back from that specific array?
Thanks for any help!
Reverend Jim 4,968 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
Temporary data, by definition, does not need to be saved. If you are referring to saving in one session, then restoring it in another, you would use either My.Settings (which you don't seem to want to use) or some other external store like a database or other file. Typically you would store "like" values in arrays. Holw you store the data depends on the format of the data. Can you give us more details please?
DobyMoby 0 Newbie Poster
Well,
It depends of the user to fill the mensioned controls /32 in total/ on the form and temporary save those values in array until the moment of final save. Which means that I need and some point, by clicking a button 'Temp.Save' to store those number of values,then clear the form and when the user calls for stored data(Restore.Temp.save) for final correction to load the values again as they are at the store point!
I hope is not confusing...
rproffitt 2,662 "Nothing to see here." Moderator
Let me think back to my PalmOS apps. They had no My.Settings feature so I would have to add code that ran on a form load to read from saved settings.
All hand coded so here you are with an all too similar issue or feature you want to code up. So what's the next step? Write code now that you know what you want it to do.
In parting, this is likely done in your form.load event handler.
Reverend Jim 4,968 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
I find the Tag
property to be a handy place to store things. Every control has this property and it is unused (available for custom use). Since it is common to all controls, you could just iterate through all of the controls on a form or in a group and just store your data there. For example
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
For Each ctl As Control In Me.Controls
ctl.Tag = ctl.Name
Next
End Sub
Tags are not limited to just strings.
Edited by Reverend Jim
xrjf 213 Posting Whiz
The following code may be a poor solution, but as far as I know controls are not serializable unless having a custom serializable class for each type of control. This is a workaround. Another possibility could be to get properties through Reflexion, but again gaining complexity.
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Public Class _De_Serialize
Dim filePath As String = "C:\Users\Public\controls.dat"
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
Dim fs As FileStream = Nothing
Try
fs = New FileStream(filePath, FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter
Dim cnt As Int32 = 0
Dim vCtl(-1) As Control
For Each ctr In Me.Controls
' ctls. having a numeric tag will be saved:'
If IsNumeric(ctr.tag) Then
ReDim Preserve vCtl(cnt)
vCtl(cnt) = ctr
cnt += 1
End If
Next
bf.Serialize(fs, cnt.ToString) ' save how many '
For i = 0 To cnt - 1
Dim ctr As Control = vCtl(i) ' next control to save'
Dim sType As String = LCase(ctr.GetType.ToString)
bf.Serialize(fs, sType)
bf.Serialize(fs, ctr.Name.ToString)
bf.Serialize(fs, ctr.Text)
bf.Serialize(fs, ctr.Location.X.ToString)
bf.Serialize(fs, ctr.Location.Y.ToString)
'...eventually save more properties '
Me.Controls.Remove(ctr)
Next
Catch ex As Exception
Finally
If fs IsNot Nothing Then
fs.Close()
End If
End Try
End Sub
Private Sub btnRestore_Click(sender As System.Object, e As System.EventArgs) Handles btnRestore.Click
Dim fs As FileStream = Nothing
Try
If Not IO.File.Exists(filePath) Then
Exit Sub
End If
fs = New FileStream(filePath, FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter
' Get how many controls were saved: '
Dim cnt As Int32 = Int32.Parse(bf.Deserialize(fs))
For i As Int32 = 0 To cnt - 1
Dim sType As String = bf.Deserialize(fs)
Dim lbl As Label = Nothing
Dim chkbox As CheckBox = Nothing
Dim btn As Button = Nothing
Dim ctr As Control = Nothing
If InStr(sType, ".button") Then
btn = New Button
ctr = btn
ElseIf InStr(sType, ".checkbox") Then
chkbox = New CheckBox
ctr = chkbox
ElseIf InStr(sType, ".label") Then
lbl = New Label
ctr = lbl
End If
ctr.Tag = "1"
ctr.Name = bf.Deserialize(fs)
ctr.Text = bf.Deserialize(fs)
Dim X As Int32 = Int32.Parse(bf.Deserialize(fs))
Dim Y As Int32 = Int32.Parse(bf.Deserialize(fs))
ctr.Location = New Point(X, Y)
Me.Controls.Add(ctr)
Next
Catch ex As Exception
Finally
If fs IsNot Nothing Then
fs.Close()
End If
End Try
End Sub
End Class
xrjf 213 Posting Whiz
Below lines #32 and #72 you could add these two snippets in order to remember if the checkbox is checked or not.
If InStr(sType, ".button") Then
bf.Serialize(fs, "")
ElseIf InStr(sType, ".checkbox") Then
bf.Serialize(fs, CType(ctr, CheckBox).Checked.ToString)
ElseIf InStr(sType, ".label") Then
bf.Serialize(fs, "")
End If
Dim checked As String = bf.Deserialize(fs)
If InStr(sType, ".checkbox") Then
If checked = "True" Then
CType(ctr, CheckBox).Checked = True
Else
CType(ctr, CheckBox).Checked = False
End If
End If
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.