我不知道“static List<user> AllUserList = new List<user>();      ”添进数据 然后怎么把这个数据给移除了。请大家帮忙。详细代码如下
user.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace WF_Server
{
    public class user
    {
        public string userName { get; set; }
        public string _ipport { get; set; }
        public user()
        {
            this.userName = "";
            this._ipport = "";
        }
    }
}
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 WF_Server
{
   
    public partial class Form_Server : Form
    {
        static List<user> AllUserList = new List<user>();      
        public Form_Server()
        {
            InitializeComponent();
        }        private void panel1_Paint(object sender, PaintEventArgs e)
        {        }        private void button1_Click(object sender, EventArgs e)  //增加        {
            string stra = textBox1.Text;
            user temp = new user();
            temp.userName = stra;            temp._ipport = stra;            AllUserList.Add(temp);
        }        private void button3_Click(object sender, EventArgs e) //显示
        {
            listBox1.Items.Clear();
            for (int i = 0; i < AllUserList.Count; i++)
            {                
                listBox1.Items.Add(AllUserList[i].userName.ToString());              
            }
        }        private void button2_Click(object sender, EventArgs e)  //移除
        {
            string stra = textBox1.Text;
            user temp = new user();
            temp.userName = stra;            temp._ipport = stra;  我这里这么写始终不能移除当时添入的数据,该怎么更改才可以,希望详细指点。谢谢了          
  AllUserList.Remove(temp);
        }    }
     
}
 

解决方案 »

  1.   

    你需要先从list中找到需要删除的那项,然后再remove那个对象才能删除
      

  2.   

    private bool issame(user u)
            {
                return (u.userName == textBox1.Text && u._ipport == textBox1.Text);
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                int index = AllUserList.FindIndex(issame);
                if (index >= 0)
                {
                    AllUserList.RemoveAt(index);
                }
            }
      

  3.   

    谢谢你,我的先找他啊,这样写更本就不对啊
    for (int i = 0; i < AllUserList.Count; i++)
      {   
      listBox1.Items.Add(AllUserList[i].userName.ToString());    找到在移除啊
      }
      

  4.   

    你的两个User值一样,但是不是一个对象如果只判断值一样的User的话,就一句:AllUserList.RemoveAll(user => user.userName == stra && user.ipport == stra);