简体   繁体   中英

How to return items from a list according to their respective checked box in checkedlistbox C#?

I have made progress on my first C# app from another answer but I still cannot wrap my head around the next part.

I have a JSON file with an array that contains my data. My app takes info from a JSON file containing an array and populates my checkedlistbox1 with the "Name" of each Finding. When you click on any item in the checkedlistbox1 (not check), it shows that particular Finding's info (name,risk,description,recommendation defined as "CompleteFinding") in the adjacent richtextbox1. All that is great.

What I want to do now is grab whichever items' CompleteFinding that are checked in my checkedlistbox1 and do what I want with it ie a variable or something to be referenced in a textbox or outputted elsewhere later when Button1 is clicked etc. I tried using "checkedlistbox1.SelectedItems" and am getting an error about converting to my Findings type. I also tried using a foreach loop and it only returns the last item that is checked. I need each checked items' CompleteFinding to use when Button1 is clicked.

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"
 }
]

Code

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 string CompleteFinding
  {
   get
   {
    return "Name:" + "\n" + Name + "\n" + "\n" + "Risk:" + "\n" + Risk + "\n" + "\n" + "Details:" + "\n" + Details + "\n" + "\n" + "Recommendation:" + Recommendation + "\n";
   }
  }
}
 
 public Form1()
 {
  InitializeComponent();

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

 private void Button1_Click(object sender, System.EventArgs e)
 {
  //would like to be able to use the CompleteFinding of each checkeditem here.
 }

 private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
  //This populates a single Finding's CompleteFinding to the richtextbox.
  richTextBox1.Text = ((Findings)checkedListBox1.SelectedItem).CompleteFinding;
 }
}

Something like this, perhaps?

private void Button1_Click(object sender, System.EventArgs e) {
    richTextBox1.Text = null; // clear out your results first
    var sb = new StringBuilder();
    foreach (var item in checkedListBox1.SelectedItems) {
        var selected = (Findings)item;
        sb.AppendLine(selected.CompleteFinding);
        sb.AppendLine(); // adds a blank line
    }
    richTextBox1.Text = sb.ToString();
}

From your comment below, it sounds like your ListView is written so that it uses physical checkboxes. That is something you probably should have mentioned in your original question.

To write the code so that it only looks at ListViewItems that have check marks on them, use something like this:

private void Button1_Click(object sender, System.EventArgs e) {
    richTextBox1.Text = null; // clear out your results first
    var sb = new StringBuilder();
    foreach (ListViewItem item in checkedListBox1.Items) {
        if (item.Checked) {
            var find = item.Tag as Finding;
            if (find != null) {
                sb.AppendLine(find.CompleteFinding);
                sb.AppendLine(); // adds a blank line
            }
        }
    }
    richTextBox1.Text = sb.ToString();
}

I do not know how JSON attaches to individual ListViewItems . In the code above, I have shown it pulling the data from a Tag field, but it would only be there if you wrote it there. There are lots of ways to write code.

I have been assuming that this is a basic Windows Forms application. The ListView control for Windows Forms is different from the one available for Web Applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM