Serialization Help!!!!!!!!!!

M

Marc

Hi,

I have simple single screen project that allows the user to drag and
drop 'buttons' around the form. I also allow the user to add new
buttons. I need to save any new buttons added to the form when the
exexcutable is closed and re-opened.

I understand serialization is the way to do this but I cannot get it
working. Can anyone save me from many more hours of frustration?? If it
helps I have attached the code below. (note this is pre any
serialization attempts!)

Thanks Very much

Marc


Public Class controldrag
Inherits System.Windows.Forms.Form
Public Dragging As Boolean
Public mousex, mousey As Integer
Friend WithEvents btnMove3 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents ContextMenuStrip1 As
System.Windows.Forms.ContextMenuStrip
Friend WithEvents DeleteToolStripMenuItem As
System.Windows.Forms.ToolStripMenuItem
Friend WithEvents RenameToolStripMenuItem As
System.Windows.Forms.ToolStripMenuItem
Friend WithEvents btnMove2 As System.Windows.Forms.Button
#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnMove As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.btnMove = New System.Windows.Forms.Button
Me.ContextMenuStrip1 = New
System.Windows.Forms.ContextMenuStrip(Me.components)
Me.DeleteToolStripMenuItem = New
System.Windows.Forms.ToolStripMenuItem
Me.RenameToolStripMenuItem = New
System.Windows.Forms.ToolStripMenuItem
Me.btnMove2 = New System.Windows.Forms.Button
Me.btnMove3 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.ContextMenuStrip1.SuspendLayout()
Me.SuspendLayout()
'
'btnMove
'
Me.btnMove.ContextMenuStrip = Me.ContextMenuStrip1
Me.btnMove.Location = New System.Drawing.Point(12, 41)
Me.btnMove.Name = "btnMove"
Me.btnMove.Size = New System.Drawing.Size(75, 23)
Me.btnMove.TabIndex = 0
Me.btnMove.Text = "Move Me"
'
'ContextMenuStrip1
'
Me.ContextMenuStrip1.Items.AddRange(New
System.Windows.Forms.ToolStripItem() {Me.DeleteToolStripMenuItem,
Me.RenameToolStripMenuItem})
Me.ContextMenuStrip1.Name = "ContextMenuStrip1"
Me.ContextMenuStrip1.Size = New System.Drawing.Size(125, 48)
'
'DeleteToolStripMenuItem
'
Me.DeleteToolStripMenuItem.Name = "DeleteToolStripMenuItem"
Me.DeleteToolStripMenuItem.Size = New System.Drawing.Size(124,
22)
Me.DeleteToolStripMenuItem.Text = "Delete"
'
'RenameToolStripMenuItem
'
Me.RenameToolStripMenuItem.Name = "RenameToolStripMenuItem"
Me.RenameToolStripMenuItem.Size = New System.Drawing.Size(124,
22)
Me.RenameToolStripMenuItem.Text = "Rename"
'
'btnMove2
'
Me.btnMove2.ContextMenuStrip = Me.ContextMenuStrip1
Me.btnMove2.Location = New System.Drawing.Point(12, 12)
Me.btnMove2.Name = "btnMove2"
Me.btnMove2.Size = New System.Drawing.Size(75, 23)
Me.btnMove2.TabIndex = 1
Me.btnMove2.Text = "Move Me"
'
'btnMove3
'
Me.btnMove3.ContextMenuStrip = Me.ContextMenuStrip1
Me.btnMove3.Location = New System.Drawing.Point(12, 70)
Me.btnMove3.Name = "btnMove3"
Me.btnMove3.Size = New System.Drawing.Size(75, 23)
Me.btnMove3.TabIndex = 2
Me.btnMove3.Text = "Move Me"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(42, 500)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(103, 66)
Me.Button2.TabIndex = 3
Me.Button2.Text = "Button1"
Me.Button2.UseVisualStyleBackColor = True
'
'controldrag
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 294)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.btnMove3)
Me.Controls.Add(Me.btnMove2)
Me.Controls.Add(Me.btnMove)
Me.IsMdiContainer = True
Me.Name = "controldrag"
Me.Text = "controldrag"
Me.ContextMenuStrip1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region
Private Sub main_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
End Sub

