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)
}
}
}
}