C#中窗体间参数传递实现增删改的例子

此例子中传递的变量有string type,string text,储存在结构数组中;static int i储存在Sta类中(如果在外面声明,调用它老出错),里面有geti和seti函数来操纵它。

各个窗体实现功能及截图:

C#中窗体间参数传递实现增删改的例子C#中窗体间参数传递实现增删改的例子C#中窗体间参数传递实现增删改的例子

各窗体代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.OleDb;

using System.IO;



using System.Data.SqlClient;



namespace 电子便签

{

    public partial class menu : Form

    {

       public menu()

        {

            InitializeComponent();

        }

        public class Sta                     //建立储存静态变量的类

        {

            public static int i = 0;

            public int geti()

            {

                return i;

            }

            public void seti(int a)

            {

                i = a;

            }

        }

        public Sta sta = new Sta();             //将此类实例化

        public struct chucun                    

        {

            public string type;

            public string text;

        }

        public chucun[] cc = new chucun[10];              //结构数组用来储存内容,最大储存单位为10个

        

        private void button1_Click(object sender, EventArgs e)           //打开增加信息add窗体

        {

            add form2 = new add(this);

            form2.Show();

        }



        private void button2_Click(object sender, EventArgs e)     //打开删除信息delete窗体

        {

            delete form4 = new delete(this);

            form4.Show();

        }



        private void button3_Click(object sender, EventArgs e)        //打开修改信息revise窗体

        {

            revise form5 = new revise(this);

            form5.Show();

        }



        private void button4_Click(object sender, EventArgs e)    //设置字体样式

        {

            FontDialog f = new FontDialog();

            if (f.ShowDialog() == DialogResult.OK)

            {

                textBox1.Font = f.Font;

            }

        }



        private void button5_Click(object sender, EventArgs e)            //设置字体颜色

        {

            ColorDialog cd = new ColorDialog();

            if (cd.ShowDialog() == DialogResult.OK)

            {

                textBox1.ForeColor = cd.Color;

            }

        }



        private void button6_Click(object sender, EventArgs e)               //设置背景

        {

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "图片文件(*.jpg)|*.jpg|All files(*.*)|*.*";

            if (ofd.ShowDialog() == DialogResult.OK)

            {



                //path = ofd.FileName;

                this.BackgroundImage = Image.FromFile(ofd.FileName);

            }

            //this.BackgroundImage = Image.FromFile(path);

        }



        private void Form1_Load(object sender, EventArgs e)       //wait窗体为打开前奏

        {

            Waiting wait = new Waiting();

            wait.Show();

            wait.Activate();

            Application.DoEvents();

            System.Threading.Thread.Sleep(1000);

            wait.Close();

            wait.Dispose();

            //conn.ConnectionString = CString;

            

           

            

        }



        private void button7_Click(object sender, EventArgs e)   //将textBox内容存储到text文件中

        {

            FileStream fs = new FileStream("abc.text", FileMode.Create, FileAccess.Write);

            StreamWriter sw = new StreamWriter(fs, Encoding.Default);

            sw.WriteLine(textBox1.Text);

            sw.Close();

        }

    }

}
 1 using System;

 2 using System.Collections.Generic;

 3 using System.ComponentModel;

 4 using System.Data;

 5 using System.Drawing;

 6 using System.Linq;

 7 using System.Text;

 8 using System.Windows.Forms;

 9 using System.Data.OleDb;

10 namespace 电子便签

11 {

12      

13     public partial class add : Form

14     {

15         private menu mF_Form;

16         public add(menu myForm)                       

17         {

18             InitializeComponent();

19             this.mF_Form = myForm;

20         }                                      //将窗体一对象作为窗体二属性

21         

22         private void button1_Click(object sender, EventArgs e)

23         {

24             if (string.IsNullOrEmpty(textBox1.Text) | string.IsNullOrEmpty(textBox2.Text))   //判断textBox内容是否为空

25             {

26                 string s = "输入不能为空";

27                 MessageBox.Show(s);

28             }

29             else 

30             {

31                 string s1 = textBox1.Text;

32                 string s2 = textBox2.Text;

33                 mF_Form.cc[mF_Form.sta.geti()].type = s1;

34                 mF_Form.cc[mF_Form.sta.geti()].text = s2;                //从文本框中读取相关内容

35                 

36                 string sum = "";

37                 for (int i2 = 0; i2 <= mF_Form.sta.geti(); i2++)               //将目前所有类型和内容输出在meau窗体的textBox中

38                 {

39                     sum = sum + mF_Form.cc[i2].type + ":\r\n     " + mF_Form.cc[i2].text + "\r\n";

40                 }

41                 mF_Form.textBox1.Text = sum;

42                 int ii = mF_Form.sta.geti() + 1;                     //由于添加了一组信息,静态变量加1

43                 mF_Form.sta.seti(ii);

44                 this.Close();

45             }

46         }

47 

48         private void button2_Click(object sender, EventArgs e)

49         {

50             this.Close();

51         }

52 

53         

54         

55         

56    }

57 }
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace 电子便签

