我在Visual Studio中製作了一個簡單的瀏覽器。
爲了能夠保存和刪除書籤,我使用這些代碼:雙擊導航到選定的ListViewItem
當frmFavorites被打開,它會讀取名爲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);
}
}
當形式的XML文件再次關閉,這將覆蓋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是我的瀏覽器,導航是我用它來檢查的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;
}
實際發生的:
我雙擊
什麼都沒有發生 - .-沒有導航,沒有錯誤,沒有任何東西
做一個有沒有想法解決這個問題?
我感謝任何幫助。
-.-看就在這:■使用removecode爲SelectIndices當時:■ – GroundZero 2012-02-02 21:38:32
刪除問題固定的,但它贏得了無法瀏覽:s – GroundZero 2012-02-02 21:43:36
如果您需要幫助,您需要發佈導航代碼。 – Erikest 2012-02-02 22:10:01