-1

我正在读取串行端口以读取 gsm 调制解调器中存在的消息,然后在列表视图中显示它们。但我最终得到了一个包含冗余项目的列表视图。我无法理解为什么会这样..

{
        port.DiscardOutBuffer();
        port.DiscardInBuffer();
        string res;
        Thread.Sleep(5000);
        res = port.ReadExisting();
        ShortMessageCollection messages = new ShortMessageCollection();
        Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");
        Match m = r.Match(res);
        while (m.Success)
        {
            ShortMessage msg = new ShortMessage();

            msg.Index = m.Groups[1].Value;
            msg.Status = m.Groups[2].Value;
            msg.Sender = m.Groups[3].Value;
            msg.Alphabet = m.Groups[4].Value;
            msg.Sent = m.Groups[5].Value;
            msg.Message = m.Groups[6].Value;
            messages.Add(msg);
            m = m.NextMatch();
            objShortMessageCollection = (ShortMessageCollection)messages;
            foreach (ShortMessage mesg in objShortMessageCollection)
            {
                ListViewItem item = new ListViewItem(new string[] {mesg.Index, mesg.Sender, mesg.Message, mesg.Sent});
                item.Tag = mesg;
                lvwMessages.Items.Insert(0, item);
            }
         }
4

1 回答 1

1

使用以下代码删除冗余:

            lvwMessages.Sorting = SortOrder.Descending;
            int i = 0;
            while (i < lvwMessages.Items.Count - 1)
            {
                if (lvwMessages.Items[i].Tag == lvwMessages.Items[i + 1].Tag)
                    lvwMessages.Items[i + 1].Remove();
                else
                    i++;
            }
于 2012-07-20T05:46:18.867 回答