当前位置: 代码迷 >> C# >> 还是COMBOBOX的有关问题 求教了
  详细解决方案

还是COMBOBOX的有关问题 求教了

热度:88   发布时间:2016-05-05 03:21:25.0
还是COMBOBOX的问题 求教了
本帖最后由 qq4783856 于 2015-06-21 16:35:25 编辑
现有LIST.TXT
内容为:
 张三,178,80,0,true
李四,168,74,0,false
王五,155,40,1,false
写一个软件 读取此列表 COMEBOX内赋值 姓名 选择姓名后,点击写入按钮
 将此行内容写入本地ini文件内 
最终ini效果
 姓名 = 张三
 身高 = 178
体重 = 80 
性别 = 男
 是否正式员工 = 是


            while (sr.EndOfStream == false)
            {

                string[] Line = sr.ReadLine().Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);


                for (int i = 0; i < Line.Length; i++)
                {
 
                    string[] s = Line[i].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    
            NameBox.Items.Add(s[0]);
          //  NameBox.SelectedIndex = Line.Length - 1;
           
                }
                int q = NameBox.SelectedIndex;
                string[] rs = Line[q].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                Settings.shengao = rs[1];
                Settings.nianling= rs[2];
            

            }
            Settings.Save();

目前COMBOBOX已可以显示姓名 但问题是 后续如何写入本地ini  现在这个写法每次都是固定为王五的身高和年龄。
主要是选中项索引的 问题  鼠标选中后的索引不知道该怎么找
------解决思路----------------------
        private List<string[]> dict;
        public Form1()
        {
            InitializeComponent();

            dict = new List<string[]>();
            foreach (var s in File.ReadAllLines("list.txt", Encoding.UTF8))
            {
                dict.Add(s.Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries));
                comboBox1.Items.Add(dict[dict.Count-1][0]);
            }
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var n = comboBox1.SelectedIndex;
            MessageBox.Show(string.Format("姓名 = {0}\n身高 = {1}\n体重 = {2}\n性别 = {3}\n是否正式员工 {4}",
                dict[n][0], dict[n][1], dict[n][2], dict[n][3] == "0" ? "男" : "女", dict[n][4] == "true" ? "是" : "否"));
        }

为显示方便,略去了文件保存代码,代之以 MessageBox
  相关解决方案