如何保存控制数组

发布于 2024-09-27 20:34:07 字数 10937 浏览 0 评论 0原文

我有这段代码,允许用户创建一个按钮数组,任何一个都在 from 中。 此处这里是我正在做的两个示例(在立即创建帐户?单击“否”,然后下载 zip 文件)。

问题如下:

我需要保存用户所做的一切:例如,如果他添加了 5 个按钮并保存了它,下次当他打开保存的文件时,他会在保存它们的同一位置看到 5 按钮。 这将允许用户使用相同的程序将文件发送给另一个人,然后另一个人查看创建的文件。


由于误解(下面的评论),我将在上面的第二个链接中发布一些代码。但问题是我找不到任何代码来执行我所要求的操作。伙计们,我不知道如何开始,我的意思是我知道如何从图片框或一些文本中保存图像,但按钮数组是另一回事。

这是代码简单的示例代码,或者您可以下载我发布的第二个文件并编译它。

ButtomArray.cs

using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ManageControls
{
    public delegate void SendSelectedButton(object sender);
    public class ButtonArray : Hashtable
    {
        private readonly Form HostForm;
        public event SendSelectedButton SelectedButton;
        Point buttonLocation;
        int cntButton = 0;

        public ButtonArray(Form host)
        {
            HostForm = host;
        }

        public void AddButton(int left, int top)
        {
           Button btnElement = new Button();

           btnElement.Top = top;
           btnElement.Left = left;
           btnElement.Tag = cntButton;
           btnElement.Cursor = System.Windows.Forms.Cursors.Default;
           btnElement.FlatStyle = System.Windows.Forms.FlatStyle.System;
           btnElement.Text = "Button " + cntButton.ToString();
           btnElement.Click += new EventHandler(btnElement_Click);
           btnElement.MouseDown += new MouseEventHandler(btnElement_MouseDown);
           btnElement.MouseMove += new MouseEventHandler(btnElement_MouseMove);
           this.Add(cntButton.ToString(), btnElement);
           HostForm.Controls.Add(btnElement);
           btnElement.BringToFront();
           cntButton++;
        }

        public void RemoveButton(string btnIndex)
        {
            if (this.Count > 0)
            {
                HostForm.Controls.Remove((Button)this[btnIndex]);
                this.Remove(btnIndex);
            }
        }

        private void btnElement_Click(Object sender, System.EventArgs e)
        {
            if(null != SelectedButton)
                SelectedButton(sender);
        }

       private void btnElement_MouseDown(object sender, MouseEventArgs e)
       {
           buttonLocation = e.Location;
       }

       private void btnElement_MouseMove(object sender, MouseEventArgs e)
       {
           if (e.Button == MouseButtons.Left)
          {
              ((Button)sender).Left += e.X - buttonLocation.X;
               ((Button)sender).Top += e.Y - buttonLocation.Y;
          }
       }

    }
}

frmMain.cs

using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ManageControls
{
    public partial class frmMain : Form
    {
        ButtonArray buttonArray;
        bool isClicked = false;
        Button btnSelected = new Button();


        public frmMain()
        {
            InitializeComponent();
            buttonArray = new ButtonArray(this);

            this.MouseDown += new  MouseEventHandler(frmMain_MouseDown);
            buttonArray.SelectedButton += new
                SendSelectedButton(buttonArray_SelectedButton);
        }

        private void buttonArray_SelectedButton(object sender)
        {
            btnSelected = sender as Button;

        }

        private void frmMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (isClicked)
            {
                buttonArray.AddButton(e.X, e.Y);
                this.Cursor = Cursors.Default;
                isClicked = false;
            }
        }

        private void tsButton_Click(object sender, EventArgs e)
        {
            isClicked = true;
            this.Cursor = Cursors.Cross;
        }

        private void tsDelete_Click(object sender, EventArgs e)
        {
            buttonArray.RemoveButton(btnSelected.Tag.ToString());
        }


    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ManageControls
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain());
        }
    }
}

和 frmMain.Designer.cs

