C# 在文本框中显示列表的问题

发布于 2024-12-06 08:27:57 字数 975 浏览 0 评论 0原文

我有一个小型应用程序,有几个复选框、一个提交按钮和一个文本框。我希望用户能够检查他们想要的内容,单击提交按钮,并将结果显示在文本框中。应用程序运行,但不是显示值,而是在文本框中显示“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江心雾 2024-12-13 08:27:57

您正在尝试连接对象名称,因为 ls.ToString() 将准确返回 TextBox 中的内容。

相反,请使用以下命令:

string line = string.Join(",", ls.ToArray());
textBoxTEST.Text = line;

另外,这里是 Linq 解决方案:

ls.Aggregate((i, j) => i + ","+ j)

You are trying to concat the object name, because of ls.ToString() will returns exactly what you've got in your TextBox.

Instead, use the following:

string line = string.Join(",", ls.ToArray());
textBoxTEST.Text = line;

Also, here is Linq solution:

ls.Aggregate((i, j) => i + ","+ j)
把时间冻结 2024-12-13 08:27:57

ls.ToString() 调用 Object.ToString,即 List 类并不假定知道您希望如何打印其内部值。如果要创建显示字符串,则需要将字符串数组传递给 String.Join,而不是 List.ToString() 的输出。

string line = string.Join(",", ls.ToArray());
textBoxTEST.Text = line;

ls.ToString() calls Object.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 to String.Join, not the output of List<T>.ToString().

string line = string.Join(",", ls.ToArray());
textBoxTEST.Text = line;
や莫失莫忘 2024-12-13 08:27:57

您遇到的第一个问题是您的 CheckChanged 事件。他们可能只应在两种情况下添加到列表中。第一个是 CheckBox.IsChecked == true 以及它们是否尚未存在于列表中。

您不应该将 string.Join 与 ls.ToString() 一起使用。使用ToArray。

处理 SelectionChanged:

private void myCheckBox_SelectionChanged(object sender, SelectionChangedEventArgs e)  
{
    myTextBox.Text = myCheckBox.IsChecked ? myTextBox.Text = "The Value" : myTextBox.Text = string.Empty;   
}

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:

private void myCheckBox_SelectionChanged(object sender, SelectionChangedEventArgs e)  
{
    myTextBox.Text = myCheckBox.IsChecked ? myTextBox.Text = "The Value" : myTextBox.Text = string.Empty;   
}
罗罗贝儿 2024-12-13 08:27:57

您必须重写按钮单击事件:

var sb = new StringBuilder();
Foreach( var item in ls)
{
 sb.Append(String.Format("{0} , ",item));
}
textboxTEST.Text = sb.ToString();

您甚至可以更改此事件,以便在最后一项之后您不会得到“,”,但我认为您可以解决这个问题。 :)

You would have to rewrite your button click event:

var sb = new StringBuilder();
Foreach( var item in ls)
{
 sb.Append(String.Format("{0} , ",item));
}
textboxTEST.Text = sb.ToString();

You could even change this so that after your last item you don't get the " , " but i think you can solve that. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文