ayush sid

ayush sid

  • NA
  • 5
  • 578

Stuck with CheckedListBox

Aug 29 2018 6:21 AM
Hi. Can anyone please help me? Following code create array of checklistboxes after user enter a value. I also have a json file:
 
{
"length": 38,
"masking": [ 1, 2, 6, 10, 15],
"date": "2018-08-07 10:10:10"
}
 
I want to be able to map user selection of checklistboxes to "masking" in json file. Also, how to assign values to arr of checkboxes e.g. checkbox[0] = A, checkbox[2] = B etc. We don't know how many checkboxes will be needed.
 
If user selects checkbox with indices 1, 3, 4, 6 it should map to json file and display value of selected checkboxes.
 
Thanks in advance.
SSMMasker.cs
  1. internal class SSMManager  
  2. public SSMManager() { }  
  3. {  
  4. public int DataLength { getset; }  
  5. public int[] Masking { getset; }  
  6. public string Date { getset; }  
  7. public bool Save(string isFilename = null)  
  8. {  
  9. JObject ajsoncontent = new JObject(  
  10. new JProperty("Datalength", DataLength),  
  11. new JProperty("Masking", Masking),  
  12. new JProperty("Date", Date)  
  13. );  
  14. if (isFilename == null) isFilename = SSMPositionManager.MASK_FILE; // public static string MASK_FILE = "masking.json"; in SSMPositionManager.cs  
  15. File.WriteAllText(isFilename, ajsoncontent.ToString());  
  16. return true;  
  17. }  
  18. }  
Form1.cs
  1. public partial class Form1 : Form  
  2. {  
  3. SSMPositionManager mcssmmanager = new SSMPositionManager();  
  4. CheckedListBox acheckbox = new CheckedListBox();  
  5.   
  6. public Form1()  
  7. {  
  8. InitializeComponent();}  
  9. private void btnCreate_Click(object sender, EventArgs e)  
  10. {  
  11. flPanel.Controls.Clear();  
  12.   
  13. int count = Int32.Parse(tbNumber.Text); //entered text  
  14.   
  15. for (int i = 0; i < count; i++)  
  16. {  
  17. CheckedListBox acheckbox = new CheckedListBox();  
  18. string[] arr = {i + "th" };  
  19. acheckbox.Items.AddRange(arr);  
  20.   
  21. //acheckbox.Text = i + "th"; //initially the button is selected to be this val  
  22. acheckbox.Size = new Size(60, 40);  
  23. acheckbox.Padding = new Padding(10, 10, 10, 10);  
  24. //acheckbox.FlatStyle = System.Windows.Forms.FlatStyle.Popup;  
  25. flPanel.Controls.Add(acheckbox);  
  26. }  
  27. }  
  28. private void btnSave_Click(object sender, EventArgs e)  
  29. {  
  30. SSMManager aMasker = new SSMManager();  
  31. SSMPositionManager mcssmmanager = new SSMPositionManager();  
  32. aMasker.DataLength = 40;  
  33. aMasker.Masking = new int[] { 1, 4, 5, 6, 7, 8, 9, 10 };  
  34. aMasker.Date = "2018-08-07 10:1mon0:10";  
  35. listBoxIndex.Items.Clear();  
  36. //gives index of the selected box  
  37. //checkedlistbox to a string array (items which has been checked in checkedlistbox to store in a string array)  
  38. ////if (acheckbox.Items.Count > 0)  
  39. String[] itemArr = new string[acheckbox.CheckedItems.Count];  
  40. Int32 counter = 0;  
  41. foreach (object item in this.acheckbox.CheckedItems)  
  42. {  
  43. String temp = Convert.ToString(item);  
  44. itemArr[counter] = temp;  
  45. counter++;  
  46. }  
  47. }  
  48. }