Solved

Checking Items in CheckListBox

Posted on 2004-10-14
7
2,540 Views
Last Modified: 2012-06-27
Greetings,

Here's the issue. I first have users check certain items in the checklistBox, and I save thier selection in an arraylist. I store all the items in the checklistBox which should also store whichs one are checked. The problem comes when i want to repopulate the same checkListBox later from the items stored in the arraylist.

Here's the code to populate the ChecklistBox...
StreamReader myXmlStream = new StreamReader(myFileStream);
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(myXmlStream);
                  
chBook.DataSource    = myDataSet.Tables[4];
chBook.DisplayMember = myDataSet.Tables[4].Columns[0].ToString();
chBook.ValueMember   = myDataSet.Tables[4].Columns[1].ToString();

Here's how i store the information...
for(int i = 0; i < this.chBook.Items.Count; i++){
ls.books.Add(chBook.Items[i]);}

Now how do i repopulate the CheckListBox from the arraylist? I've tried so many things I won't even post them here.

Thanks,


0
Comment
Question by:vargasbo
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:aacool
ID: 12315025
You have a few declarations missing, but assuming,

ArrayList lsBooks;
CheckedListBox chBook
private void setCBValues()
{
      chBook.Items.Clear();
        // Populate checkedListBox with items from array
      chBook.Items.AddRange(lsBooks.ToArray());
}

I have a fully working example to send you if you need it - please mail me at aacool - hotmail.com if needed
0
 
LVL 3

Assisted Solution

by:aacool
aacool earned 200 total points
ID: 12315173
Of course, if you want to also store and retrieve the list items check state, make a class like
class CheckedItem
{
   string text;
   bool itemState;
}

and add objects of this class when storing the objects in the ArrayList. When getting them back, create items one by one and set the text as the item Text and the checked state as the CheckState property for the Item.

if(chItem.ItemState==true)
    chBook.Items.Add(chItem.text,CheckState.Checked);
else
    chBook.Items.Add(chItem.text,CheckState.Unchecked);

0
 
LVL 1

Author Comment

by:vargasbo
ID: 12315357
aacool

What i did try your following suggestion, but it didn't work. What shows up in the CheckedListBox is System.DataView..

DataRowView []oCheckedItems= new DataRowView[ls.books.Count];
ls.books.CopyTo(oCheckedItems,0);
chBook.DataSource = null;  //Have to remove Datasource otherwise can't edit items            
chBook.Items.AddRange(ls.books.ToArray());

Why can't i just save the items in the list with it's checked status? This is the code I'm using which shows the item name, but it doesn't show it's checked state.

for(int x = 0; x < oCheckedItems.Length; x++) {
   chGrade.Items.Add(oCheckedItems[x].Row[0]);}
                  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Assisted Solution

by:aacool
aacool earned 200 total points
ID: 12320835
I see why - the way you are adding your items to the array is you are adding the Objects from the chBook.Items.
The only important elements from the checkedListBox.Items are the Item Text and Checked State. Store the text from chBook.Items.Item[i].Text and chBook.Items.Item[i].CheckState in an object and put that in your array. Then, use the code to add items to the chBook

Basically, Do you need to use DataRowView[] as your array? Use something simple like an array of the class
class CheckedItem
{
   string text;
   bool itemState;
}
chItemsArray[] CheckedItem;
Then when adding entries to the chBook, do this:

for(int x=0;x < checkedItemsArray;x++)
{
if(chItemsArray[x].ItemState==true)
    chGrade.Items.Add(chItemsArray[x].text,CheckState.Checked);
else
    chGrade.Items.Add(chItemsArray[x].text,CheckState.Unchecked);
}
0
 
LVL 3

Accepted Solution

by:
aacool earned 200 total points
ID: 12320898
Going back to your original code,
rather than storing the items as

for(int i = 0; i < this.chBook.Items.Count; i++){
                      ls.books.Add(chBook.Items[i]);}

use

for(int i=0;i<chBook.Items.Count;i++)
{
     CheckedItem chItem = new CheckedItem();
     chItem.text = chBook.Items[i].Text;
     (chBook.Items[i].CheckState=CheckState.Checked) ? chItem.itemState=true : chItem.itemState = false;
     chItemsArray[i++] = chItem;
     chItem.Dispose();
}

to store the checkedlistbox items in the item array
0
 
LVL 1

Author Comment

by:vargasbo
ID: 12320996
for(int i=0;i<chBook.Items.Count;i++)
{
     CheckedItem chItem = new CheckedItem();
     chItem.text = chBook.Items[i].Text; <---- Can't do this
     (chBook.Items[i].CheckState=CheckState.Checked) ? chItem.itemState=true : chItem.itemState = false; <<< --- or this
     chItemsArray[i++] = chItem;
     chItem.Dispose();
}

Have to uses DataRowView b/c the CheckListBox is tied to a Datasource, That why i can't uses the chBook.Items[i].Text.
0
 
LVL 1

Author Comment

by:vargasbo
ID: 12321337
OK, I finally got the code to work and here's the code

When Items first saves....

for(int i = 0; i < this.chBook.Items.Count; i++){
                        
CheckedItems chItem = new CheckedItems();
chItem.text  = ((DataRowView)chBook.Items[i]).Row[0].ToString();
chItem.check = chBook.GetItemChecked(i);
ls.books.Add(chItem);
chItem = null;
}

When repopulating the form
chBook.DataSource = null;
for(int x = 0; x < ls.books.Count; x++) {
  if(((CheckedItems)ls.books[x]).check == true)
      chBook.Items.Add(((CheckedItems)ls.books[x]).text,true);
      else
      chBook.Items.Add(((CheckedItems)ls.books[x]).text,false);
}

Thanks for the help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Chat Room 1 43
bulid json format 3 45
How to write a select year(max(datefield) in EntitySQL in  c# entity framework 12 27
Entity Framework 7 28
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now