0

我的 WinForms 应用程序的组合框组件中列出了一些 flash 文件。现在,如果选择了文件,我希望它在 webBrowser 组件中播放,这里是代码:

将文件添加到组合框中:

   private void Form1_Load(object sender, EventArgs e)
    {
        string[] filePaths = Directory.GetFiles(@"c:\folder\");

          foreach (string s in filePaths)
        {
            comboBox1.Items.Add(s);
        } 
    }

播放 Flash 文件:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.webBrowser1.Navigate(comboBox1.SelectedText); //it doesnt work
    }

我可以正常打开 Flash 文件,看起来我无法从组合框中传递选定的值。

更新

为了确保此事件有效,我将其更改为:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
        this.textBox1.Text = "adsfadsf";
        this.textBox2.Text = comboBox1.SelectedText;
}

当我更改所选项目时,字符串 adsfadsf 出现在 textBox1 中,但 textBox2 保持为空。

更新2

这是完整的代码:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        string[] filePaths = Directory.GetFiles(@"c:\folder\");

        foreach (string s in filePaths)
        {
            comboBox1.Items.Add(s);
        } 
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }


    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.textBox2.Text = "adsfadsf";
        this.textBox1.Text = comboBox1.SelectedText;
       // this.webBrowser1.Navigate(comboBox1.SelectedText);
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

}

}

4

1 回答 1

3

快速回答您的问题 - 使用 comboBox1.Text,而不是 comboBox1.SelectedText。

这里更大的问题是,如果您正确使用调试器,您可能很快就会发现这一点。如果您不知道如何执行此操作,请参阅快速教程:

我为此使用VS2008,但在所有版本中几乎相同。

首先,在无法正常工作的代码行上设置断点。单击左边距,您将看到一个显示断点的红点。

接下来,运行您的代码。当您更改下拉菜单时,断点将触发,您将返回到 Visual Studio。您可以将鼠标指向“comboBox1.SelectedText”的“SelectedText”部分,并看到它显示任何空字符串,这就是您的应用程序无法按预期工作的原因。

Then hover over the "comboBox1" portion, and you'll see a small amount of info on the variable. Click the "+" on the left, and it will expand. You can scroll down through the properties and values, and see that the value you're looking for is in the "Text" property.

于 2012-07-13T11:54:17.320 回答