c# - 为什么我会收到 ""对象“不包含 "Index"的定义”错误?

标签 c# winforms user-interface selectedindexchanged

我看了又看,但我似乎找不到任何解决这个问题的方法,所以我决定问问。

我正在关注 this tutorial帮助我使用 WinForms 在 C# 中创建地址簿。我已经创建了 GUI 和将它们放在一起的“Person”类,我可以很好地添加条目。但是当我按照教程中的内容编写在它们之间切换的代码时,我收到此错误消息:

“object”不包含“Index”的定义,并且找不到接受“object”类型的第一个参数的扩展方法“Index”(您是否缺少 using 指令或程序集引用?)

这是我的“SelectedIndexChanged”事件代码。它与教程略有不同:我将“entryList.SelectedItems[0].Index”放入预定义的 int“i”中以节省写出八次,我的 GUI 相当不同,我定义了列表出于各种原因,Person 对象在单独的“方法”类中。尽管基本原理是相同的,所以我不明白为什么我的会标记错误而他的不会。

        private void entryList_SelectedIndexChanged(object sender, EventArgs e)
    {   //set the text boxes to display the data in the selected entry
        if (entryList.SelectedItems.Count > 0)
        {
            i = entryList.SelectedItems[0].Index;
            nameText.Text = Methods.list[i].Name;
            birthDay.Text = Methods.list[i].BirthDay.ToString();
            birthMonth.Text = Methods.list[i].BirthMonth.ToString();
            birthYear.Text = Methods.list[i].BirthYear.ToString();
            numberText1.Text = Methods.list[i].NumberPart1;
            numberText2.Text = Methods.list[i].NumberPart2;
            addressText.Text = Methods.list[i].Address;
            eMailText.Text = Methods.list[i].Email;
        }
    }

这是标记错误的“.Index”部分,我不知道为什么。那么,我哪里出错了?

编辑:为了澄清,我尝试了各种解决方法,例如

i = entryList.SelectedIndex;

但是那些仍然不允许我在 ListBox 中的项目之间切换,它会一直显示我最后输入的内容。

编辑 2:根据 Rotem 的要求提供更多信息。下面是添加条目的代码,这会将其添加到 Person 对象列表中,出于各种原因,该列表保存在单独的 Methods 类中(因此称为 Methods.list)。它还将此人的姓名添加到 ListBox 列表中,理论上,我随后希望能够通过单击 ListBox 中的姓名在查看不同条目之间切换,这是第一个代码段的用途。除非那不起作用。

        private void newEntry_Click(object sender, EventArgs e)
    {   //creates a new entry, displaying its data in the appropriate fields, and then saves it
        Person p = new Person(nameText.Text, Convert.ToByte(birthDay.Text), Convert.ToByte(birthMonth.Text), Convert.ToInt16(birthYear.Text), numberText1.Text, numberText2.Text, addressText.Text, eMailText.Text);
        Methods.list.Add(p);
        entryList.Items.Add(p.Name);
        nameText.Text = " ";
        birthDay.Text = "DD";
        birthMonth.Text = "MM";
        birthYear.Text = "YYYY";
        numberText1.Text = " ";
        numberText2.Text = " ";
        addressText.Text = " ";
        eMailText.Text = " ";
    }

编辑 3:根据要求,这是“Person”类的代码:

class Person
{
    private static string name;
    private static byte birthDay;
    private static byte birthMonth;
    private static short birthYear;
    private static string numberPart1;
    private static string numberPart2;
    private static string address;
    private static string email;

    //encapsulation goes here...

    public Person(string a, byte b, byte c, short d, string e, string f, string g, string h)
    {
        name = a;
        if (b <= 31 && c <= 12 && d < DateTime.Now.Year)
        {
            birthDay = b;
            birthMonth = c;
            birthYear = d;
        }
        else
            throw new ArgumentException("Invalid date of birth");
        numberPart1 = e;
        numberPart2 = f;
        address = g;
        email = h;
    }
}

最佳答案

entryList中是一个ListBox,那么它的项目只是object,它们不是一些跟踪索引的数据结构(与 ListView 项目不同,后者具有 Index 属性)。

相反,您可以使用 SelectedIndices ListBox 的属性。

int i = entryList.SelectedIndices[0];

关于c# - 为什么我会收到 ""对象“不包含 "Index"的定义”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20703476/

相关文章:

c# - 为什么默认情况下不使用 C# "using aliases"?

c# - 为什么 Mono 源代码大部分是 C#?

C# Winform PrintDialog 弹出页面

c# - 我可以在 ListView 的详细信息模式下显示链接吗?

c# - 有没有办法让一个 .NET 控件包含另一个由单独的 GUI 线程拥有的控件?

c# - 有没有一种简单的方法可以在 C# 中返回两种不同的运算符类型

c# - 在 LINQ 中查找相似的记录

c# - DateTimePicker 从不更新!

java - 如何将从数据库(主键)获取的字符串值排序为表中的整数?

wpf - 使用 Task 与 Dispatcher 进行 ui 线程操作