0

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();
    }

3 Answers 3

2

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()));
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks!! But, I've tried this, as I saw in this post stackoverflow.com/questions/15659409/…, but it didn't work...
@dev-john Show me your string.Join code that doesn't work.
txtTela.Text = string.Join(separator, tela);
@dev-john Everything should work fine. Remember to define separator and I hope it is obvious that if you have empty array nothing will be display.
1

Or with linq expression (using System.Linq):

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

Comments

0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.