none
CheckListBox show folder name, but include folder path when checked RRS feed

  • Question

  • I am  working on a game launcher, and I have a CheckListBox that populates with mods the user has. Right now the list is populated by checking two folders (will add more later on). When the user checks the mod they want to use when Launching the game, it gets added to the argument start parameter.

    Right now the code I use to populate the CheckListBox:

                    string HiddenFolder = regA3Path + WorkshopFolder;
                    string[] dirs = Directory.GetDirectories(regA3Path, modNames);
                    string[] hiddendirs = Directory.GetDirectories(HiddenFolder, modNames);
    
                    clbMods.Items.Clear();
                    
                    for(int getModz = 0; getModz < dirs.Length; getModz++)
                    {
                        string folderName = Path.GetFileName(dirs[getModz]);
                        clbMods.Items.Add(folderName, false);
                    }
                    
                    for(int modzGet = 0; modzGet < hiddendirs.Length; modzGet++)
                    {
                        //string hiddenFolderName = Path.GetFileName(hiddendirs[modzGet]);
                        string hiddenFullPath = Path.GetFullPath(hiddendirs[modzGet]);
                        clbMods.Items.Add(hiddenFullPath, false);
                    }

    As that shows, the first set of mods loaded are just by name, the second set is loaded by their full path. The parameter will have the FolderName for each mod selected. Showing the full path of the mod would mean the CheckListBox would need to stretch. I was wondering if I could change it to just show the folder name, and when selected it would add the fullpath to the parameter.

    Below is the code I use to add each checked mod into the list

    if (clbMods.CheckedItems != null)
                {
                    foreach (string modz in clbMods.CheckedItems)
                    {
                        withMods += "" + modz + ";";
                        Mods = " -mod=" + "\"" + withMods + "\"";
                    }
                }

    Then at the start point, the mod parameter would look something like this:
    -mod="@Mod1;@Mod2;D:\OtherMods\@Modded2;D:\OtherMods\@ModLocation2;"

    Any idea on how to do that?

    Sunday, May 19, 2019 7:30 PM

Answers

  • Hello,

    Conceptually speaking you can setup a CheckedListBox via it's DataSource property then to access items cast the an item as the class type used to load the CheckedListBox as shown below.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                var itemList = new List<Item>()
                {
                    new Item() {Checked = true, Description = "Fruit", DisplayMember = "Apples"},
                    new Item() {Checked = false, Description = "Car", DisplayMember = "Miata"},
                    new Item() {Checked = true, Description = "Programming language", DisplayMember = "C#"}
                };
    
                checkedListBox1.DataSource = itemList;
                for (int index = 0; index < itemList.Count; index++)
                {
                    checkedListBox1.SetItemChecked(index, itemList[index].Checked);
                }
            }
    
            private void getCurrentItemButton_Click(object sender, EventArgs e)
            {
                var currentItem = ((Item) checkedListBox1.SelectedItem);
                MessageBox.Show(currentItem.Description);
            }
        }
    
        public class Item
        {
            public bool Checked { get; set; }
            public string DisplayMember { get; set; }
            public string Description { get; set; }
            public override string ToString()
            {
                return DisplayMember;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by DragulaDemos Monday, May 20, 2019 2:54 AM
    Monday, May 20, 2019 12:16 AM
    Moderator