Hi all,

I have two problems
1. I have a comma separated string values. I want to load them all into checkedlistbox as items.....HOW?

2. how do I get the itemindex of checkedlistbox, passing the text?

regards and thanks
Jeet

First step is to split the string into an array.

string MyString = "Games,Tvs,Shows";
string[] MyArray = new string[3];
MyArray = MyString.Split(',');
CheckBoxList1.DataSource = MyArray;
CheckBoxList1.DataBind();

and the second question I do not really understand but this is what i think.

private string GetIndex(string descr)
    {
        int i = 0;
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Text == descr)
            {
                return i.ToString();
            }
            i++;
        }
        return "";
    }

In order to get the value call the function like this String CheckIndex = GetIndex("Shows"); should return 2
Regards

Make an empty WindowsApplication.
Add a CheckedListBox via the toolbox.
Add the following lines after InitializeComponent(); in the Form1 constructor :

public Form1()
        {
            InitializeComponent();
            this.checkedListBox1.Items.Add("azerty");
            this.checkedListBox1.Items.Add("querty");
            this.checkedListBox1.Items.Insert(1, "boe");

        }

The first two lines add two strings, the thirth line inserts a string between these two strings. Index 1 meaning the second position because the Items collection is zero based.

First step is to split the string into an array.

string MyString = "Games,Tvs,Shows";
string[] MyArray = new string[3];
MyArray = MyString.Split(',');
CheckBoxList1.DataSource = MyArray;
CheckBoxList1.DataBind();

and the second question I do not really understand but this is what i think.

private string GetIndex(string descr)
    {
        int i = 0;
        foreach (ListItem li in CheckBoxList1.Items)
        {
            if (li.Text == descr)
            {
                return i.ToString();
            }
            i++;
        }
        return "";
    }

In order to get the value call the function like this String CheckIndex = GetIndex("Shows"); should return 2
Regards

Thanks for your reply - i checked first one and it seems checkedlistbox does nt have DataSource or DataBind() as members!
Second - I have nt given try yet - but seems to be working!

Thanks again,

Jeet

Uhmm, Are we talking about the same control?
I think I misunderstood the question, you guys are talking about CheckedListBox and I am talking about CheckBoxList jeje, well anyway if there are different controls my post have no sense.

Thanks. hope you solve your issue soon.

He, jbisono, don't get alarmed.
CheckBoxList lives in the System.Web.UI.WebControls namespace.
CheckedListBox lives in the System.Windows.Forms namespace.
Had the same trouble once with the Timer class : there are a least two Timer classes in different namespaces.

He, jbisono, don't get alarmed.
CheckBoxList lives in the System.Web.UI.WebControls namespace.
CheckedListBox lives in the System.Windows.Forms namespace.
Had the same trouble once with the Timer class : there are a least two Timer classes in different namespaces.

-- Yes I see....thanks all..but I am still not sure how to do with the checkedlistbox of windows application in C#?

regards

Jeet

Did you read #3 in this thread?

I read it - but please forgive . I do not understand - how can it help me solve my problems?

regards

Jeet

Well, jbisono pointed out in #2 how you can split a comma separated list of strings. Now a checkedListBox has an Items collection. This collection has an Add method.
So if you code checkedListBox.Items.Add("my string");
You will see my string as the first item in your checkedListBox if you run your program. If you still can not understand this, please explain what it is you don't understand.

Hi friends,

it was as simple as it looks!

arrFieldList = new ArrayList(strAllFields.Split(new char[] { ',' }));

        for (int i = 0; i < arrFieldList.Count; i++)
        {
            checkedlistbox1.Items.Add(arrFieldList[i].ToString());
        } 

regards
Jeet

Edited 3 Years Ago by Reverend Jim: Fixed formatting

[Hi friends,

it was as simple as it looks!

arrFieldList = new ArrayList(strAllFields.Split(new char[] { ',' }));

for (int i = 0; i < arrFieldList.Count; i++)
{
checkedlistbox1.Items.Add(arrFieldList.ToString());
}

regards
Jeet

This article has been dead for over six months. Start a new discussion instead.