Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Guys!
I'm working a project with window form and C#.
My idea for this project is, when the user run app, they can change interface of app, Example: Create new label, change the label location, or change color of button.
Therefore I want my apps can save all change interface by user after close app.
Below is my code, i just write for it create new label.
if can, please share me your code or your project you did it.
Thanks!


What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool selected = false;
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        int B = 1;
        public System.Windows.Forms.Label AddNewLabel()
        {
            System.Windows.Forms.Label newLabel = new System.Windows.Forms.Label();
            this.Controls.Add(newLabel);
            newLabel.Top = B * 28;
            newLabel.Left = 50;
            newLabel.Text = "DOOR EXIT " + this.B.ToString();
            B = B + 1;
            newLabel.BorderStyle = BorderStyle.FixedSingle;
            newLabel.TextAlign = ContentAlignment.MiddleCenter;
            return newLabel;
        }
        private void btn_Create_Click_1(object sender, EventArgs e)
        {
            AddNewLabel();
        }
    }
}
Posted
Updated 6-Nov-19 0:26am

You can Create a Control generation class for this and the object of class can be serialize and deserialize and can be save in to the file or db.
Second approach you can define some meta-data notations for the controls and its properties and can be save it in to the database or xml file as per your requirement.
 
Share this answer
 
Comments
Member 10452637 6-Nov-19 6:26am    
Yes, Thanks! I tried with your suggest, and it can serialize to file when i click backup button, but when i click [restore] button at my interface, it can't deserializer. Could you help me please! my code is below:
the class "setting infor" to use take tpye of label
C#
<pre>using System;                     
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml.Serialization;

namespace Form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<Label> listLabel = new List<Label>();
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        int B = 1;
        private void btn_Create_Click_1(object sender, EventArgs e)
        {
            System.Windows.Forms.Label newLabel = new System.Windows.Forms.Label();
            this.Controls.Add(newLabel);
            newLabel.Top = B * 28;
            newLabel.Left = 50;
            newLabel.Text = "DOOR EXIT " + this.B.ToString();
            B = B + 1;
            newLabel.BorderStyle = BorderStyle.FixedSingle;
            newLabel.TextAlign = ContentAlignment.MiddleCenter;
            listLabel.Add(newLabel);
        }
        void SerializeButton()
        {

        }
        void Serialize()
        {
            StreamWriter sw = new StreamWriter(new FileStream("Setting.TestSave", FileMode.Create));
            XmlSerializer seri2 = new XmlSerializer(typeof(int));
            seri2.Serialize(sw, listLabel.Count);
            foreach (var item in listLabel)
            {
                SettingInfor info = new SettingInfor();
                info.text = item.Text;
                info.location = item.Location;                
                XmlSerializer seri = new XmlSerializer(typeof(SettingInfor));                
                seri.Serialize(sw, info);
            }
            sw.Close();
        }
        void Deserialize()
        {
            listLabel.Clear();
            StreamReader sr = new StreamReader(new FileStream("Setting.TestSave", FileMode.Open));
            XmlSerializer seri2 = new XmlSerializer(typeof(int));
            //seri2.Serialize(sw, listLabel.Count);
            
            for (int i = 0; i<(int)seri2.Deserialize(sr);i++)
            {
                XmlSerializer seri = new XmlSerializer(typeof(SettingInfor));
                SettingInfor info = new SettingInfor();
                info = (SettingInfor)seri.Deserialize(sr);

                Label newLabel = new Label();
                newLabel.Text = info.text;
                newLabel.Location = info.location;
                listLabel.Add(newLabel);
                this.Controls.Add(newLabel);
               
            }
            sr.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Serialize();
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {

        }
    }
}

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Form1
{
public class SettingInfor
{
public string text;
public Point location;
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900