namespace ManageControls
{
    partial class frmMain
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tsMain = new System.Windows.Forms.ToolStrip();
            this.AddButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton1 = new System.Windows.Forms.ToolStripSeparator();
            this.RemoveButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.openLabel1 = new System.Windows.Forms.ToolStripLabel();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.saveLabel2 = new System.Windows.Forms.ToolStripLabel();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.tsMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // tsMain
            // 
            this.tsMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.AddButton,
            this.toolStripButton1,
            this.RemoveButton,
            this.toolStripSeparator1,
            this.openLabel1,
            this.toolStripSeparator2,
            this.saveLabel2,
            this.toolStripSeparator3});
            this.tsMain.Location = new System.Drawing.Point(0, 0);
            this.tsMain.Name = "tsMain";
            this.tsMain.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            this.tsMain.Size = new System.Drawing.Size(740, 25);
            this.tsMain.TabIndex = 0;
            this.tsMain.Text = "toolStrip1";
            // 
            // AddButton
            // 
            this.AddButton.Image = global::ManageControls.Properties.Resources.Button;
            this.AddButton.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
            this.AddButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.AddButton.Name = "AddButton";
            this.AddButton.Size = new System.Drawing.Size(63, 22);
            this.AddButton.Text = "Button";
            this.AddButton.Click += new System.EventHandler(this.tsButton_Click);
            // 
            // toolStripButton1
            // 
            this.toolStripButton1.Name = "toolStripButton1";
            this.toolStripButton1.Size = new System.Drawing.Size(6, 25);
            // 
            // RemoveButton
            // 
            this.RemoveButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.RemoveButton.Image = global::ManageControls.Properties.Resources.Delete;
            this.RemoveButton.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
            this.RemoveButton.ImageTransparentColor = System.Drawing.Color.White;
            this.RemoveButton.Name = "RemoveButton";
            this.RemoveButton.Size = new System.Drawing.Size(57, 22);
            this.RemoveButton.Text = "Delete";
            this.RemoveButton.Click += new System.EventHandler(this.tsDelete_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // openLabel1
            // 
            this.openLabel1.Name = "openLabel1";
            this.openLabel1.Size = new System.Drawing.Size(36, 22);
            this.openLabel1.Text = "Open";
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // saveLabel2
            // 
            this.saveLabel2.Name = "saveLabel2";
            this.saveLabel2.Size = new System.Drawing.Size(31, 22);
            this.saveLabel2.Text = "Save";
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
            // 
            // frmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(740, 449);
            this.Controls.Add(this.tsMain);
            this.Name = "frmMain";
            this.Text = "Manage Controls - Sample";
            this.tsMain.ResumeLayout(false);
            this.tsMain.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ToolStrip tsMain;
        private System.Windows.Forms.ToolStripButton AddButton;
        private System.Windows.Forms.ToolStripButton RemoveButton;
        private System.Windows.Forms.ToolStripSeparator toolStripButton1;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
        private System.Windows.Forms.ToolStripLabel openLabel1;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
        private System.Windows.Forms.ToolStripLabel saveLabel2;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
    }
} 

I have this code that allow the user to create a button array, any were in a from.
Here and here are two samples of what I am doing (on Create an account now? Click NO and then download the zip file).

The problem or questions is as follow:

I need to save what ever the user makes: for example if he added 5 buttons and saved it, next time when he opens his saved file he will see the 5 button in the same place he save them.
This will allow the user to send the file to another person with the same program and the other person take a look at the created file.


Because of the misunderstanding (comments below) I will post some code with is the second link above. BUT the problem is that I cant find any code to do what I am asking. guys i do not have any idea on how to even start, I mean I know how to save a image from a picture box or some text but the button array is another thing.

Here is the code Simple example code or you can download the second file posted by me and compile it.

ButtomArray.cs

using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ManageControls
{
    public delegate void SendSelectedButton(object sender);
    public class ButtonArray : Hashtable
    {
        private readonly Form HostForm;
        public event SendSelectedButton SelectedButton;
        Point buttonLocation;
        int cntButton = 0;

        public ButtonArray(Form host)
        {
            HostForm = host;
        }

        public void AddButton(int left, int top)
        {
           Button btnElement = new Button();

           btnElement.Top = top;
           btnElement.Left = left;
           btnElement.Tag = cntButton;
           btnElement.Cursor = System.Windows.Forms.Cursors.Default;
           btnElement.FlatStyle = System.Windows.Forms.FlatStyle.System;
           btnElement.Text = "Button " + cntButton.ToString();
           btnElement.Click += new EventHandler(btnElement_Click);
           btnElement.MouseDown += new MouseEventHandler(btnElement_MouseDown);
           btnElement.MouseMove += new MouseEventHandler(btnElement_MouseMove);
           this.Add(cntButton.ToString(), btnElement);
           HostForm.Controls.Add(btnElement);
           btnElement.BringToFront();
           cntButton++;
        }

        public void RemoveButton(string btnIndex)
        {
            if (this.Count > 0)
            {
                HostForm.Controls.Remove((Button)this[btnIndex]);
                this.Remove(btnIndex);
            }
        }

        private void btnElement_Click(Object sender, System.EventArgs e)
        {
            if(null != SelectedButton)
                SelectedButton(sender);
        }

       private void btnElement_MouseDown(object sender, MouseEventArgs e)
       {
           buttonLocation = e.Location;
       }

       private void btnElement_MouseMove(object sender, MouseEventArgs e)
       {
           if (e.Button == MouseButtons.Left)
          {
              ((Button)sender).Left += e.X - buttonLocation.X;
               ((Button)sender).Top += e.Y - buttonLocation.Y;
          }
       }

    }
}

frmMain.cs

