items = File
.ReadLines(RecentFiles)
.Select(line => new ToolStripMenuItem()
{
Text = line
})
.ToArray();
recentFilesToolStripMenuItem.DropDownItems.AddRange(items);
I want to check if the items exist already in the recentFilesToolStripMenuItem.DropDownItems
If not exist add but if exist don't add.
You basically have two collections: items
& recentFilesToolStripMenuItem.DropDownItems
Using Linq
, you should be able to do an a Except()
Where()
to only add the difference between the two collections.
This is not tested.
recentFilesToolStripMenuItem.DropDownItems.AddRange(items.Except(recentFilesToolStripMenuItem.DropDownItems));
This is tested
recentFilesToolStrip.DropDownItems.AddRange(
items
.Where(i => !recentFilesToolStrip.DropDownItems
.OfType<ToolStripMenuItem>()
.Select(t => t.Text).Contains(i.Text)
).ToArray()
);
SLaks comment refers to doing something like the following:
recentFilesToolStripMenuItems.DropDownItems.Clear();
recentFilesToolStripMenuItems.DropDownItems.AddRange(items);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.