Rank: Newbie
Groups: Registered
Joined: 4/6/2020(UTC) Posts: 4  Location: Kiel
|
Code:private PdfBookmark currBookmark = null;
private void TraverseBookmark(PdfBookmarkCollections bookmarks, int iLevel, string nTitle)
{
foreach (PdfBookmark item in bookmarks)
{
TraverseBookmark(item.Childs, iLevel + 1, nTitle);
if (item.Title.Equals(nTitle)) { currBookmark = item; }
}
}
private void DeleteThisBookmarkToolStripMenuItem_Click(object sender, EventArgs e)
{
currBookmark = null;
TraverseBookmark(pdfViewer.Document.Bookmarks, 0, bookmarksViewer.SelectedNode.Text);
if (currBookmark != null)
{ //MessageBox.Show(currBookmark.Title);
if (pdfViewer.Document.Bookmarks.Remove(currBookmark))
{
bookmarksViewer.RebuildTree();
bookmarksViewer.ExpandAll();
}
else { Console.Beep(); }
}
}
The PdfBookmarkCollections.Remove Method doesn't delete any element which is a child element!?
|
|
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,138
Thanks: 10 times Was thanked: 133 time(s) in 130 post(s)
|
Bookmarks are grouped into a tree. And you are trying to remove a bookmark from the flat list that is the root of this tree. Instead, you must remove the bookmark from the flat list of children of the parent of the bookmark. Something like that: Code:currBookmark.Parent.Childs.Remove(currBookmark);
|
|
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 4/6/2020(UTC) Posts: 4  Location: Kiel
|
Many Thanks! Thats how it works: Code:if (pdfViewer.Document.Bookmarks.Remove(currBookmark) || currBookmark.Parent.Childs.Remove(currBookmark))
|
|
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,138
Thanks: 10 times Was thanked: 133 time(s) in 130 post(s)
|
In this case, one unnecessary search is performed in the collection of root elements. Here is another option :) Code:if((currBookmark.Parent?.Childs ?? pdfViewer1.Document.Bookmarks).Remove(currBookmark))
Edited by user Sunday, May 9, 2021 12:07:01 AM(UTC)
| Reason: Not specified
|
|
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 4/6/2020(UTC) Posts: 4  Location: Kiel
|
Nice code! Learned something again!
|
|
|
|
|
|
Forum Jump
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.
Important Information:
The Patagames Software Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close