很抱歉,如果这是一个愚蠢的问题,但我是C#的初学者。我正在尝试创建一个简单的时间表帮助器,并在指示的代码行下出现错误:
private void buttonShow_Click(object sender, EventArgs e)
{
try
{
int val = Convert.ToInt32(textBoxVal.Text);
for (int i = 1; i < 13; i = i + 1)
{
listBoxTimes.Items.Add((i * val).ToString()); //'This is the line'//
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
textBoxVal.Text = "";
listBoxTimes.Items.Clear();
}
}
这是错误消息:
错误1'System.Windows.Forms.ListBox.ObjectCollection.Add(object)'的最佳重载方法匹配包含一些无效参数C:\ Users \ Chris \ Documents \ Visual Studio 2012 \ Projects \ sharptimestable \ sharptimestable \ Form1.cs 27 21 sharptimestable
这个是.ToString
转换的结果:
错误2参数1:无法从'方法组'转换为'对象'C:\ Users \ Chris \ Documents \ Visual Studio 2012 \ Projects \ sharptimestable \ sharptimestable \ Form1.cs 27 44 sharptimestable
答案 0 :(得分:4)
尝试将添加行更改为:
listBoxTimes.Items.Add(new ListboxItem("Name", (i * val).ToString()));
ListBox的内部(默认)数据结构是ListBoxItem。那就是你使用WPF。
如果您使用的是WinForms,那么请查看以下问题:
答案 1 :(得分:2)
您的第二个错误表示您忘记了{em>发布代码未显示的ToString
上的parens。除非你在发布错误的代码时解决了这个问题。
修复该错误也应该处理第一个错误。