Assigning Items from JSON Array to CheckedListBox C# and Referencing Them

Viewed 113

I am writing my first C# Windows app and I have looked at several places to try to piece together what I am trying to do but I just cant wrap my head around it.

I have a JSON file with an array that contains my data. I have figured out how to populate my Checkedlistbox1 with the "Name" field for each object in my array. What I want to do now is display the rest of the fields (name, risk, details, recommendation) of whichever item I have selected (highlighted, not checked if possible) in a richtextbox1. The goal is to be able to read each selected object's information before actually checking them. Later on, I will want to take whichever item(s) that are checked in the checkedbox details for use elsewhere in the app.

JSON File Example Content:

[
 {
   "Name": "Test Name 1",
   "Risk": "Low",
   "Details": "Detailed description",
   "Recommendation": "Recommended action"
 },
 {
   "Name": "Test Name 2",
   "Risk": "Low",
   "Details": "Detailed description",
   "Recommendation": "Recommended action"
 }
]

Here is the pertinent part of what I have so far. I tried to put a foreach loop for each checkedlistbox1.SelectedItems inside of the foreach loop below, but it doesnt put anything in the richtextbox1. Other attempts gave me the complete details for every finding in the array. Trying to get details for just the ones that are selected, and later, checked. Thank you!

public partial class Form1 : Form
{
 public class Findings
 {
  [JsonProperty("Name")]
  public string Name { get; set; }
 
  [JsonProperty("Risk")]
  public string Risk { get; set; }

  [JsonProperty("Details")]
  public string Details { get; set; }

  [JsonProperty("Recommendation")]
  public string Recommendation { get; set; }
 }
 
 public class FindingsList
 {
  [JsonProperty("Findings")]
  public IList<Findings> Findings { get; set; }
 }

 public Form1()
 {
  InitializeComponent();

  var json = JsonConvert.DeserializeObject<List<Findings>>(File.ReadAllText(@"findings-array.json"));

  foreach (var item in json)
  {
   string Name = item.Name
   string Risk = item.Risk
   string Details = item.Details
   string Recommendation = item.Recommendation
   string Complete_Finding = "Name:" + "\n" + Name + "\n" + "\n" + "Risk:" + "\n" + Risk + "\n" + "\n" + "Details:" + "\n" + Details + "\n" + "\n" + "Recommendation:" + Recommendation + "\n"; 

   checkedListBox1.Items.Add(item.Name)
   
   foreach (var checkedItem in checkedListBox1.SelectedItems)
   {
    richTextBox1.AppendText(Complete_Finding)
   }
  }
 }
}
1 Answers

First, you can get rid of the FindingsList class, it is unnecessary. You can also get rid of the foreach loop. I would recommend you populate your CheckedListBox using the DataSource property rather than adding items. You can do this like so -

checkedListBox1.DataSource = json;
checkedListBox1.DisplayMember = "Name";

The DisplayMember property determines which property in the Findings object is displayed to the user in the list.

You can achieve what you are trying to do with the RichTextBox using the SelectedIndexChanged Event Handler on the CheckedListBox. You do this one of two ways -

  • In the Forms Designer by selecting the CheckedListBox and clicking the lightening bolt icon in the Properties list to view events, scrolling down to SelectedIndexChanged and double clicking in the empty space next to this (will automatically generate a method in Form1.cs)

  • By adding checkedListBox1.SelectedIndexChanged += new EventHandler(MethodName); to your constructor (public Form1()) and then creating a new method that accepts the parameters required - private void MethodName(object sender, EventArgs e)

The method created will fire every time the selected (highlighted) item is changed in the CheckedListBox. You can then update your RichTextBox using richTextBox1.Text = ((Findings)checkedListBox1.SelectedItem).CompleteFinding;

Finally, to get all this to work, add the following to your Findings class -

public string CompleteFinding
{
   get
   {
      return "Name:" + "\n" + Name + "\n" + "\n" + "Risk:" + "\n" + Risk + "\n" + "\n" + "Details:" + "\n" + Details + "\n" + "\n" + "Recommendation:" + Recommendation + "\n";
   }
}
Related