StringBuilder Won't Show In TextBox (WinForms, C#)

牧云@^-^@ 提交于 2019-12-14 03:59:28

问题


I'm trying to get the contents of a StringBuilder to display in a WinForms textbox, but the window never appears when I try to compile the program. This is my first venture into WinForms and C# and I've only used the language for about a week and a half now, so this is probably a simple fix that I'm just not seeing.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        StringBuilder builder = new StringBuilder();
        Random rand = new Random();
        int[] builderList = new int[10000];
        for (int i = 0; i < 10000; i++)
        {
            builderList[i] = rand.Next(1, 20000);
            builder.Append(builderList[i].ToString() + " ");
        }

        // This is the line that seems to be the problem...
        textBox1.Text = builder.ToString(); 
    }
}

When I try to run the program and insert a breakpoint on that last line of code, I can see that the program seems to just hit that line continuously. Oddly enough, if I change that line to this:

textBox1.Text = "Hey, lol";

my program will run. I checked the debugger in Visual Studio and saw that the contents of 'builderList' are updated to random numbers and 'builder' looks like it's correctly storing the values in 'builderList' as a string like I want, so I'm kind of confused about what's going on here. I'd appreciate any help I can get on this one as it seems like it should be a relatively easy fix but I've been stumped on it so far and I haven't really found anything helpful in the MSDN documentation.

Thanks so much!


回答1:


Change your TextBox1 to be a MultiLine TextBox.Select "Allow multiLine"




回答2:


The real reason for this is about the pixel width, check my post and @TaW's answer here: The maximum number of characters a TextBox can display



来源:https://stackoverflow.com/questions/25708186/stringbuilder-wont-show-in-textbox-winforms-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!