Okay so I am writing some C# and the program at one point reads in a file and stores each line in a string array. This part of the code works I have tested it. Well then I have the string displayed in a listBox with this string array using

listBox1.DataSource = storedFileArray; //storedFileArray is the string array

Well then I go onto a checkedBoxList and try doing the same thing but using the line

checkedListBox1.Items.AddRange(shiftTypeArray)

//their in different classes

The only problem is no matter what I try I can't get more then one line (see link for screen)

http://i109.photobucket.com/albums/n65/Vin_diesalxxx/addEmployeecheckedBox.jpg

I have tried almost everything to get this to work like

string transfer = shiftTypeIn[0];
checkedListBox1.Items.AddRange(transfer)
string transfer2 = shiftTypeIn[1];
checkedListBox1.Items.AddRange(transfer)

...

And still it displays like the screenshot shows (NOTE: If I was to display the string variables transfer or transfer2 as a MessageBox they display the line of text fine). I have tried a for loop of .Add(shiftTypeIn) and have no luck. I can't get this to work. I am not brand new to C# but I still have alot to learn, so any help can help.

Oh also I tried a test and did a code like this


string [] values = {"1", "2", "3"};
checkedListBox1.Items.AddRange(values)…

This code works fine. Now the reason I read from a file is because the part of the code that has the listBox allows the user to add or remove (well their shifts) and then the changes are saved to a txt file, then tee file can be read in (from that delegate call) so I can save them when the program closes. Thanks again to anyone who can help I really need it, I have been looking at this for hours. (Also if you would like to see more of the code I am more then willing to show you)

Recommended Answers

All 6 Replies

Let's start it over...

string fileData = File.ReadAllText(@"file path");
checkedListBox1.Items.AddRange(fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));

When you are adding a single item (not an array) use Add() and when you are using an array use AddRange().

Working version of your code:

string transfer = shiftTypeIn[0];
checkedListBox1.Items.Add(transfer)
string transfer2 = shiftTypeIn[1];
checkedListBox1.Items.Add(transfer)

Thanks

Yeah farooqaaa I reallize that I was just saying I tried both ways, also Randy you are a genius! What exactly does that second line of code do? Also is their a way I can register a length of of this? Like save it to a variable or something?

Actually scratch that last comment I figured out how to find the length

string[] value = { "1", "2", "3" };
            foreach (string values in value)
            {
                checkedListBox1.Items.Add(values);
            }

here you go.
also i already tested it and it worked

Let's start it over...

string fileData = File.ReadAllText(@"file path");
checkedListBox1.Items.AddRange(fileData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));

I just thought I would post this for anyone who may look at this thread and this is the working answer. I haven't had a problem since I tried it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.