1

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!

Munsy
  • 28
  • 5
  • what's the point of the `builderlist`? You can just as easily append to the string builder without the extra int array. Having said that, did you try with a simple string to see that it works as you expect ? – Noctis Sep 07 '14 at 07:58
  • what is the error? Is there any maxsize for the textbox? – ray Sep 07 '14 at 08:03
  • So you mean your form doesn't appear when you set `textBox1.Text = builder.ToString();`? – Sriram Sakthivel Sep 07 '14 at 08:05
  • 2
    Since the iteration is 10000 times, the string generated is large and is not getting displayed in a single line textbox. @Sajeetharan's answer will work for sure – SomeUser Sep 07 '14 at 08:07
  • Noctis - this is part of an assignment for one of my programming classes, but I'm just testing things in small chunks. This is my first assignment involving WinForms, but the point of the int array has to do with other parts of the assignment that I didn't bother including here (it's basically an assignment where you sort the unique numbers in the list in several different ways, just as an fyi) because I didn't think it was really relevant to my issue. – Munsy Sep 07 '14 at 08:17
  • ray - There is no error, the 'dock' property is set to fill, if that's what you're asking, which to my understanding just means that if you expand the window in any direction, the textbox will expand along with it. I think that's right? Sriram - Yes, exactly! – Munsy Sep 07 '14 at 08:18
  • possible duplicate of [Textbox disabled with a huge string](http://stackoverflow.com/questions/10821504/textbox-disabled-with-a-huge-string) – Luke Woodward Sep 07 '14 at 08:29
  • Can't reproduce. The above code works fine and the textbox comes up filled immediately, provided it is set to multiline. – TaW Sep 07 '14 at 09:05

2 Answers2

1

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

enter image description here

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • I'm not the downvoter. But this is not an answer. OP's problem is not about multiple lines, it is about window doesn't appear. – Sriram Sakthivel Sep 07 '14 at 08:07
  • Not sure why it was downvoted, but thank you for the quick response. I already had multiline set to "True" in the properties menu for the textbox... does doing that accomplish the same thing? – Munsy Sep 07 '14 at 08:09
  • I quickly opened a forms proj and copy, pasted the code above. The form appears with an empty text box. So I think OP precisely asked for this. – SomeUser Sep 07 '14 at 08:09
  • I'm not either.When I first saw this i thought it was wrong. I had to run it to believe it. – George Vovos Sep 07 '14 at 08:09
  • @Munsy, yes. They are the same. If its working, pls accept this answer – SomeUser Sep 07 '14 at 08:11
  • @Partha - Multiline was already set to 'true' before I had this issue. I went back and double-checked to make sure it still was set to 'true,' but unfortunately it is and the window still won't appear. Maybe I should try to just compile the code in a new project? – Munsy Sep 07 '14 at 08:21
  • Can you change iteration values to 10 and let us know what happens ? – SomeUser Sep 07 '14 at 08:24
  • @Partha Yup, I just did that and it actually printed some numbers! So now I'm guessing that this has something to do with either the way the textbox displays things when the contents are too big to fit in the window maybe? I really appreciate the help btw... you guys are awesome :D – Munsy Sep 07 '14 at 08:28
  • Really quick update... I just tested it with the iterator set to go up to 100 and was able to get those values to display as well... when I set the iterator to 1000 it didn't work. So this seems to be some sort of issue where it won't work if the string won't fit inside the box... – Munsy Sep 07 '14 at 08:32
  • But it worked for me even for 10,000. Put that inside a try-catch block and display exception message in a message box jus to be sure of the exception – SomeUser Sep 07 '14 at 08:35
  • @Partha I'm new to exceptions too but I think I added the code in correctly... it never ended up displaying a message box though. I could try to paste the code for you to see if I actually wrote that code correctly but I'm not sure if it'll work very well with the comment formatting on here. :-/ – Munsy Sep 07 '14 at 08:46
  • Jus 1 last thing, you said form never appears. How long you waited and what you did after that ? – SomeUser Sep 07 '14 at 08:49
  • @Partha - I waited about 5 minutes before stopping on the first try. After it didn't work with the StringBuilder, I tried using a regular string, such as textBox1.Text = "Hey, lol"; and found that that worked without an issue. I think even though I haven't been able to figure out why it stops rendering after a certain value for the iterator, it's not going to be an issue for the overall functionality of the assignment. Originally I just wanted to see if the list of numbers was actually generating but then I ran into this issue and was just curious about it so I thought I'd post here. ^^ – Munsy Sep 07 '14 at 09:00
1

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

Community
  • 1
  • 1
nevets
  • 4,631
  • 24
  • 40