双击导航到选定的 ListViewItem

发布于 2025-01-02 03:35:28 字数 3549 浏览 0 评论 0原文

我正在 Visual Studio 中制作一个简单的浏览器。
为了能够保存和删除书签,我使用了以下代码:

当打开 frmFavorites 时,它将读取名为 favorites.xml 的 xml 文件

private void frmFavorites_Load(object sender, EventArgs e) {
        System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();
        loadDoc.Load(Application.StartupPath + "\\Favorites.xml");

        foreach (System.Xml.XmlNode favNode in loadDoc.SelectNodes("/Favorites/Item")) {
            listViewFavs.Items.Add(favNode.Attributes["url"].InnerText);
        }
    }

当再次关闭表单时,它将覆盖该 xml 文件并将所有剩余项目保存在 xml 文件中

private void frmFavorites_FormClosing(object sender, FormClosingEventArgs e) {
        System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(Application.StartupPath + "\\Favorites.xml", null);

        writer.WriteStartElement("Favorites");
        for (int i = 0 ; i < listViewFavs.Items.Count ; i++) {
            writer.WriteStartElement("Item");
            writer.WriteAttributeString("url", listViewFavs.Items[i].Text);
            writer.WriteEndElement();
        }
        writer.WriteEndElement();
        writer.Close();
    }

要添加和删除书签,我使用以下代码:

