c# - 如何根据checkedlistbox C#中各自的复选框从列表中返回项目?
问题描述
我在另一个答案中的第一个 C# 应用程序上取得了进展,但我仍然无法理解下一部分。
我有一个 JSON 文件,其中包含一个包含我的数据的数组。我的应用程序从一个包含数组的 JSON 文件中获取信息,并使用每个结果的“名称”填充我的 checklistbox1。当您单击选中列表框1(未选中)中的任何项目时,它会在相邻的 Richtextbox1 中显示该特定结果的信息(名称、风险、描述、定义为“CompleteFinding”的建议)。这一切都很棒。
我现在要做的是抓取在我的checkedlistbox1中选中的任何项目的CompleteFinding,并用它做我想做的事情,即变量或要在文本框中引用的东西,或者稍后在单击Button1时输出到其他地方等。我尝试使用“ checkedlistbox1.SelectedItems" 并且收到关于转换为我的结果类型的错误。我还尝试使用 foreach 循环,它只返回检查的最后一项。单击 Button1 时,我需要使用每个选中项目的 CompleteFinding 。
JSON 文件示例内容:
[
{
"Name": "Test Name 1",
"Risk": "Low",
"Details": "Detailed description",
"Recommendation": "Recommended action"
},
{
"Name": "Test Name 2",
"Risk": "Low",
"Details": "Detailed description",
"Recommendation": "Recommended action"
}
]
代码
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;
}
}
解决方案
大概是这样的吧?
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();
}
从您在下面的评论中,听起来您的ListView是为了使用物理复选框而编写的。您可能应该在原始问题中提到这一点。
要编写代码使其仅查看带有复选标记的ListViewItems,请使用以下代码:
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();
}
我不知道 JSON 如何附加到单个ListViewItems。在上面的代码中,我已经展示了它从Tag字段中提取数据,但只有在你在那里编写它时才会出现。有很多方法可以编写代码。
我一直假设这是一个基本的 Windows 窗体应用程序。Windows 窗体的ListView控件不同于 Web 应用程序可用的控件。
推荐阅读
- r - 在 ggplot 区域外添加文本
- amazon-web-services - 使用亚马逊信任服务证书
- python - How to keep variable value if new value is None
- android-room - How to order items from Room database by date with "MMM dd yyyy" format
- javascript - ReactJS - How to have a string with multiple colors for each characters
- swift - didSelectRowAt:IndexPath 不起作用,并且在按下单元格时不会打印到控制台
- docusignapi - TSP 签名哈希会话信息
- java - Sorting a List of integers based on a List of strings in Java
- google-app-engine - 位桶存储库镜像到 Google Cloud Source Repositories
- sass - How to get value of grid-breakpoint variable