C# 在文本框中显示列表的问题
我有一个小型应用程序,有几个复选框、一个提交按钮和一个文本框。我希望用户能够检查他们想要的内容,单击提交按钮,并将结果显示在文本框中。应用程序运行,但不是显示值,而是在文本框中显示“System.Collections.Generic.List`1[System.String]”。我对此很陌生,非常感谢任何帮助。我的代码如下...
namespace MY_App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> ls = new List<string>();
private void Checkbox1_CheckedChanged(object sender, EventArgs e)
{
ls.Add( "P.C. ");
}
private void Checkbox2_CheckedChanged(object sender, EventArgs e)
{
ls.Add( "WYSE Terminal" );
}
private void Checkbox3_CheckedChanged(object sender, EventArgs e)
{
ls.Add("Dual Monitors ");
}
private void button1_Click(object sender, EventArgs e)
{
string line = string.Join(",", ls.ToString());
textBoxTEST.Text = line;
}
private void textBoxTEST_TextChanged(object sender, EventArgs e)
{
}
I have a small application that has several checkboxes, a submit button, and a textbox. I want the user to be able to check what they want, click the submit button, and have the results display in the textbox. The application runs but instead of the values displaying I get "System.Collections.Generic.List`1[System.String]" displayed in the textbox. I am very new to this and would appreciate any help. My code is as follows...
namespace MY_App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> ls = new List<string>();
private void Checkbox1_CheckedChanged(object sender, EventArgs e)
{
ls.Add( "P.C. ");
}
private void Checkbox2_CheckedChanged(object sender, EventArgs e)
{
ls.Add( "WYSE Terminal" );
}
private void Checkbox3_CheckedChanged(object sender, EventArgs e)
{
ls.Add("Dual Monitors ");
}
private void button1_Click(object sender, EventArgs e)
{
string line = string.Join(",", ls.ToString());
textBoxTEST.Text = line;
}
private void textBoxTEST_TextChanged(object sender, EventArgs e)
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在尝试连接对象名称,因为
ls.ToString()
将准确返回TextBox
中的内容。相反,请使用以下命令:
另外,这里是 Linq 解决方案:
You are trying to concat the object name, because of
ls.ToString()
will returns exactly what you've got in yourTextBox
.Instead, use the following:
Also, here is Linq solution:
ls.ToString()
调用Object.ToString
,即 List 类并不假定知道您希望如何打印其内部值。如果要创建显示字符串,则需要将字符串数组传递给String.Join
,而不是List.ToString()
的输出。ls.ToString()
callsObject.ToString
, i.e., the List class does not presume to know how you would like to print out its internal values. If you want to create a display string you will need to pass in an array of strings toString.Join
, not the output ofList<T>.ToString()
.您遇到的第一个问题是您的 CheckChanged 事件。他们可能只应在两种情况下添加到列表中。第一个是 CheckBox.IsChecked == true 以及它们是否尚未存在于列表中。
您不应该将 string.Join 与 ls.ToString() 一起使用。使用ToArray。
处理 SelectionChanged:
The first problem you have is with your CheckChanged events. They should probably only add to the List under two conditions. The first is the CheckBox.IsChecked == true and if they don't already exist in the list.
You shouldn't use string.Join with ls.ToString(). Use ToArray.
to handle the SelectionChanged:
您必须重写按钮单击事件:
您甚至可以更改此事件,以便在最后一项之后您不会得到“,”,但我认为您可以解决这个问题。 :)
You would have to rewrite your button click event:
You could even change this so that after your last item you don't get the " , " but i think you can solve that. :)