[英]My code causes URL to open in 2 different tabs, why?
As person helped me to figure out how to start open URL in default browser now I am having bit of a problem which causes to open URL in 2 different tabs when clicked. 由于有人帮助我弄清楚了如何在默认浏览器中启动打开URL,我遇到了一个问题,该问题导致在单击时在2个不同选项卡中打开URL。 What could be the cause for this?
可能是什么原因造成的?
EDIT: I think it's good to point out that I am using a ListView in detailed mode. 编辑:我认为最好指出我在详细模式下使用ListView。 So when user clicks column with URL, it should open a single window in the browser.
因此,当用户单击带有URL的列时,它应该在浏览器中打开一个窗口。
lvWeb.MouseMove += new MouseEventHandler(lvWeb_MouseMove);
lvWeb.MouseUp +=new MouseEventHandler(lvWeb_MouseUp);
private void lvWeb_MouseMove(object sender, MouseEventArgs e)
{
var hit = lvWeb.HitTest(e.Location);
if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1])
lvWeb.Cursor = Cursors.Hand;
else lvWeb.Cursor = Cursors.Default;
}
private void lvWeb_MouseUp(object sender, MouseEventArgs e)
{
var hit = lvWeb.HitTest(e.Location);
if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1])
{
var url = new Uri(hit.SubItem.Text);
System.Diagnostics.Process.Start(url.ToString());
}
}
As one person sugested, I have visited the article which did the trick and it works as following: 正如一个人向往的那样,我访问了完成该技巧的文章 ,其工作方式如下:
private string getDefaultBrowser()
{
string browser = string.Empty;
RegistryKey key = null;
try
{
key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
//trim off quotes
browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!browser.EndsWith("exe"))
{
//get rid of everything after the ".exe"
browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
}
}
finally
{
if (key != null) key.Close();
}
return browser;
}
private void lvWeb_MouseUp(object sender, MouseEventArgs e)
{
var hit = lvWeb.HitTest(e.Location);
if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1])
{
var url = new Uri(hit.SubItem.Text);
//System.Diagnostics.Process.Start(url.ToString());
Process p = new Process();
p.StartInfo.FileName = getDefaultBrowser();
p.StartInfo.Arguments = url.ToString();
p.Start();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.