1

タグの名前とxmlファイルを保持するリストボックスがあります。クラスにも保持されている他のタグがありますが、リストボックス内の値をクリックしてラベルテキストを値の内容に設定しようとすると「InvalidCastException が処理されませんでした。型 'System.String' のオブジェクトを型 'Coursework1.modules' にキャストできません。」というエラーが表示されます。コードは次のとおりです。これは、インデックスが変更されたときのリストボックス イベント内にあります。

for (int i = 0; i < selectionBox.Items.Count; i++)
        {
            if (selectionBox.GetSelected(i) == true)
            {
                infoLabel.Text = ((modules)selectionBox.Items[i]).mInfo;
                lectureSlotLabel.Text = ((modules)selectionBox.Items[i]).mLSlot;
                tutorialLabel.Text = ((modules)selectionBox.Items[i]).mTSlot;
                prerequisiteLabel.Text = ((modules)selectionBox.Items[i]).mPreReq;
                codeLabel.Text = ((modules)selectionBox.Items[i]).mCode;
                nameLabel.Text =((modules)selectionBox.Items[i]).mName;
            }
        }

//セレクションボックス作成

       String workingDir = Directory.GetCurrentDirectory();

        XmlTextReader textReader = new XmlTextReader(workingDir + @"\XML.xml");
        textReader.Read();
        XmlNodeType type;

        while (textReader.Read())
        {
            textReader.MoveToElement();
            type = textReader.NodeType;
            if (type == XmlNodeType.Element)
            {
                //if (textReader.Name == "Code")
                //{
                //    module.Add(new modules(textReader.ReadElementContentAsString(),
                //        textReader.ReadElementContentAsString(),
                //        textReader.ReadElementContentAsString(),
                //        textReader.ReadElementContentAsString(),
                //        textReader.ReadElementContentAsString(),
                //        textReader.ReadElementContentAsString(),
                //        textReader.ReadElementContentAsString()));
                //}


                if (textReader.Name == "Code")
                {
                    textReader.Read();
                    code = textReader.Value;
                    Console.WriteLine(code);

                }
                if (textReader.Name == "Name")
                {
                    textReader.Read();
                    name = textReader.Value;
                    //selectionBox.Items.Add(name);
                    Console.WriteLine(name);

                }
                if (textReader.Name == "Semester")
                {
                    textReader.Read();
                    semester = textReader.Value;
                    Console.WriteLine(semester);

                }
                if (textReader.Name == "Prerequisite")
                {
                    textReader.Read();
                    preReq = textReader.Value;
                    Console.WriteLine(code);
                }
                if (textReader.Name == "LectureSlot")
                {
                    textReader.Read();
                    lSlot = textReader.Value;
                    Console.WriteLine(lSlot);
                }
                if (textReader.Name == "TutorialSlot")
                {
                    textReader.Read();
                    tSlot = textReader.Value;
                    Console.WriteLine(tSlot);
                }
                if (textReader.Name == "Info")
                {
                    textReader.Read();
                    info = textReader.Value;
                    Console.WriteLine(info);
                    module.Add(new Modules(code, name, semester, tSlot, lSlot, info, preReq));
                }


            }


            //Console.WriteLine(module);

        }
        foreach (object o in module)
        {
            Modules m = (Modules)o;
            //String hold = m.mName;
            selectionBox.Items.Add(m.mName);
        }
        textReader.Close();

//使用中のラベルテキスト

var xmlFile = XDocument.Load(xmlDirectory + @"\XML.xml");
        //Finds the XML file again and looks for the Name tag
        var mName = from directory in xmlFile.Descendants("Name")
                    where directory.Value == (String)selectionBox.SelectedItem
                    select directory.Parent.Element("Name").Value;
        //in the mName we search the directory for the Name tag and after we search
        //for the selected item of the list in the directory after that we look for 
        //the "Name" element in the directory and that gets the name value and saves it to
        //mName
        foreach (var item in mName)
        {
            //use the loop to make it dynamic as when the selected item changes so does,
            //the moduleName label
            nameLabel.Text = item.ToString();
        }
4

2 に答える 2

2

この行:

selectionBox.Items.Add(m.mName);

は単にmoduletoの名前を付けているだけです。これは、 aを aselectionBoxにキャストしようとして問題が発生した理由を説明しています。調べる必要があるのは、データバインディングの使用です。ループの代わりに次のコードを追加します。stringmoduleforeach (object o in module)

selectionBox.DataSource = module;
selectionBox.DisplayMember = "mName";

また、インデックス変更イベントでは、次のように選択したアイテムを取得できます。

modules m = (modules)selectionBox.SelectedItem;

さらに、この方法で選択した項目を取得すると、for現在 index changed イベント ハンドラーにあるループをなくすことができます。

編集:

現在、それぞれの名前を に追加moduleしていselectionBoxます。ただし、選択したアイテムを取得しようとすると、オブジェクトの名前を実際のオブジェクトにキャストしようとしているため、エラーが発生します。

だから私のソリューションを使用して:

selectionBox.DataSource = module;

作成したオブジェクトのリストをDataSourceselectionBoxおよび

selectionBox.DisplayMember = "mName";

オブジェクトのどのプロパティを表示テキストとして使用するかを示します。この場合mName、これは以前に に追加していたプロパティでしたselectionBox

于 2013-03-12T23:57:11.873 に答える
-3

これは、質問自体に対する回答ではなく、OPがコメントに投稿した質問に対する回答です。

@HighCore最初のキャストの意味を教えてください。

あなたはすべきではありません

((modules))selectionBox.Items[i]

何度も。代わりに、

if (selectionBox.GetSelected(i) == true)
{
    var module = selectionBox.Items[i] as modules; 

    if (module != null)
    {
        infoLabel.Text = module.mInfo;
        //etc...
    }
}

これはより簡単で安全です(私は実際に物事が最初からであることを確認しているためですmodules。これにより、例外のキャストが防止されます。

于 2013-03-12T22:50:59.113 に答える