c#版gshop

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;

namespace StructTest
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 结构体
        /// </summary>
        [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
        struct ObjItem
        {
            public int a1;  //4 byte
            public int a2;  //4 byte
            public int a3;  //4 byte
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
            public byte[] a4; //Name 占128字节
            public int a5;  //4 byte
            public int a6;  //4 byte
            public int a7;  //4 byte
            public int a8;  //4 byte
            public int a9;  //4 byte
            public int a10;  //4 byte
            public int a11;  //4 byte
            public int a12;  //4 byte
            public int a13;  //4 byte
            public int a14;  //4 byte
            public int a15;  //4 byte
            public int a16;  //4 byte
            public int a17;  //4 byte
            public int a18;  //4 byte
            public int a19;  //4 byte
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
            public char[] a20; //512
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
            public char[] a21; //32
            /// <summary>
            /// 初始化结构体
            /// </summary>
            /// <param name="a4is128">a4的长度</param>
            /// <param name="a20is512">a20的长度</param>
            /// <param name="a21is32">a21的长度</param>
            public ObjItem(int a4is128, int a20is512, int a21is32)
            {
                a1 = 0;
                a2 = 0;
                a3 = 0;
                a4 = new byte[128];
                a5 = 0;
                a6 = 0;
                a7 = 0;
                a8 = 0;
                a9 = 0;
                a10 = 0;
                a11 = 0;
                a12 = 0;
                a13 = 0;
                a14 = 0;
                a15 = 0;
                a16 = 0;
                a17 = 0;
                a18 = 0;
                a19 = 0;
                a20 = new char[512];
                a21 = new char[32];
            }

        }

        private static ArrayList AL = new ArrayList();  //数据集
        private static string strFile = "gshop.data";

        /// <summary>
        /// 判断指定的ListView是否有选择项目
        /// </summary>
        /// <param name="lv">传进来一个ListView</param>
        /// <returns>有选项则返回True,否则返回false</returns>
        private static Boolean lvSelected(ListView lv)
        {
            if (lv.SelectedItems.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 写结构体
        /// </summary>
        void WriteStruct()
        {
            int ALCount = AL.Count;
            FileStream fs = new FileStream(strFile, FileMode.Create); 
            ObjItem ts = new ObjItem(128,512,32);
            byte[] count = BitConverter.GetBytes(ALCount);
            fs.Write(count, 0, count.Length);
            for (int i = 0; i <ALCount ; i++)
            {
                byte[] bytData = BytesStruct.StructToBytes((ObjItem)AL[i]);
                fs.Write(bytData, 0, bytData.Length);
            }
            fs.Close();
        }

        /// <summary>
        /// 读结构体
        /// </summary>
        void ReadStruct()
        {
            FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read);
            BinaryReader r = new BinaryReader(fs);
            int count = r.ReadInt32();
            ObjItem ts = new ObjItem(128, 512, 32);
            byte[] bytData = new byte[Marshal.SizeOf(ts)];
            listView1.Items.Clear();
            for (int i = 0; i < count; i++)
            {
                fs.Read(bytData, 0, bytData.Length);
                ts = (ObjItem)BytesStruct.BytesToStuct(bytData, ts.GetType());
                AL.Add(ts);
                ListViewItem Item = new ListViewItem(((ObjItem)AL[i]).a1.ToString());
                Item.SubItems.Add(Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(((ObjItem)AL[i]).a21)));//char[]
                listView1.Items.Add(Item);
            }
            fs.Close(); 
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            #region 程序启动时做的事
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.HideSelection = false;
            listView1.MultiSelect = false;
            listView1.Columns.Add("编号", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称",120,HorizontalAlignment.Left); 
        
            //控件Text设置
            label1.Text = "ID";
            label2.Text = "大分类";
            label3.Text = "小分类";
            label6.Text = "数量";
            label7.Text = "价格";
            label9.Text = "New";
            label20.Text = "名称";
            label21.Text = "说明";
            //读数据
            ReadStruct();

            btnEdit.Enabled = false;
            btnDel.Enabled = false;
            #endregion
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            #region ListView改变
            if (lvSelected(listView1))
            {
                int NowIndex = listView1.SelectedItems[0].Index;
                textBox1.Text = ((ObjItem)AL[NowIndex]).a1.ToString();
                textBox2.Text = ((ObjItem)AL[NowIndex]).a2.ToString();
                textBox3.Text = ((ObjItem)AL[NowIndex]).a3.ToString();
                textBox4.Text = Encoding.Default.GetString(((ObjItem)AL[NowIndex]).a4);
                textBox5.Text = ((ObjItem)AL[NowIndex]).a5.ToString();
                textBox6.Text = ((ObjItem)AL[NowIndex]).a6.ToString();
                textBox7.Text = ((ObjItem)AL[NowIndex]).a7.ToString();
                textBox8.Text = ((ObjItem)AL[NowIndex]).a8.ToString();
                textBox9.Text = ((ObjItem)AL[NowIndex]).a9.ToString();
                textBox10.Text = ((ObjItem)AL[NowIndex]).a10.ToString();
                textBox11.Text = ((ObjItem)AL[NowIndex]).a11.ToString();
                textBox12.Text = ((ObjItem)AL[NowIndex]).a12.ToString();
                textBox13.Text = ((ObjItem)AL[NowIndex]).a13.ToString();
                textBox14.Text = ((ObjItem)AL[NowIndex]).a14.ToString();
                textBox15.Text = ((ObjItem)AL[NowIndex]).a15.ToString();
                textBox16.Text = ((ObjItem)AL[NowIndex]).a16.ToString();
                textBox17.Text = ((ObjItem)AL[NowIndex]).a17.ToString();
                textBox18.Text = ((ObjItem)AL[NowIndex]).a18.ToString();
                textBox19.Text = ((ObjItem)AL[NowIndex]).a19.ToString();
                textBox20.Text = Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(((ObjItem)AL[NowIndex]).a21));
                textBox21.Text = Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(((ObjItem)AL[NowIndex]).a20));
                btnEdit.Enabled = true;
                btnDel.Enabled = true;
            }
            #endregion
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            #region 删除记录
            if (lvSelected(listView1))
            {
                string str = "确定删除 " + listView1.SelectedItems[0].SubItems[1].Text + "?";
                DialogResult Result = MessageBox.Show(str, "提示", MessageBoxButtons.OKCancel);
                if (Result == System.Windows.Forms.DialogResult.OK)
                {

                    int NowIndex = listView1.SelectedItems[0].Index;
                    AL.RemoveAt(NowIndex);
                    listView1.Items.RemoveAt(NowIndex);
                    if (listView1.Items.Count > 0)
                    {
                        if (NowIndex - 1 > 0)
                        {
                            listView1.Items[NowIndex - 1].Selected = true;
                        }
                        else
                        {
                            listView1.Items[0].Selected = true;
                        }
                    }
                    WriteStruct();
                }
            }
            #endregion
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            #region 修改记录
            if (lvSelected(listView1))
            {
                int NowIndex = listView1.SelectedItems[0].Index;
                ObjItem ts = new ObjItem(128, 512, 32);
                ts.a1 = Convert.ToInt32(textBox1.Text.Trim());
                ts.a2 = Convert.ToInt32(textBox2.Text.Trim());
                ts.a3 = Convert.ToInt32(textBox3.Text.Trim());
                byte[] a4 = Encoding.Default.GetBytes(textBox4.Text.Trim());
                Array.Copy(a4, ts.a4, a4.Length);
                ts.a5 = Convert.ToInt32(textBox5.Text.Trim());
                ts.a6 = Convert.ToInt32(textBox6.Text.Trim());
                ts.a7 = Convert.ToInt32(textBox7.Text.Trim());
                ts.a8 = Convert.ToInt32(textBox8.Text.Trim());
                ts.a9 = Convert.ToInt32(textBox9.Text.Trim());
                ts.a10 = Convert.ToInt32(textBox10.Text.Trim());
                ts.a11 = Convert.ToInt32(textBox11.Text.Trim());
                ts.a12 = Convert.ToInt32(textBox12.Text.Trim());
                ts.a13 = Convert.ToInt32(textBox13.Text.Trim());
                ts.a14 = Convert.ToInt32(textBox14.Text.Trim());
                ts.a15 = Convert.ToInt32(textBox15.Text.Trim());
                ts.a16 = Convert.ToInt32(textBox16.Text.Trim());
                ts.a17 = Convert.ToInt32(textBox17.Text.Trim());
                ts.a18 = Convert.ToInt32(textBox18.Text.Trim());
                ts.a19 = Convert.ToInt32(textBox19.Text.Trim());
                char[] a20 = Encoding.Unicode.GetChars(Encoding.Unicode.GetBytes(textBox21.Text.Trim()));
                Array.Copy(a20, ts.a20, a20.Length);
                char[] a21 = Encoding.Unicode.GetChars(Encoding.Unicode.GetBytes(textBox20.Text.Trim()));
                Array.Copy(a21, ts.a21, a21.Length);
                AL[NowIndex] = ts;
                listView1.SelectedItems[0].Text = ((ObjItem)AL[NowIndex]).a1.ToString();
                listView1.SelectedItems[0].SubItems[1].Text = Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(((ObjItem)AL[NowIndex]).a21));
                WriteStruct();
            }
            #endregion
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            #region 添加记录
            ObjItem ts = new ObjItem(128, 512, 32);
                ts.a1 = Convert.ToInt32(textBox1.Text.Trim());
                ts.a2 = Convert.ToInt32(textBox2.Text.Trim());
                ts.a3 = Convert.ToInt32(textBox3.Text.Trim());
                byte[] a4 = Encoding.Default.GetBytes(textBox4.Text.Trim());
                Array.Copy(a4, ts.a4, a4.Length);
                ts.a5 = Convert.ToInt32(textBox5.Text.Trim());
                ts.a6 = Convert.ToInt32(textBox6.Text.Trim());
                ts.a7 = Convert.ToInt32(textBox7.Text.Trim());
                ts.a8 = Convert.ToInt32(textBox8.Text.Trim());
                ts.a9 = Convert.ToInt32(textBox9.Text.Trim());
                ts.a10 = Convert.ToInt32(textBox10.Text.Trim());
                ts.a11 = Convert.ToInt32(textBox11.Text.Trim());
                ts.a12 = Convert.ToInt32(textBox12.Text.Trim());
                ts.a13 = Convert.ToInt32(textBox13.Text.Trim());
                ts.a14 = Convert.ToInt32(textBox14.Text.Trim());
                ts.a15 = Convert.ToInt32(textBox15.Text.Trim());
                ts.a16 = Convert.ToInt32(textBox16.Text.Trim());
                ts.a17 = Convert.ToInt32(textBox17.Text.Trim());
                ts.a18 = Convert.ToInt32(textBox18.Text.Trim());
                ts.a19 = Convert.ToInt32(textBox19.Text.Trim());
                char[] a20 = Encoding.Unicode.GetChars(Encoding.Unicode.GetBytes(textBox21.Text.Trim()));
                Array.Copy(a20, ts.a20, a20.Length);
                char[] a21 = Encoding.Unicode.GetChars(Encoding.Unicode.GetBytes(textBox20.Text.Trim()));
                Array.Copy(a21, ts.a21, a21.Length);
                AL.Add(ts);

                ListViewItem Item = new ListViewItem(((ObjItem)AL[AL.Count - 1]).a1.ToString());
                Item.SubItems.Add(Encoding.Unicode.GetString(Encoding.Unicode.GetBytes(((ObjItem)AL[AL.Count - 1]).a21)));
                listView1.Items.Add(Item);
                listView1.Items[listView1.Items.Count - 1].EnsureVisible();//滚到动指定行并显示
                listView1.Items[listView1.Items.Count - 1].Selected = true;//选中选指行
                WriteStruct();
            #endregion
        }
           
    }
}

原文地址:https://www.cnblogs.com/jxgxy/p/1824278.html