private void btnAddFav_Click(object sender, EventArgs e) {
        ListViewItem item = new ListViewItem(txtURL.Text);
        listViewFavs.Items.Add(txtURL.Text);
    }

    private void btnDelFav_Click(object sender, EventArgs e) {
        try {
            listViewFavs.Items.RemoveAt(listViewFavs.SelectedIndices[0]);
        }
        catch {
            MessageBox.Show("Je moet een item selecteren", "Geen item geselecteerd", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

现在,我的问题:
我想让我的用户能够双击某个项目,以便他们可以导航到他们保存的收藏夹。至少,这是应该发生的事情。
到目前为止,我已经尝试了一些代码并最终得到以下结果:

private void listViewFavs_DoubleClick(object sender, EventArgs e) {
        try {
            FrmMain Main = new FrmMain();
            Main.navigate(listViewFavs.SelectedItems[0].Text);
        }
        catch {
            MessageBox.Show("Je moet een item selecteren", "Geen item geselecteerd", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

frmMain 是我的浏览器,navigate 是我用来检查 URL 和导航的公共方法。

为了进行导航,我使用以下代码:

public void navigate(String URL) {
        if (String.IsNullOrEmpty(URL) || URL.Equals("about:blank")) {
            GetActiveBrowser().DocumentText = Properties.Resources.FirstTime; // this is a HTML-doc you also see when you open the browser for the 1st time
            txtURL.Text = "about:blank";
            return;
        } else if (!URL.StartsWith("http://") && !URL.StartsWith("https://") && !URL.StartsWith("file://") && !URL.StartsWith("ftp://"))
            URL = "http://" + URL;
        try {
            GetActiveTab().Text = "... Loading ...";
            this.Icon = Properties.Resources.loading1;
            GetActiveBrowser().Navigate(new Uri(URL));
        }
        catch (System.UriFormatException) {
            MessageBox.Show("'" + URL + "' is geen geldige URL", "Ongeldige URL", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    } // go to URL

如您所见,我使用 GetActiveTab() 和 GetActiveBrowser:

private WebBrowser GetActiveBrowser() {
        return (WebBrowser)tabs.SelectedTab.Controls[0];
    }

private TabPage GetActiveTab() {
        return tabs.SelectedTab;
    }

实际发生的情况:
我双击该项目
什么也没发生 -.- 没有导航,没有错误,什么也没有

有谁知道解决这个问题吗?
我感谢所提供的任何帮助。

I'm making a simple browser in Visual Studio.
To be able to save and delete bookmarks, I'm using these codes:

When the frmFavorites is opened, it'll read an xml-file named Favorites.xml

private void frmFavorites_Load(object sender, EventArgs e) {
        System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();
        loadDoc.Load(Application.StartupPath + "\\Favorites.xml");

        foreach (System.Xml.XmlNode favNode in loadDoc.SelectNodes("/Favorites/Item")) {
            listViewFavs.Items.Add(favNode.Attributes["url"].InnerText);
        }
    }

When the form is closed again, it'll overwrite the xml-file and save all remaining items in the xml-file

private void frmFavorites_FormClosing(object sender, FormClosingEventArgs e) {
        System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(Application.StartupPath + "\\Favorites.xml", null);

        writer.WriteStartElement("Favorites");
        for (int i = 0 ; i < listViewFavs.Items.Count ; i++) {
            writer.WriteStartElement("Item");
            writer.WriteAttributeString("url", listViewFavs.Items[i].Text);
            writer.WriteEndElement();
        }
        writer.WriteEndElement();
        writer.Close();
    }

To add and delete a bookmark, I'm using this code:

private void btnAddFav_Click(object sender, EventArgs e) {
        ListViewItem item = new ListViewItem(txtURL.Text);
        listViewFavs.Items.Add(txtURL.Text);
    }

    private void btnDelFav_Click(object sender, EventArgs e) {
        try {
            listViewFavs.Items.RemoveAt(listViewFavs.SelectedIndices[0]);
        }
        catch {
            MessageBox.Show("Je moet een item selecteren", "Geen item geselecteerd", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

Now, my problem:
I'd like to give my users the ability to double click on an item so they can navigate to their saved fav. At least, that's what SHOULD happen.
For so far, I've tried some codes and ended up with this:

private void listViewFavs_DoubleClick(object sender, EventArgs e) {
        try {
            FrmMain Main = new FrmMain();
            Main.navigate(listViewFavs.SelectedItems[0].Text);
        }
        catch {
            MessageBox.Show("Je moet een item selecteren", "Geen item geselecteerd", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

frmMain is my browser and navigate is a public method that I use to check the URL's and to navigate.

To navigate, I use this code:

public void navigate(String URL) {
        if (String.IsNullOrEmpty(URL) || URL.Equals("about:blank")) {
            GetActiveBrowser().DocumentText = Properties.Resources.FirstTime; // this is a HTML-doc you also see when you open the browser for the 1st time
            txtURL.Text = "about:blank";
            return;
        } else if (!URL.StartsWith("http://") && !URL.StartsWith("https://") && !URL.StartsWith("file://") && !URL.StartsWith("ftp://"))
            URL = "http://" + URL;
        try {
            GetActiveTab().Text = "... Loading ...";
            this.Icon = Properties.Resources.loading1;
            GetActiveBrowser().Navigate(new Uri(URL));
        }
        catch (System.UriFormatException) {
            MessageBox.Show("'" + URL + "' is geen geldige URL", "Ongeldige URL", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    } // go to URL

As you can see, I use a GetActiveTab() and GetActiveBrowser:

private WebBrowser GetActiveBrowser() {
        return (WebBrowser)tabs.SelectedTab.Controls[0];
    }

private TabPage GetActiveTab() {
        return tabs.SelectedTab;
    }

What actually happens:
I double-click on the item
Nothing happens -.- No navigation, no error, no nothing

Does anyone have an idea to fix this problem?
I appreciate any help given.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2025-01-09 03:35:28

您似乎正在从列表中删除该项目,然后尝试访问该项目。

在这里您删除了该项目:

listViewFavs.Items.RemoveAt(listViewFavs.SelectedIndices[0]); 

然后在这里您尝试访问所选项目,但它不在那里,您刚刚删除了它:

Main.navigate(listViewFavs.SelectedItems[0].Text); 

尝试不调用 RemoveAt,您无论如何都不想删除它,对吧?如果目标只是使用所选项目在父表单上导航,那么将其从书签列表中删除是违反直觉的。

编辑:

要调试navigate,您需要单步执行该调用(在第一行设置断点)并验证URL 是否正在获取值。使用单步执行(通常是 F10)来遍历该函数并验证它采用您期望的代码路径。

另外,也许更重要的是,您似乎正在创建一个新的 FrmMain 但没有显示它。然后它就会超出范围,因为它是在该方法中定义的,然后 GC 会将其清除。

您应该在这里执行以下两件事之一:

1) 使用 FrmMain 的现有实例,这可能是这种情况,因为我假设有人启动 frmFavorites 来自 FrmMain,然后双击以导航回已打开的窗口。

2) 或者调用 Main.Show() 使新表单可见,以便在定义它的方法返回后它将继续存在。

如果当用户单击收藏夹时 FrmMain 实例可能已存在或尚未存在,则您可以使用延迟加载的静态实例,您可以在需要访问主窗体的任何地方引用该实例。因此,在双击处理程序中:

FrmMain main = FrmMain.Instance;
main.Show();
main.navigate(listViewFavs.SelectedItems[0].Text);

然后在 FrmMain 中:

static FrmMain _instance;
public static FrmMain Instance 
{
   if (_instance==null)
       _instance = new FrmMain();

   return _instance;
}

It looks like you're removing the item from the list, then trying to access that item.

Here you remove the item:

listViewFavs.Items.RemoveAt(listViewFavs.SelectedIndices[0]); 

then right here you are trying to access the selected items, but it's not there, you just removed it:

Main.navigate(listViewFavs.SelectedItems[0].Text); 

Try not calling RemoveAt, you don't want to remove it anyway right? If the goal is just to use that selected item to navigate on the parent form, then removing it from the list of bookmarks is counter intuitive.

Edit:

To Debug navigate, you'll need to step into that call (set a breakpoint at the first line) and verify that URL is getting a value. Use Step-Over (usually F10), to walk through the function and verify it takes the code path you're expecting it to.

Also, and perhaps more importantly, it seems that you are creating a new FrmMain but not showing it. Then it goes out of scope, since it is defined in that method, and the GC then sweeps it up.

You should being doing one of two things here:

1) Using an existing instance of FrmMain, which is probably the case, since I'm assuming someone launches frmFavorites from FrmMain and then is double clicking to navigate back in the already open window.

2) Or calling Main.Show() to make the new form visible so it will live after the method it was defined in has returned.

If the FrmMain instance may or may not be already in existence when the users clicks on a favorite, you could use a lazy loaded static instance that you refer to anywhere you need access to the main form. So in the double click handler:

FrmMain main = FrmMain.Instance;
main.Show();
main.navigate(listViewFavs.SelectedItems[0].Text);

then in FrmMain:

static FrmMain _instance;
public static FrmMain Instance 
{
   if (_instance==null)
       _instance = new FrmMain();

   return _instance;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文