0

以下は、私が約 2 週間取り組んできたコードで、最後の情報 (Class MyClient) を入力するまでは機能していると思っていましたが、Process.Start(url); で win32 エラーが発生しています。指定されたファイルが見つからないと言います。「ieexplorer.exe」に設定して、URLのIEをロードしようとしましたが、変更はありません。

public partial class Form1 : Form
{
    List<MyClient> clients;
    public Form1()
    {
        InitializeComponent();
        clients = new List<MyClient>();
        clients.Add(new MyClient { ClientName = "Client 1", UrlAddress = @"http://www.google.com" });
        BindBigClientsList();
    }

    private void BindBigClientsList()
    {
        BigClientsList.DataSource = clients;
        BigClientsList.DisplayMember = "ClientName";
        BigClientsList.ValueMember = "UrlAddress";
    }

    private void BigClientsList_SelectedIndexChanged(object sender, EventArgs e)
    {
        MyClient c = BigClientsList.SelectedItem as MyClient;
        if (c != null)
        {
            string url = c.ClientName;
            Process.Start("iexplorer.exe",url);
        }
    }
}
class MyClient
{
    public string ClientName { get; set; }
    public string UrlAddress { get; set; }
}

}

4

1 に答える 1

2

を URL として使用しClientNameていますが、これは正しくありません...

string url = c.ClientName;

...はず...

string url = c.UrlAddress;

どちらも指定しないでくださいiexplorer.exe。デフォルトでは、OS の URL はデフォルトの Web ブラウザで開かれます。ユーザーが Internet Explorer を使用する必要が本当にない限り、システムにブラウザーを選択させることをお勧めします。

UPDATE
OPのコメントに対応して...

「空白」の意味によって異なります。つまりnull、いいえ、これは不可能です。をリストの最初のエントリとして使用nullすると、 を呼び出そうとすると NullReferenceException が発生しますc.UrlAddressMyClientダミー値を持つプレースホルダー インスタンスを使用できる場合があります...

clients = new List<MyClient>();
clients.Add(new MyClient { ClientName = "", UrlAddress = null });
clients.Add(new MyClient { ClientName = "Client 1", UrlAddress = @"http://www.google.com" });

しかし、その後、アクションメソッドを次のように変更する必要があります...

private void BigClientsList_SelectedIndexChanged(object sender, EventArgs e)
{
    MyClient c = BigClientsList.SelectedItem as MyClient;
    if (c != null && !String.IsNullOrWhiteSpace(c.UrlAddress))
    {
        string url = c.ClientName;
        Process.Start("iexplorer.exe",url);
    }
    else
    {
        // do something different if they select a list item without a Client instance or URL
    }
}
于 2012-09-17T19:32:59.857 に答える