3

I have a CheckedListBox on a backup application I am writing. I want the user to pick the folders they want backing up i.e. desktop I have my for loop working for each ticked item etc but I want the user to see the tick box labeled as "Desktop" rather than c:\users\username\desktop

Can someone inform me how to change the listbox label to something different than what is actually being returned to my for loop.

1
  • Are you working with WinForms or WPF? Commented Nov 23, 2011 at 11:25

3 Answers 3

2

You should create a type that holds the full path and override ToString() to return what you want to display in the CheckedListBox. CheckedListBox.SelectedItems will then hold a list of your type.

    public void PopulateListBox()
    {
        _checkedListBox.Items.Add(new BackupDir(@"C:\foo\bar\desktop", "Desktop"));
    }

    public void IterateSelectedItems()
    {
        foreach(BackupDir backupDir in _checkedListBox.CheckedItems)
            Messagebox.Show(string.format("{0}({1}", backupDir.DisplayText, backupDir.Path));            
    }

    public class BackupDir
    {
        public string Path { get; private set; }
        public string DisplayText { get; private set; }

        public BackupDir(string path, string displayText)
        {
            Path = path;
            DisplayText = displayText;
        }

        public override string ToString()
        {
            return DisplayText;
        }
    }

you could of course strip the folder name from the path if that is what you wanted to do for every list item and just have the path arg on the BackupDir class.

Sign up to request clarification or add additional context in comments.

6 Comments

The paths are all hard coded I want desktop, My Documents etc each with a seperate tickbox. Myles has got what I wanted to do however when I imnplement your suggestion the tickbox is labelled "system_backup(name of the project).form1+BackupDir" Any ideas why this has come up, the code you posted looks correct to me but it does not label the box with my string
I have tried implementing your solution but get system_backup.form1+BackupDir instead of Desktop. Any suggestions? - Your solution fits best for me as I would like to type the string to be displayed Just wont work though.cheers
My bad, neglected to override ToString() on BackupDir, have now edited.
Almost there Myles, it works apart from when I do public void IterateSelectedItems() { foreach (Backupdir backupdir in checkedListBox1.SelectedItems) { MessageBox.Show(sources); } } It Returns "Desktop" rather than C:\foo\bar\desktop
what is sources? inside the loop the backupDir variable is of type BackupDir so you can access Path or DisplayText, I have edited the answer to demonstrate.
|
0

How are you getting the folders names, by FolderBrowserDialog, or it is manual input by the user?

Use .Split('\')

1 Comment

Myles has got what I want to do but when I implement that code it labels the box as system_backup.form1+BackupDir rather than "Desktop"
0

Here is my suggestion for you. Create a data class for your backup folders like that:

public class BackupFolder
{
    private string folderPath;

    public BackupFolder(string folderPath)
    {
        this.folderPath = folderPath;
        FolderName = folderPath.Split(new[] { '\\' }).Last();
    }

    public string FolderName { get; private set; }
}

Then set a list of those files as DataSource for the CheckedListBox and set the DisplayMember to the property that contains the value as you want to display it. Like this:

var data = new BindingList<BackupFolder>();
data.Add(new BackupFolder("D:\\Data"));
checkedListBox1.DataSource = data;
checkedListBox1.DisplayMember = "FolderName";

4 Comments

Thanks for that, It is displaying correctly now i.e. it says desktop on the box on the form but as above the path is actually system_backup.form1+backupfolder rather than the actual D:\data as defined in your code. Also from visual studio when hovering over checkedlistbox1.dislpaymember & displaysource it says "this property is not relevant to this class"
@meeeeeeeeee: Excuse my "d:\\data" - it was just a sample, not the solution. ;-) Please don't forget to vote and accept an answer, that's how StackOverflow works. See faq
I know thanks, but I tried proper paths and got the same result, I ticked data but it returned system_backup.form1+backupfolder for some reason rather than the proper path.
@meeeeeeeeee: You have to fill the list with the paths you want to show by creating for each of those paths a new BackupFolder object giving the desired path in the constructor. Than it should work like you expect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.