代码之家  ›  专栏  ›  技术社区  ›  user8174757

在c中,从列表中添加和删除项目失败#

c#
  •  0
  • user8174757  · 技术社区  · 7 年前

    我有几个动态文本框,每当文本框失去焦点时,我想将其文本值添加到列表中,并在清除文本框时删除

    <TextBox LostFocus="comment_box_LostFocus" Name="comment_box" Tag="{x:Bind id}" Width="300"/>
    

    List<CommentModel> commentlist = new List<CommentModel>();
    
    private void comment_box_LostFocus(object sender, RoutedEventArgs e)
    {
        var comenttext = ((TextBox)sender).Text; //this is okay
        var commentid = ((TextBox)sender).Tag; //this is okay
    
        if (String.IsNullOrEmpty(comenttext)) //incase user clears the textbox
        {
            for(var i=0; i< commentlist.Count; i++)
            {
                if(commentlist[i].id == (int)commentid)
                {
                    var itemToRemove = commentlist.Single(r => r.id == (int)commentid);
                    commentlist.Remove(itemToRemove);
                }
            }
        }
        else //add new or update incase its a comment change
        {  
            for (var i = 0; i < commentlist.Count; i++)
            {
                if ((commentlist[i].id) == ((int)commentid))
                {
                    commentlist[i].comment = comenttext;
                }
                else
                {
                     commentlist.Add(new CommentModel((int)commentid, (string)comenttext));
                }
            }
            }
    
                if (commentlist.Count == 0)
                {
                    Debug.WriteLine("Comment list is empty adding initial comment");
                    if (!String.IsNullOrEmpty(comenttext))
                    {
                        //automatically add new
                        commentlist.Add(new CommentModel((int)commentid, (string)comenttext));
                    }
                }
        }
    }
    

    类CommentModel是

    class CommentModel
    {
        public int id;
        public string comment;
    
        public CommentModel(int idval, string commentval)
        {
            this.id = idval;
            this.comment = commentval;
        }
    }
    

    for(var i=0; i< commentlist.Count; i++)
    {
        Debug.WriteLine(commentlist[i].comment);
    }
    

    这是评论的重复

    first entry: "test one", debug output "test one", 
    second entry :"no2", debug output :"test one", "no2"....
    

    我哪里会出错?

    1 回复  |  直到 7 年前
        1
  •  0
  •   PaulF    7 年前

    您需要将代码更改为以下内容:

    if (String.IsNullOrEmpty(comenttext)) //incase user clears the textbox
    {
        for (var i = 0; i < commentlist.Count; i++)
        {
            if (commentlist[i].id == (int)commentid)
            {
                var itemToRemove = commentlist.Single(r => r.id == (int)commentid);
                commentlist.Remove(itemToRemove);
                break;
            }
        }
    }
    else
    {
        bool found = false;
        for (var i = 0; i < commentlist.Count; i++)
        {
            if ((commentlist[i].id) == ((int)commentid))
            {
                commentlist[i].comment = comenttext;
                found = true;
                break;
            }
        }
        if (! found)
            commentlist.Add(new CommentModel((int)commentid, (string)comenttext));
    }
    

    类似地,您可以在if子句中找到条目后退出循环,但在这种情况下,您只会浪费时间,因为您已经删除了id的条目

    没有for循环的较短LINQ解决方案可以是:

    var itemInList = commentlist.Where(li => li.id == (int)commentid).FirstOrDefault();
    if (String.IsNullOrEmpty(comenttext)) //incase user clears the textbox
    {
      if (itemInList != null)
        commentlist.Remove(itemInList);
    }
    else //add new or update incase its a comment change
    {
      if (itemInList != null)
        itemInList.comment = comenttext;
      else
        commentlist.Add(new CommentModel((int)commentid, comenttext));
    }