c#如何在Windows窗体应用程序的运行时保存创建控件?

时间:2018-01-25 11:52:06

标签: c#

用户单击save_button后如何保存位置和属性。然后UI显示,以便UI每次在应用程序启动时显示。 我的代码创建按钮并移动...

private Point firstpoint = new Point();

private void btn_submit_Click(object sender, EventArgs e)
{
    string bttext = txt_tbname.Text;
    int w, h;
    int cw, ch;
    if (txt_tbname.Text == "" || txt_width.Text == "" || txt_height.Text == "")
    {
        MessageBox.Show("Please insert Name, Width and Height before submit", "Informations", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        List<Button> button = new List<Button>();
        Button newbtn = new Button();
        w = Convert.ToInt32(txt_width.Text);
        h = Convert.ToInt32(txt_height.Text); 
        cw = w * 80;
        ch = h * 80;
        newbtn.Width = cw;
        newbtn.Height = ch;
        button.Add(newbtn);
        this.Controls.Add(newbtn);
        newbtn.Text = "" + bttext;
        newbtn.BackColor = Color.MediumPurple;
        newbtn.ForeColor = Color.White;

        newbtn.MouseDown += (ss, ee) =>
          {
              if (ee.Button == System.Windows.Forms.MouseButtons.Left)
              {
                  firstpoint = Control.MousePosition;
              }
          };
        newbtn.MouseMove += (ss, ee) =>
        {
            if (ee.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Point temp = Control.MousePosition;
                Point res = new Point(firstpoint.X - temp.X, firstpoint.Y - temp.Y);
                newbtn.Location = new Point(newbtn.Location.X - res.X, newbtn.Location.Y - res.Y);
                firstpoint = temp;
            }
        };
        cleartxt();
    }
}

0 个答案:

没有答案