读书人

本人C#版的一个有关问题:InvalidArgu

发布时间: 2012-01-22 22:38:43 作者: rapoo

本人C#版的一个问题:InvalidArgument=“0”的值对于“SelectedIndex”无效。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Web_浏览器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
//抱错:InvalidArgument=“0”的值对于“SelectedIndex”无效。
webBrowser1.GoHome();
}

private void goButton_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(new Uri(comboBox1.SelectedItem.ToString()));
}

private void 主页ToolStripMenuItem_Click(object sender, EventArgs e)
{
webBrowser1.GoHome();
}

private void 前进ToolStripMenuItem_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}

private void 返回ToolStripMenuItem_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
}
}

帮助是这么说的:
SelectedIndex
属性值
当前选定项的从零开始的索引。如果未选定任何项,则返回值为负一 (-1)。

当我把0改成-1后
在 webBrowser1.Navigate(new Uri(comboBox1.SelectedItem.ToString()));
出现如下错误:

未将对象引用设置到对象的实例。

这是那里有问题,如果这里想改能运行,怎么改?


[解决办法]
当我把0改成-1后
在 webBrowser1.Navigate(new Uri(comboBox1.SelectedItem.ToString()));
出现如下错误:

未将对象引用设置到对象的实例。
-----------------
因为你改成-1,所以没有comboBox1就没有选择如何item,所以SelectedItem就会为null

读书人网 >C#

热点推荐