dev-john
dev-john

Reputation: 129

Array in a textbox C#

I'm new in C#, and I'm trying to show an array in a textbox using window forms.

The problem is that when I give the command txtTela.Text = tela.ToString();, the program compiles successfully, but the result in the textbox is "System.String[]", and not the string that I'd like to show.

Image of what is printed in the textbox: https://snag.gy/L34bfM.jpg

    public String[] comboPalavra;
    public String []tela = new String[1];

    public Form1()
    {

        InitializeComponent();
        comboPalavra = embaralhaPalavra.CarregaPalavra();//Recebe uma palavra e uma dica


        //MessageBox.Show(comboPalavra[0]);

        foreach(char element in comboPalavra[0])
        {
            this.tela[0] = tela + "#";
        }

        txtTela.Text = tela.ToString();
        txtDica.Text = comboPalavra[1].ToString();
    }

Upvotes: 0

Views: 191

Answers (3)

Krzysztof Lach
Krzysztof Lach

Reputation: 1380

You need to convert your string array into single string. You can do this by string.Join().

textBox.Text = string.Join(separator, stringArray);

or

 textBox.Text = string.Join(separator, stringArray.Select(x => x.ToString()));

Upvotes: 2

user8651894
user8651894

Reputation:

You defined 'tela' as an array of String and applied the .ToString()-method directly to that array-object, which is why it ended in: System.String[]

public String []tela = new String[1];
txtTela.Text = tela.ToString();

To print a specific element, you need to define which element you want to print:

txtTela.Text = tela[0];

Upvotes: 0

Esperento57
Esperento57

Reputation: 17462

Or with linq expression (using System.Linq):

textBox.Text =stringArray.Aggregate((x, y) => x + separator + y);

Upvotes: 1

Related Questions