Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Problem with a List Displaying in a Textbox

Tags:

c#

list

textbox

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)
    {

    }
like image 654
2boolORNOT2bool Avatar asked Jan 17 '26 20:01

2boolORNOT2bool


1 Answers

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)
like image 89
Samich Avatar answered Jan 20 '26 11:01

Samich