简体   繁体   中英

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

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

或使用linq表达式(使用System.Linq):

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM