Featured List of CheckedListbox and ValueMember Members

I have a problem with my checked list box.

public void GetFolder() { var dict = new Dictionary<string, string>(); foreach (Folder folder in rootfolder.FindFolders(new FolderView(100))) { dict.Add(folder.Id.ToString(),folder.DisplayName); } checkedListBox1.DataSource = new BindingSource(dict, null); checkedListBox1.DisplayMember = "Value"; checkedListBox1.ValueMember = "Key"; } 

And now I want to get all the "Verified list" fields,

I do it with

  foreach (object item in checkedListBox1.CheckedItems) { lala = lala + item +"|"; } 

My CheckedListbox shows me CheckIcon and the name of all the folders that I read from Directory, and now I want to save the folder with the links. In some settings, but only with an identifier, but I always get the folder name and identifier.

Hope Someone can help me, maybe I have some tomatoes in my eyes :)

+4
source share
9 answers
 public void GetFolder() { var dict = new Dictionary<string, string>(); ArrayList arr = new ArrayList(); foreach (Folder folder in rootfolder.FindFolders(new FolderView(100))) { dict.Add(folder.Id.ToString(),folder.DisplayName); arr.Add(folder.Id.ToString()); } checkedListBox1.DataSource = new BindingSource(dict, null); checkedListBox1.DisplayMember = "Value"; checkedListBox1.ValueMember = "Key"; //Do whatever to arraylist.. } 

This will give you a list of arrays with all the IDs in it. You can link this to the source or run a foreach to get all the elements.

+1
source

I assume lala is a settings string.

If so, use this:

 lala = lala + item.Value.ToString() +"|"; 

So lala woud contains all identifiers like this: 1 | 2 | 34 | 567 | 5...

+1
source

OK, try the following:

 ArrayList folders=new ArrayList(); foreach (Folder folder in rootfolder.FindFolders(new FolderView(100))) { folders.Add(folder); } checkedListBox1.DataSource = folders; checkedListBox1.DisplayMember = "DisplayName"; checkedListBox1.ValueMember = "Id"; foreach (Folder item in checkedListBox1.CheckedItems) { lala += item.Id.ToString() +"|"; } 
+1
source

Maybe I have a solution.

I use a DataTable instead of a dictionary

  DataTable table = new DataTable(); table.Columns.Add("Name",typeof(string)); table.Columns.Add("Key", typeof(string)); table.Rows.Add("ID1=", "Folder1"); table.Rows.Add("ID2=", "Folder2"); table.Rows.Add("ID3=", "Folder3"); table.Rows.Add("ID4=", "Folder4"); table.Rows.Add("ID5=", "Folder5"); table.Rows.Add("ID6=", "Folder6"); 

and now un Click all the selected items that I received

foreach (DataRowView objDataRowView in checkedListBox1.CheckedItems) {MessageBox.Show ("My value:" + objDataRowView ["Name"]); }

only my ids back :)

+1
source

Try

 foreach (object item in checkedListBox1.CheckedItems) { DictionaryEntry de = (DictionaryEntry)item; string htKey = de.Key.ToString(); } 
0
source

In your foreach, create a var object instead of an object - after that you can access the Value property.

0
source

i testet with a new project. :) so if something goes wrong, no problem * g

private void button1_Click (object sender, EventArgs e) {

  var dict = new Dictionary<string, string>(); dict.Add("aQYCeWGlY9aKLAQANCS+7ujPFQpP5KZ/ydAp2AAAAEaCNAAA=", "Folder1"); dict.Add("HF§CeWGlY9aKLAQATKJZ63wo8TpT3xtDFVRdrAAAAANsVAAA=", "Folder2"); dict.Add("AAAAAACTXZc0AXhaQYCeWGlY9aKLAQH8KxFhrAAAARAi9AAA=", "Folder3"); dict.Add("AAAAAACTXZc0AXhaQYCeWGlY9aKLAQv+KxFhrAAAARAi+BBB=", "Folder4"); dict.Add("AlY9aKLAQC9TICb7ytCSbvH6E+KxFhrHd8883AAAARAjAAAA=", "Folder5"); dict.Add("AAMkADE5YmIwZDA3LTdjMzAtNDZlOS04MDc3LAAAAEaCNAAA=", "Folder6"); checkedListBox1.DataSource = new BindingSource(dict,null); checkedListBox1.DisplayMember = "Value"; checkedListBox1.ValueMember = "Key"; } 

When clicked, my checkedListBox1 shows me Folder1 Folder2 Folder3

how did i read all selected items with only keys? I want to keep the application restart set checked on the stored folder identifiers

private void button2_Click (object sender, EventArgs e) {int allDirCount = checkedListBox1.CheckedItems.Count;

  foreach (var item in checkedListBox1.CheckedItems) { } textBox3.Text = allDirCount.ToString(); } 

If I checked some folder, then klick Button2 I got all the selected items, but with the names of the folders :(

Some other ideas?

0
source
 Hi sanoth, Bind CheckedListBOx or any other control with displaymember and valuemember is quite simple you just need to specify datasource property of control as well as displaymember and valuemember. Following is working code 100 % work for me i have tested:P /* checkedlistbox bindig code */ DataSet ds = new DataSet(); string strChechboxlist = "select Subject_ID as code, SubjectName as Display from dbo.Mst_Subject_Detail"; /* filldataset() is function i have created to return dataset. */ ds = dc.FillDataSet(strChechboxlist); if (ds.Tables[0].Rows.Count > 0) { checkedListBox1.DataSource = ds.Tables[0]; checkedListBox1.DisplayMember = "Display"; checkedListBox1.ValueMember = "code"; } /* for fetching valuemember or displaymember from checkedlistbox */ for(int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { /*Now with the following code we can get valemember and displaymember as per your requirement you can store in table. */ DataRow r; r = ((DataRowView)this.checkedListBox1.CheckedItems[i]).Row; string val = (r[this.checkedListBox1.ValueMember]).ToString(); string dis = (r[this.checkedListBox1.DisplayMember]).ToString(); r = null; } Note :- I am attaching working demo of code 
0
source

// Here is a simple way [I'm new to coding].

// CheckedListBox can retrieve CheckedItems in the form of a DataRowView

// For CheckedListBox, make sure your data source also passes the value "ID".

// Set up the data source for CheckedListBox

  var dict = new Dictionary<string, string>(); foreach (Folder folder in rootfolder.FindFolders(new FolderView(100))) { dict.Add(folder.Id.ToString(),folder.DisplayName); } checkedListBox1.DataSource = new BindingSource(dict, null); checkedListBox1.DisplayMember = "Value"; //This will be the folders display name in the check box checkedListBox1.ValueMember = "Key"; // this will be the folder ID 

// create a list to store all selected identifiers

 List<string> ListOfCheckedIDs = new List<string>(); 

// now get all checked items in a CheckedListBox as a DataRowView and use a ValueMember that contains folder IDs to add folder IDs to ListOfCheckedIDs

 foreach (DataRowView drowVw in checkedListBox1.CheckedItems) { ListOfCheckedIDs.Add(drowVw[checkedListBox1.ValueMember].ToString()); } 

// Now use string.join in ListOfCheckedID so that there are no unwanted trailing characters

 string myStringOfIDs = string.Join("|", ListOfCheckedIDs); 
0
source

Source: https://habr.com/ru/post/1446226/


All Articles