0

我在尝试将浏览器组件导航到我的组合框选定值时遇到问题 - 当组合框值更改时。

当我像下面那样做时它工作正常(但是当combobox2改变时它不会被触发):

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] filePaths = Directory.GetFiles(sites.paths[comboBox1.SelectedIndex]);

        List<Foo> combo2data = new List<Foo>();

        foreach (string s in filePaths)
        {
            Foo fileInsert = new Foo();
            fileInsert.path = s;
            fileInsert.name = Path.GetFileName(s);
            combo2data.Add(fileInsert);
        }

        comboBox2.DataSource = combo2data;
        comboBox2.ValueMember = "path";
        comboBox2.DisplayMember = "name";         

        this.webBrowser1.Navigate((string)comboBox2.SelectedValue); // THE MOST IMPORTANT LINE : )
    }

但是我看到一个异常(无法将 App1.Foo 类型的对象转换为 System.String 类型),就像这样:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] filePaths = Directory.GetFiles(sites.paths[comboBox1.SelectedIndex]);

        List<Foo> combo2data = new List<Foo>();

        foreach (string s in filePaths)
        {
            Foo fileInsert = new Foo();
            fileInsert.path = s;
            fileInsert.name = Path.GetFileName(s);
            combo2data.Add(fileInsert);
        }

        comboBox2.DataSource = combo2data;
        comboBox2.ValueMember = "path";
        comboBox2.DisplayMember = "name";

    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
         this.webBrowser1.Navigate((string)comboBox2.SelectedValue);
    }
4

2 回答 2

0

我认为您应该在路径前添加类似 file:/// 的内容

但还有更多你应该考虑的

通过 HTTP(S) 检索到的页面上的本地文件链接在 Mozilla/Firefox 中被故意禁用,因为它们可能存在安全风险,并且过去曾被用于攻击。

http://kb.mozillazine.org/Firefox_:问题:_Links_to_Local_Pages_Don%27t_Work

于 2012-07-18T11:06:04.603 回答
0

尝试从此更改行的顺序:

    comboBox2.DataSource = combo2data; 
    comboBox2.ValueMember = "path"; 
    comboBox2.DisplayMember = "name"; 

对此:

    comboBox2.DisplayMember = "name";
    comboBox2.ValueMember = "path";
    comboBox2.DataSource = combo2data;

然后你的代码:

  (string)comboBox2.SelectedValue

将给出正确的结果,即 Path 的值

于 2012-07-18T11:30:25.957 回答