- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个项目,我将文本插入到 TextBox
中,所述文本成为 checkeListBox
中的 Item
。到现在为止一切都很好。通过按下按钮,我希望 checkedlistBox
中的所有 checked Items
都显示在位于另一种形式的 RichTextBox
中。我已经弄清楚了第二种形式的命令......我似乎无法将 checkedItems
发送到 RichTextBox
。
附言: 我有几个 CheckedListBoxes
如果有任何相关性,需要将其发送到 RichTextBox
。
更新:
我用过
List<string> Items = checkedListBox1.CheckedItems.Cast<string>().ToList();`
将 checkedListBox1.Items
转换为字符串。但是,如果我试着写
fc.richTextBox1.Text = Items;
(fc 是我的 Form1)我收到一条错误消息
can not convert type "...." to "string"`.
如何解决这个错误?添加变量或其他内容?
第二次更新:
问题差不多解决了。我之前使用的代码不正确,因为我无法将字符串形式的 richTextBox.Text
转换为 .因此,我使用了答案中建议的另一行代码。这是我收到的错误:
再次感谢大家!一旦找到解决方案,我会立即更新!
其他问题的解决方案无效:
关于这个的事情是它部分起作用了。我在问题的开头显示了它。
尽管这与我的代码并不严格相关,但我尝试将其转换为可以正常工作。我达到了它部分工作的程度,但我得到了屏幕截图中显示的相同错误。
This Question is from another website
当我开始这个项目时,我也尝试使用 for
。但问题是它遍历了整个列表。我有 4 个单独的 checkedListBoxes
,而不是包含所有内容的单个。此外,每个盒子都有 10 个条目的限制,而第 4 个盒子可以容纳无限数量。
我也从中找到了几个解决方案,但与我的需求没有任何关系。
private void button1_Click(object sender, EventArgs e)
{
i++;
if (i <= 10)
checkedListBox1.Items.Insert(0, textBox1.Text);
if (i >= 11 && i <= 20)
{
if (checkedListBox2.Items == checkedListBox1.Items)
{
checkedListBox2.Items.Clear();
}
checkedListBox2.Items.Insert(0, textBox1.Text);
}
if (i >= 21 && i <= 30)
checkedListBox3.Items.Insert(0, textBox1.Text);
if (i >= 31)
checkedListBox4.Items.Insert(0, textBox1.Text);
textBox1.Text = "";
}
private void button3_Click(object sender, EventArgs e)
{
checkedListBox1.Items.Clear();
checkedListBox2.Items.Clear();
checkedListBox3.Items.Clear();
checkedListBox4.Items.Clear();
}
private void button5_Click(object sender, EventArgs e)
{
f2.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
f3.ShowDialog();
/*StringBuilder sb = new StringBuilder();
foreach (string S in checkedListBox1.CheckedItems)
{
sb.Append(S + Environment.NewLine);
}
fc.richTextBox1.Text = sb.ToString();
*/
/*
StringBuilder sb = new StringBuilder();
foreach (object checkedItem in checkedListBox1.CheckedItems)
{
{
sb.Append(checkedItem.ToString() + Environment.NewLine);
}
fc.richTextBox1.Text = sb.ToString();
}
*/
fc.richTextBox1.Text = String.Join("\n", checkedListBox1.CheckedItems);
}
private void button6_Click(object sender, EventArgs e)
{
Ab1.ShowDialog();
}
}
Form2 f2 = new Form2();
Form3 fc = new Form3();
问题的最终更新和回答
标记答案是我阅读的所有答案中对这个问题的正确答案。出现错误是因为我在代码中使用了不同的 form
作为引用,而不是包含 Richtextbox
的那个。我没有早点注意到是我的错误,但至少我了解了导致这些 NullException
错误的原因。对造成的困扰深表歉意!
谢谢大家的帮助和时间,这个项目真的对我有帮助,但我无法完成它。
最佳答案
Text
RichTextBox
的属性是 string
.正如您在错误消息中看到的那样,它要求只是一个字符串 而不是 List<string>
.您需要遍历列表并将所有行添加到文本框。我会说最简单的方法是使用 StringBuilder
:
StringBuilder sb = new StringBuilder();
foreach(string S in Items)
{
sb.Append(S + Environment.NewLine);
}
fc.richTextBox1.Text = sb.ToString();
正如@Spacemancraig 在评论中建议的那样,您也可以直接从项目中执行此操作:
StringBuilder sb = new StringBuilder();
foreach (object checkedItem in checkedListBox1.CheckedItems) {
{
sb.Append(checkedItem.ToString() + Environment.NewLine);
}
fc.richTextBox1.Text = sb.ToString();
关于c# - 发送 checkedListBox.CheckedItems 到 richTextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27929997/
我编写了以下类来创建使用自然排序算法的 CheckedListbox。被覆盖的 Sort()然而,方法并没有被解雇。 (该方法中的代码来自 MSDN 示例,关于如何在常规列表框上实现您自己的排序算法。
我在创建 CheckedListBox 的“二级”时遇到了一些问题。 示例: 这是我的 CheckedListBox: 代码: checkedListBox_materiałyDoRozliczen
我有: ListenCheckedListBox.SetItemCheckState(0, CheckState.Unchecked); 这个赋值会触发 ListBox 的 ItemCheck 事件吗
我有一个包含两个元素的表单:一个CheckedListBox 和一个CheckBox。 CheckBox 称为SelectAllCheckBox,用于选中/取消选中CheckedListBox 中的所
我需要改变我的 CheckedListBox 中项目的颜色,但“alternatingColors”不是 CheckedListBox 的属性。 如何让项目的颜色交替? 最佳答案 默认情况下,OnDr
我有一个使用此代码获取项目的 CheckedListBox。 public static void GetDisplayValueList(object clk, string[] kv, strin
我想在一个应用程序中使用 CheckedListBox,其中 ListBox 中的每个项目都是我硬盘上一个文件夹的名称,并且为了在这些文件夹中的每个文件夹中读取和写入文本文件,我想确保在 Checke
我在一个表单上有多个 checkedListBox。对于每个 checkedListBox 我都有一个按钮来“选择所有”项目: private void btnSelectAll1_Click(obj
我需要覆盖基本的 CheckedListBox 行为。 可以在不附加任何代码的情况下选中和取消选中 CheckedListBox。 我需要禁用此行为,以便我可以实现自定义代码。 有什么想法吗? 谢谢。
在 .NET 领域,有非常有用的 System.Windows.Forms.CheckedListBox类。 Windows Common Controls 中的等价物是什么? (如果有的话) 最佳答
在我的 CheckedListBox 应用程序中,我只想允许检查一个项目。 我已经设置了这些属性 checkOnClick = true; SelectionMode = One; 任何建议将不胜感激
当用户的鼠标悬停在 CheckedListBox 中的项目上时,是否有一种直接的方法来设置在工具提示中显示额外的文本? 我期望能够在代码中做的是: uiChkLstTables.DisplayOnHo
我有一个名为 Subjects 的集合,其属性为字符串中的名称和整数中的 SubjectCode。我将它传递给一个选中的列表框。按下按钮,我想获取用户检查的所有项目的集合,将其转换为集合主题。 请任何
我在 Windows 窗体中有一个 CheckedListBox 控件。 我想知道向谁添加多列。MultiColumn 属性设置为 true。我只想知道添加 2 列的编码。 我的代码是 while (
在 .NET Framework 中,是否可以将 CheckedListBox 中的某些项目设置为“不可选中”?我不想让用户再次检查相同的项目并将它们添加到另一个现有列表中。 我希望我是清楚的。提前致
我为 Windows 窗体应用程序创建了一个用户控件。它只有一个带有四个单元格的 TableLayoutPanel。 Cell[0,0] 和 cell[0,1] 有标签。 Cell[1,0] 有一个
我想根据传递给表单构造函数的项目填充 CheckedListBox(在本例中,列表 )。 我的框架代码是: foreach (int platypus in listPlatypi) { us
我有一个像这样的CheckedListBox: :'''''''''''''''/\ : [ ] item1 || : [x] item2 || : [ ] item3 ##
是否可以像在复选框中一样将 .Checked== 应用于 checkedlistbox? 如果以复选框的方式来做它是行不通的 if(checkedListBox1.Items[2].Checked==
我有一个绑定(bind)到 DataTable 的 CheckedListBox。现在我需要以编程方式检查一些项目,但我发现 SetItemChecked(...) 方法只接受项目索引。 有没有一种实
我是一名优秀的程序员,十分优秀!