{

    public partial class delete : Form

    {

        

        private menu mF_Form;               

        CheckBox[] checkBox = new CheckBox[5];            //声明并实例化复选框

        public delete(menu myForm)

        {

            InitializeComponent();

            mF_Form = myForm;

            if (mF_Form.sta.geti() == 0)

            {

                Label label = new Label();

                label.Location = new System.Drawing.Point(35, 49);  //label控件显示位置

                label.Text = "无可删除类型";

                this.Controls.Add(label);

            }

            else

            {

                for (int i4 = 0; i4 < mF_Form.sta.geti(); i4++)               //动态添加复选框

                {

                    //textBox1.Text = "sfsdfsdfs";

                    checkBox[i4] = new CheckBox();

                    checkBox[i4].Location = new System.Drawing.Point(35, 49+20*i4);   //控件显示在同一列中

                    checkBox[i4].Size = new System.Drawing.Size(this.Width, 16);         //同一宽度

                    checkBox[i4].Text = myForm.cc[i4].type;

                    this.Controls.Add(checkBox[i4]); 

                }

            }

        }

        private void button1_Click(object sender, EventArgs e)

        {



            for (int i = 0; i <= mF_Form.sta.geti(); i++)     //遍历所有

            {

                if (checkBox[i].Checked)                   //发现复选框被选中

                {

                    for (int ii = i; ii < mF_Form.sta.geti(); ii++)                 //将后面的前移,同时静态变量-1

                    {

                        mF_Form.cc[ii].type = mF_Form.cc[ii + 1].type;

                        mF_Form.cc[ii].text = mF_Form.cc[ii + 1].text;

                    }

                    mF_Form.sta.seti(mF_Form.sta.geti() -1);

                }

            }

            

            //将删除后的显示在menu窗体的textBox中

            string sum ="";            

            for (int i2 = 0; i2 <mF_Form.sta.geti(); i2++)

            {

                

                 sum = sum + mF_Form.cc[i2].type +  ":\r\n     " + mF_Form.cc[i2].text + "\r\n"; 

            }

            mF_Form.textBox1.Text = sum;

            

            

            this.Close();

        }



        private void button2_Click(object sender, EventArgs e)

        {

            this.Close();

        }



        private void delete_Load(object sender, EventArgs e)

        {



        }



        

        



        

    }

}
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace 电子便签

{

    public partial class revise : Form

    {

        private menu mF_Form;

        CheckBox[] checkBox = new CheckBox[10];    //声明和实例化复选框空间

        

        public revise(menu myForm)

        {

            InitializeComponent();

            mF_Form = myForm;

            if (mF_Form.sta.geti() == 0)                       //没有内容

            {

                Label label = new Label();

                label.Location = new System.Drawing.Point(35, 49); 

                label.Text = "无可删除类型";

                this.Controls.Add(label);

            }

            else

            {

                for (int i4 = 0; i4 < mF_Form.sta.geti(); i4++)               //动态添加复选框

                {

                    //textBox1.Text = "sfsdfsdfs";

                    checkBox[i4] = new CheckBox();

                    checkBox[i4].Location = new System.Drawing.Point(35, 49 + 20 * i4);

                    checkBox[i4].Size = new System.Drawing.Size(this.Width, 16);

                    checkBox[i4].Text = myForm.cc[i4].type;

                    this.Controls.Add(checkBox[i4]);

                }

            }

        }



        private void button2_Click(object sender, EventArgs e)        //窗体关闭

        {

            this.Close();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            

            for (int i5 = 0; i5 < mF_Form.sta.geti(); i5++)

            {

                

                if (this.checkBox[i5].Checked)

                {

                    

                    mF_Form.cc[i5].text = textBox1.Text;       //修改选择项的内容

                }

           }

            //将修改后的显示在menu窗体的textBox中

            string sum = "";

            for (int i2 = 0; i2 < mF_Form.sta.geti(); i2++)

            {

                sum = sum + mF_Form.cc[i2].type + ":\r\n     " + mF_Form.cc[i2].text + "\r\n";

            }

            mF_Form.textBox1.Text = sum;



            this.Close();

        }



        

    }

}

其实使用数据库可以更方便的实现,而且数据库也可以体现出技术。这是在还没学会数据库做的,现在快交作业了,必须用数据库了,整个代码必须更新了。把这些代码提出来和大家共享一下。

 

 

你可能感兴趣的:(参数传递)