开发者

listView.Items.Contains with DateTime/String?

开发者 https://www.devze.com 2022-12-13 14:36 出处:网络
How do I check if a listView contains a DateTime or String? I\'ve tried if (!listView1.Items.Contains(SetAlarm.Value)开发者_高级运维)

How do I check if a listView contains a DateTime or String?

I've tried

if (!listView1.Items.Contains(SetAlarm.Value)开发者_高级运维)

But it cannot convert System.DateTime to System.Windows.Forms.ListViewItem.


listView1.Items.Contains(new ListItem(DateTime.Now)) will not work. Contains only works if we are passing an Item actually in ListView.Items collections. I think you will have to use a for loop :(

foreach (ListViewItem s in listView1.Items)
{
    if (s.Text != SetAlarm.Value.ToString()) continue;
    MsgBox.Show("Found: " + SetAlarm.Value.ToString());
    return;
}


    bool isMatch = false;

    int matchIndex = -1;

    DateTime resultDate = = new DateTime();

    foreach(ListViewItem lvItem in listView1.Items)
    {
        if(DateTime.TryParse(lvItem.Text, out resultDate ))
        {
           isMatch = true;
           matchIndex = lvItem.Index;
           break;
        }
    }

    if(isMatch) Console.WriteLine("match at index : " + matchIndex.ToString() + " = " + resultDate.ToString());
0

精彩评论

暂无评论...
验证码 换一张
取 消