using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ManageControls
{
    public partial class frmMain : Form
    {
        ButtonArray buttonArray;
        bool isClicked = false;
        Button btnSelected = new Button();


        public frmMain()
        {
            InitializeComponent();
            buttonArray = new ButtonArray(this);

            this.MouseDown += new  MouseEventHandler(frmMain_MouseDown);
            buttonArray.SelectedButton += new
                SendSelectedButton(buttonArray_SelectedButton);
        }

        private void buttonArray_SelectedButton(object sender)
        {
            btnSelected = sender as Button;

        }

        private void frmMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (isClicked)
            {
                buttonArray.AddButton(e.X, e.Y);
                this.Cursor = Cursors.Default;
                isClicked = false;
            }
        }

        private void tsButton_Click(object sender, EventArgs e)
        {
            isClicked = true;
            this.Cursor = Cursors.Cross;
        }

        private void tsDelete_Click(object sender, EventArgs e)
        {
            buttonArray.RemoveButton(btnSelected.Tag.ToString());
        }


    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ManageControls
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain());
        }
    }
}

and the frmMain.Designer.cs

namespace ManageControls
{
    partial class frmMain
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tsMain = new System.Windows.Forms.ToolStrip();
            this.AddButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton1 = new System.Windows.Forms.ToolStripSeparator();
            this.RemoveButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.openLabel1 = new System.Windows.Forms.ToolStripLabel();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.saveLabel2 = new System.Windows.Forms.ToolStripLabel();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.tsMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // tsMain
            // 
            this.tsMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.AddButton,
            this.toolStripButton1,
            this.RemoveButton,
            this.toolStripSeparator1,
            this.openLabel1,
            this.toolStripSeparator2,
            this.saveLabel2,
            this.toolStripSeparator3});
            this.tsMain.Location = new System.Drawing.Point(0, 0);
            this.tsMain.Name = "tsMain";
            this.tsMain.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            this.tsMain.Size = new System.Drawing.Size(740, 25);
            this.tsMain.TabIndex = 0;
            this.tsMain.Text = "toolStrip1";
            // 
            // AddButton
            // 
            this.AddButton.Image = global::ManageControls.Properties.Resources.Button;
            this.AddButton.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
            this.AddButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.AddButton.Name = "AddButton";
            this.AddButton.Size = new System.Drawing.Size(63, 22);
            this.AddButton.Text = "Button";
            this.AddButton.Click += new System.EventHandler(this.tsButton_Click);
            // 
            // toolStripButton1
            // 
            this.toolStripButton1.Name = "toolStripButton1";
            this.toolStripButton1.Size = new System.Drawing.Size(6, 25);
            // 
            // RemoveButton
            // 
            this.RemoveButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.RemoveButton.Image = global::ManageControls.Properties.Resources.Delete;
            this.RemoveButton.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
            this.RemoveButton.ImageTransparentColor = System.Drawing.Color.White;
            this.RemoveButton.Name = "RemoveButton";
            this.RemoveButton.Size = new System.Drawing.Size(57, 22);
            this.RemoveButton.Text = "Delete";
            this.RemoveButton.Click += new System.EventHandler(this.tsDelete_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // openLabel1
            // 
            this.openLabel1.Name = "openLabel1";
            this.openLabel1.Size = new System.Drawing.Size(36, 22);
            this.openLabel1.Text = "Open";
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // saveLabel2
            // 
            this.saveLabel2.Name = "saveLabel2";
            this.saveLabel2.Size = new System.Drawing.Size(31, 22);
            this.saveLabel2.Text = "Save";
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
            // 
            // frmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(740, 449);
            this.Controls.Add(this.tsMain);
            this.Name = "frmMain";
            this.Text = "Manage Controls - Sample";
            this.tsMain.ResumeLayout(false);
            this.tsMain.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ToolStrip tsMain;
        private System.Windows.Forms.ToolStripButton AddButton;
        private System.Windows.Forms.ToolStripButton RemoveButton;
        private System.Windows.Forms.ToolStripSeparator toolStripButton1;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
        private System.Windows.Forms.ToolStripLabel openLabel1;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
        private System.Windows.Forms.ToolStripLabel saveLabel2;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2024-10-04 20:34:07

您没有提供太多信息,我不会下载您的代码。但是您可以创建一个类来表示按钮可以保存的属性(例如位置/大小/文本)并序列化此类的集合。 Google .NET 序列化并且有数百个关于该主题的链接。您可以序列化数组并轻松地从文件中反序列化它,以动态取回所有按钮,然后循环遍历反序列化的集合并将它们添加回您的表单。

You don't give much information and i'm not going to download your code. But you can create a class which represents the properties that a button can hold such as position / size / text and serialise a collection of this class. Google .NET serialisation and there are hundreds of links on the topic. You can serialise an array and deserialise it easily from a file to dynamically get back all the buttons, then loop through the deserialised collection and add them back to your form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文