Link to home
Start Free TrialLog in
Avatar of arozzy
arozzy

asked on

Creating New Objects on a Winform and Saving all object when program running

Hi experts,

How can I add new object like buttons or listviews on a forms when program running and save created objects. And created objects are needed to be on the same form when program  running secondly and after.

I have used a contexmenustrip for adding buttons with right click. But when program  secondly runs, the created objects are not on the form.

How can I save objects all the time until user deleting objects.

Thank you

Oguzhan

    Private Sub BuNoktayaMasaEkleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuNoktayaMasaEkleToolStripMenuItem.Click
        Dim btn1 As New Button
        btn1.Text = "New Button"
        Dim x As Integer
        Dim y As Integer
        x = MousePosition.X
        y = MousePosition.Y
        btn1.Location = New Point(x, y)
        btn1.Size = New Size(100, 100)
        Me.Controls.Add(btn1)
        Me.ResumeLayout(True)
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Right...you'd have to save all the information about the added controls (location, size, text, etc.) in a file (possibly XML?), and then read the file on Form load and re-created those controls again.
Avatar of arozzy
arozzy

ASKER

Thanks for your reply,

How can I add click event for these buttons ? Please help

    Private Sub Form25_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dset As New DataTable
        adptr.Fill(dset)
        For i = 0 To dset.Rows.Count - 1
            Dim button As New Button
            Dim aa As String
            aa = dset.Rows(i).Item(1).ToString
            button.Text = aa
            button.ForeColor = Color.Black
            button.Font = New Font("Arial", 12, FontStyle.Bold)
            Dim x As Integer
            Dim y As Integer
            x = 60 + x + 50
            y = 50
            button.Location = New Point(x, y)
            button.Size = New Size(100, 100)
            Me.Controls.Add(button)
            Me.ResumeLayout(True)
        Next
Avatar of arozzy

ASKER

Problem solved with this,

Thank you again

button.ContextMenuStrip = ContextMenuStrip1
To wire up dynamic controls, use the AddHandler() method.