开发者

Using LINQ expressions for custom data types

In my "settings" dialog box i have two list boxes which look like this:

+------+   +---------+
|File  |   |Extension|
|type  |   |         |
|      |   |         |
+------+   +---------+

I have some data that needs to be loaded from XML, so i made a class for that:

public class XmlConfig
{
    public List<config> con = new List<config>();
    public XmlConfig(string PathToSettings)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(PathT开发者_JAVA百科oSettings);
        XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName("ext");
        foreach (XmlNode xmlnode in xmlNodes)                          
            con.Add(new config()
            {
                FolderName = xmlnode.Attributes["File"].Value,
                Extensions = xmlnode.InnerText.Split(',').ToList<string>()
            });
    }
}

public struct config
{
    public string FolderName;
    public List<string> Extensions;
}

now i want to fill the folderlistbox with all the values in the xml file and that is happening correctly but the problem is that i want it to be like when the file type list item is clicked the corresponding extensions should be displayed and i have no idea about how to do that. somebody suggested to use linq but since i am new to c# i have no idea what it is.


You have to subscribe to fileTypes ListView event SelectedValueChanged and then in handler of that event filter the Extensions collection and bind to another ListBox

    private void listBox1_SelectedValueChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem != null)
        {
            //get selected config object
            config conf = listBox1.SelectedItem as config;

            //fill extensions listbox
            listBox2.DataSource = config.Extensions;
        }
    }


i finally found a way to do it. plz correct me if i am wrong...

  private void folderListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            extListBox.Items.Clear();
            string selectVal = folderListBox.GetItemText(folderListBox.SelectedItem);            
            //var extn = from ext in configData.con where ext.FolderName == folderListBox.SelectedValue select ext;
            var extn = from ext in configData.con where ext.FolderName == selectVal select ext;
            //var result = List 
            foreach (var ext in extn)
            {
                foreach (string extension in ext.Extensions)
                    extListBox.Items.Add(extension);
            }                
        }

before this i was doing

 var extn = from ext in configData.con where ext.FolderName == selectVal select ext.Extensions;

but it showed an error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