Search code examples
c#checklistbox

CheckBoxList Datasource won't set in Program Launch.


So I'm trying to load file names from a directory and set them to a checkList box however the box always remains blank, any ideas why this could be?

Please see code below.

static void Main( string[] args )
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var launchArg = args.FirstOrDefault( arg => !arg.StartsWith( "-" ) );
            launchArg = @"Apollo";
            if (!String.IsNullOrEmpty(launchArg))
            {
                var configID = launchArg;

                var configuration = AdrConfigurationManager.AdrSettings.Cast<AdrConfigurationElement>().SingleOrDefault(c => String.Equals(c.ID, configID, StringComparison.CurrentCultureIgnoreCase));
                if (configuration == null) throw new ArgumentException(String.Format("Could not find the configuration with ID={0}.", configID));

                // Search for the files.
                DirectoryInfo dirinfo = new DirectoryInfo(@"C:\SVNRepositiory\BMSConsulting\Common\ADR\Client Scripts\" + configuration.ID);
                var fileQuery =
                    from FileInfo fileinfo in dirinfo.GetFiles()
                    orderby fileinfo.Name.Substring(0, 1) ascending
                    select fileinfo.Name;
                // Add files to checkbox. 
                var chkListBox = new CheckedListBox();
                chkListBox.DataSource = fileQuery.ToArray();
                for (int i = 0; i < chkListBox.Items.Count; i++)
                    chkListBox.SetItemChecked(i, true);
                var progress = new AdrProgress(configuration, chkListBox);
                progress.Show();

                Application.Run( progress );
            }
            else
            {
                Application.Run(new ADR());   
            }

Solution

  • Rather than add as a datasource iterate through the query and add.

    foreach (var variable in fileQuery)
    {
        chkListBox.Items.Add(variable);
    }