Private Sub Thebutton_MouseDown(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim theButton As Button = DirectCast(sender, Button) ' cast

If e.Button = Windows.Forms.MouseButtons.Left Then
Dragging = True
mousex = -e.X
mousey = -e.Y
Dim clipleft As Integer = Me.PointToClient(MousePosition).X
- theButton.Location.X()
Dim cliptop As Integer = Me.PointToClient(MousePosition).Y
- theButton.Location.Y()
Dim clipwidth As Integer = Me.ClientSize.Width -
(theButton.Width - clipleft)
Dim clipheight As Integer = Me.ClientSize.Height -
(theButton.Height - cliptop)
Windows.Forms.Cursor.Clip = Me.RectangleToScreen( _
New Rectangle(clipleft, cliptop, clipwidth,
clipheight))
theButton.Invalidate()
End If
End Sub


Private Sub Thebutton_MouseMove(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim theButton As Button = DirectCast(sender, Button) ' cast
If Dragging Then
'move control to new position
Dim MPosition As New Point()
MPosition = Me.PointToClient(MousePosition)
MPosition.Offset(mousex, mousey)
'ensure control cannot leave container
theButton.Location = MPosition
End If
End Sub

Private Sub Thebutton_MouseUp(ByVal sender As Object, ByVal e As
MouseEventArgs)
Dim theButton As Button = DirectCast(sender, Button) ' cast
If Dragging Then
'end the dragging
Dragging = False
'Me.Capture = False
Cursor.Clip = Nothing
theButton.Invalidate()
End If
End Sub


Private Sub controldrag_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
AddHandler btnMove.MouseDown, AddressOf Me.Thebutton_MouseDown
AddHandler btnMove.MouseMove, AddressOf Me.Thebutton_MouseMove
AddHandler btnMove.MouseUp, AddressOf Me.Thebutton_MouseUp
AddHandler btnMove2.MouseDown, AddressOf Me.Thebutton_MouseDown
AddHandler btnMove2.MouseMove, AddressOf Me.Thebutton_MouseMove
AddHandler btnMove2.MouseUp, AddressOf Me.Thebutton_MouseUp
AddHandler btnMove3.MouseDown, AddressOf Me.Thebutton_MouseDown
AddHandler btnMove3.MouseMove, AddressOf Me.Thebutton_MouseMove
AddHandler btnMove3.MouseUp, AddressOf Me.Thebutton_MouseUp
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
' Create a button and add it to the form.
Dim test As New Button()
' Anchor the button to the bottom right corner of the form
'test.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
' Assign a background image.
'test.BackgroundImage = imageList1.Images(0)
' Specify the layout style of the background image. Tile is the
default.
'button1.BackgroundImageLayout = ImageLayout.Center
' Make the button the same size as the image.
'button1.Size = button1.BackgroundImage.Size
' Set the button's TabIndex and TabStop properties.
' button1.TabIndex = 1
' button1.TabStop = True
' Add delegate to handle the Click event.
AddHandler test.MouseDown, AddressOf Me.Thebutton_MouseDown
AddHandler test.MouseMove, AddressOf Me.Thebutton_MouseMove
AddHandler test.MouseUp, AddressOf Me.Thebutton_MouseUp

' Add the button to the form.
test.Text = "New Item!!!"
Me.Controls.Add(test)
End Sub

'Button(btn = New Button())
'btn.Text = userText.Text
' add events
' AddHandler btn.click, AddressOf theclickhandler
'...
' btn.location = New Point(x, y)
' Me.Controls.Add(btn)

Private Sub ContextMenuStrip1_Opening(ByVal sender As
System.Object, ByVal e As System.ComponentModel.CancelEventArgs)
Handles ContextMenuStrip1.Opening

End Sub
End Class
 
T

tommaso.gastaldi

Marc ha scritto:
Hi,

I have simple single screen project that allows the user to drag and
drop 'buttons' around the form. I also allow the user to add new
buttons. I need to save any new buttons added to the form when the
exexcutable is closed and re-opened.

I understand serialization is the way to do this but I cannot get it
working. Can anyone save me from many more hours of frustration?? If it
helps I have attached the code below. (note this is pre any
serialization attempts!)

Thanks Very much

Marc
I would not apply the concept of serialization here.

If you are adding dinamically controls, just recreate
them when you Load the form.

If you want to save some settings relevant to the interface
create the appropriate classes to store the info and serialize them.

I have seen some programmers also store interface setting in the
registry.
I tend not to do that. Others may have different opinions ...

Tommaso
 
M

Marc

Thanks Tommaso,

so if a 'user' adds a new button to the form, how can that button be
stored in the main executable so it tis there next time the program is
opened?
 
T

tommaso.gastaldi

Hi marc,

Usually one (at least me :) ) does not "store" buttons. You simply run
a sub that
recreates it at load time.

Tommaso

Marc ha scritto:
 
C

Chris Dunaway

Hi marc,

Usually one (at least me :) ) does not "store" buttons. You simply run
a sub that
recreates it at load time.

Yes, but when the user of the application creates a button that did not
previously exist, the program will need to store some information in
order to recreate the button the next time the program is run.

I don't know how the OP is structured, but I imagine he has a set of
actions or commands that can be executed by the button so he would have
to store the physical characteristics of the button such as it's size,
position, and caption and also what action the button must perform.

Then when the program re-loads, it can read this data, dynamically
create a button using this information.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top