Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. setting large amount of text in textbox

setting large amount of text in textbox

Scheduled Pinned Locked Moved C#
cssperformancehelptutorialquestion
18 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    V 0
    wrote on last edited by
    #1

    Guys, A few days ago I asked about an assignment we had to do here at work. convert "aabb" to "aAbB" and set it to the textbox as quickly as possible. I think the conversion itself is very quick, reading in and converting in 1 or 2 seconds, but setting the entire thing in the texbox takes me another 10 seconds! (20 MB file) I've read about the AppendText property and tried to set my characterarray to the box inside my loop, but so far the fastest results I got where form txtbox_result.Text = new string(text); where text = char []. Any ideas on how to speed this up? (Suspend/resumelayout don't help either) thanks ! PS: here's tho code I got so far:

    		    starttime = DateTime.Now;
    		    reader = new System.IO.StreamReader(openfiledlg.FileName);
    		    char \[\] text = reader.ReadToEnd().ToCharArray();
    		    reader.Close();
    		    difference = DateTime.Now - starttime;
    		    lbl\_result.Text = "File read: " + difference.TotalMilliseconds + " milliseconds.";
    		    lbl\_result.Refresh();
    		    pb\_conversion.Maximum = text.Length;
    		    //Starting from 1 will gain me 1 character less to analyze.
    		    //don't update the progress bar every iteration.
    		    int i2 = 0;
    		    for(int i = 1; i < text.Length; i+=2){
    		        text\[i\] = char.ToUpper(text\[i\]);
    		        if(++i2 == 1000){
    		            i2 = 0;
    		            pb\_conversion.Value = i;
    		        }											//end if
    		    }												//end for
    		    difference = DateTime.Now - starttime;
    		    lbl\_result.Text = "conversion: " + difference.TotalMilliseconds + " milliseconds.";
    		    lbl\_result.Refresh();
    		    pb\_conversion.Value = pb\_conversion.Maximum;
    		    txtbox\_result.SuspendLayout();
    		    txtbox\_result.Text = new string(text);
    		    txtbox\_result.ResumeLayout();
    		    difference = DateTime.Now - starttime;
    		    lbl\_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds.";
    

    V. I found a living worth working for, but haven't found work worth living for.

    M A 3 Replies Last reply
    0
    • V V 0

      Guys, A few days ago I asked about an assignment we had to do here at work. convert "aabb" to "aAbB" and set it to the textbox as quickly as possible. I think the conversion itself is very quick, reading in and converting in 1 or 2 seconds, but setting the entire thing in the texbox takes me another 10 seconds! (20 MB file) I've read about the AppendText property and tried to set my characterarray to the box inside my loop, but so far the fastest results I got where form txtbox_result.Text = new string(text); where text = char []. Any ideas on how to speed this up? (Suspend/resumelayout don't help either) thanks ! PS: here's tho code I got so far:

      		    starttime = DateTime.Now;
      		    reader = new System.IO.StreamReader(openfiledlg.FileName);
      		    char \[\] text = reader.ReadToEnd().ToCharArray();
      		    reader.Close();
      		    difference = DateTime.Now - starttime;
      		    lbl\_result.Text = "File read: " + difference.TotalMilliseconds + " milliseconds.";
      		    lbl\_result.Refresh();
      		    pb\_conversion.Maximum = text.Length;
      		    //Starting from 1 will gain me 1 character less to analyze.
      		    //don't update the progress bar every iteration.
      		    int i2 = 0;
      		    for(int i = 1; i < text.Length; i+=2){
      		        text\[i\] = char.ToUpper(text\[i\]);
      		        if(++i2 == 1000){
      		            i2 = 0;
      		            pb\_conversion.Value = i;
      		        }											//end if
      		    }												//end for
      		    difference = DateTime.Now - starttime;
      		    lbl\_result.Text = "conversion: " + difference.TotalMilliseconds + " milliseconds.";
      		    lbl\_result.Refresh();
      		    pb\_conversion.Value = pb\_conversion.Maximum;
      		    txtbox\_result.SuspendLayout();
      		    txtbox\_result.Text = new string(text);
      		    txtbox\_result.ResumeLayout();
      		    difference = DateTime.Now - starttime;
      		    lbl\_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds.";
      

      V. I found a living worth working for, but haven't found work worth living for.

      M Offline
      M Offline
      Marek Grzenkowicz
      wrote on last edited by
      #2

      Do you really need all this text in a textbox? If you really do, I would consider loading a small portion of text first and then appending the rest in a separate thread.

      V 1 Reply Last reply
      0
      • M Marek Grzenkowicz

        Do you really need all this text in a textbox? If you really do, I would consider loading a small portion of text first and then appending the rest in a separate thread.

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        lol, it's a test at work and one of the requirements was, show conversion in a textbox. I don't think that this one will work because timing only counts when everything is done. Good idea though. tnx.

        V.
        Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

        J 1 Reply Last reply
        0
        • V V 0

          lol, it's a test at work and one of the requirements was, show conversion in a textbox. I don't think that this one will work because timing only counts when everything is done. Good idea though. tnx.

          V.
          Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

          J Offline
          J Offline
          jpg 0
          wrote on last edited by
          #4

          The textbox aren't really designed for holding 20MB of text. Try opening the file in notepad and see how much time it take. A RichTextBox might be a better choice in your case.

          V 1 Reply Last reply
          0
          • V V 0

            Guys, A few days ago I asked about an assignment we had to do here at work. convert "aabb" to "aAbB" and set it to the textbox as quickly as possible. I think the conversion itself is very quick, reading in and converting in 1 or 2 seconds, but setting the entire thing in the texbox takes me another 10 seconds! (20 MB file) I've read about the AppendText property and tried to set my characterarray to the box inside my loop, but so far the fastest results I got where form txtbox_result.Text = new string(text); where text = char []. Any ideas on how to speed this up? (Suspend/resumelayout don't help either) thanks ! PS: here's tho code I got so far:

            		    starttime = DateTime.Now;
            		    reader = new System.IO.StreamReader(openfiledlg.FileName);
            		    char \[\] text = reader.ReadToEnd().ToCharArray();
            		    reader.Close();
            		    difference = DateTime.Now - starttime;
            		    lbl\_result.Text = "File read: " + difference.TotalMilliseconds + " milliseconds.";
            		    lbl\_result.Refresh();
            		    pb\_conversion.Maximum = text.Length;
            		    //Starting from 1 will gain me 1 character less to analyze.
            		    //don't update the progress bar every iteration.
            		    int i2 = 0;
            		    for(int i = 1; i < text.Length; i+=2){
            		        text\[i\] = char.ToUpper(text\[i\]);
            		        if(++i2 == 1000){
            		            i2 = 0;
            		            pb\_conversion.Value = i;
            		        }											//end if
            		    }												//end for
            		    difference = DateTime.Now - starttime;
            		    lbl\_result.Text = "conversion: " + difference.TotalMilliseconds + " milliseconds.";
            		    lbl\_result.Refresh();
            		    pb\_conversion.Value = pb\_conversion.Maximum;
            		    txtbox\_result.SuspendLayout();
            		    txtbox\_result.Text = new string(text);
            		    txtbox\_result.ResumeLayout();
            		    difference = DateTime.Now - starttime;
            		    lbl\_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds.";
            

            V. I found a living worth working for, but haven't found work worth living for.

            A Offline
            A Offline
            althamda
            wrote on last edited by
            #5

            I'm not too sure about putting text into a textbox quicker, but if you definately know that every 2nd character is a letter, then using text[i] = (char)((int)text[i] - 32); is quicker than using text[i] = char.ToUpper(text[i]);

            V M L 3 Replies Last reply
            0
            • J jpg 0

              The textbox aren't really designed for holding 20MB of text. Try opening the file in notepad and see how much time it take. A RichTextBox might be a better choice in your case.

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              .jpg wrote:

              The textbox aren't really designed for holding 20MB of text

              He he, don't tell me :-). the rich textbox does give a slight improvement :-).

              V.
              Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

              1 Reply Last reply
              0
              • A althamda

                I'm not too sure about putting text into a textbox quicker, but if you definately know that every 2nd character is a letter, then using text[i] = (char)((int)text[i] - 32); is quicker than using text[i] = char.ToUpper(text[i]);

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                if(char.IsLetter(text[i])){
                text[i] = (char)((int)text[i] - 32); //char.ToUpper(text[i]);
                }

                gained me 1 second thanks :-) (this is actually kinda fun ;-), I learned a lot here... ;P)

                V.
                Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                A 1 Reply Last reply
                0
                • V V 0

                  if(char.IsLetter(text[i])){
                  text[i] = (char)((int)text[i] - 32); //char.ToUpper(text[i]);
                  }

                  gained me 1 second thanks :-) (this is actually kinda fun ;-), I learned a lot here... ;P)

                  V.
                  Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                  A Offline
                  A Offline
                  althamda
                  wrote on last edited by
                  #8

                  You could even call your progress bar update fewer times than you are, with a 20 meg file you're currently calling the progress bar update more than 20,000 times. If this takes 10 secs to go through the for loop, that's 2000 progress bar updates per second, bit of overkill there.

                  1 Reply Last reply
                  0
                  • V V 0

                    Guys, A few days ago I asked about an assignment we had to do here at work. convert "aabb" to "aAbB" and set it to the textbox as quickly as possible. I think the conversion itself is very quick, reading in and converting in 1 or 2 seconds, but setting the entire thing in the texbox takes me another 10 seconds! (20 MB file) I've read about the AppendText property and tried to set my characterarray to the box inside my loop, but so far the fastest results I got where form txtbox_result.Text = new string(text); where text = char []. Any ideas on how to speed this up? (Suspend/resumelayout don't help either) thanks ! PS: here's tho code I got so far:

                    		    starttime = DateTime.Now;
                    		    reader = new System.IO.StreamReader(openfiledlg.FileName);
                    		    char \[\] text = reader.ReadToEnd().ToCharArray();
                    		    reader.Close();
                    		    difference = DateTime.Now - starttime;
                    		    lbl\_result.Text = "File read: " + difference.TotalMilliseconds + " milliseconds.";
                    		    lbl\_result.Refresh();
                    		    pb\_conversion.Maximum = text.Length;
                    		    //Starting from 1 will gain me 1 character less to analyze.
                    		    //don't update the progress bar every iteration.
                    		    int i2 = 0;
                    		    for(int i = 1; i < text.Length; i+=2){
                    		        text\[i\] = char.ToUpper(text\[i\]);
                    		        if(++i2 == 1000){
                    		            i2 = 0;
                    		            pb\_conversion.Value = i;
                    		        }											//end if
                    		    }												//end for
                    		    difference = DateTime.Now - starttime;
                    		    lbl\_result.Text = "conversion: " + difference.TotalMilliseconds + " milliseconds.";
                    		    lbl\_result.Refresh();
                    		    pb\_conversion.Value = pb\_conversion.Maximum;
                    		    txtbox\_result.SuspendLayout();
                    		    txtbox\_result.Text = new string(text);
                    		    txtbox\_result.ResumeLayout();
                    		    difference = DateTime.Now - starttime;
                    		    lbl\_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds.";
                    

                    V. I found a living worth working for, but haven't found work worth living for.

                    A Offline
                    A Offline
                    althamda
                    wrote on last edited by
                    #9

                    Instead of using txtbox_result.Text = new string(text); Do this txtbox_result.AppendText(new string(text));

                    V 1 Reply Last reply
                    0
                    • A althamda

                      Instead of using txtbox_result.Text = new string(text); Do this txtbox_result.AppendText(new string(text));

                      V Offline
                      V Offline
                      V 0
                      wrote on last edited by
                      #10

                      tnx, good idea, but didn't help. :)

                      V.
                      Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                      A 1 Reply Last reply
                      0
                      • V V 0

                        tnx, good idea, but didn't help. :)

                        V.
                        Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                        A Offline
                        A Offline
                        althamda
                        wrote on last edited by
                        #11

                        I can process a 20 meg file and display it with appendtext in 4 seconds.

                        V 1 Reply Last reply
                        0
                        • A althamda

                          I can process a 20 meg file and display it with appendtext in 4 seconds.

                          V Offline
                          V Offline
                          V 0
                          wrote on last edited by
                          #12

                          mine is, with ngen and Highest priority thread about 16 seconds. Maybe you have more memory and faster cpu? (Centrino 2,13GHz, 2Gb memory)

                          V.
                          Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                          A 1 Reply Last reply
                          0
                          • V V 0

                            mine is, with ngen and Highest priority thread about 16 seconds. Maybe you have more memory and faster cpu? (Centrino 2,13GHz, 2Gb memory)

                            V.
                            Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                            A Offline
                            A Offline
                            althamda
                            wrote on last edited by
                            #13

                            Here's my code. I'm using a standard textbox.

                            		textBox2.Clear();	
                            			DateTime dt = DateTime.Now;
                            			StreamReader sr = new StreamReader(@"C:\test\test.txt");
                            			char[] c = sr.ReadToEnd().ToCharArray();
                            			progressBar1.Maximum = c.Length;
                            
                            			for (int i = 0; i < c.Length; i += 2)
                            			{
                            				if ((int)c[i] >= 97 && (int)c[i] <= 122)
                            					c[i] = (char)((int)c[i] - 32);
                            
                            				if (i % 10000 == 0)
                            					progressBar1.Value = i;
                            			}
                            
                            			textBox2.AppendText(new string(c));
                            
                            			MessageBox.Show((DateTime.Now.Ticks - dt.Ticks).ToString());
                            

                            Note that a textbox does only store 32k of text, but if your assignment says you just have to fill a textbox, then you are only doing what it says...

                            V 1 Reply Last reply
                            0
                            • A althamda

                              Here's my code. I'm using a standard textbox.

                              		textBox2.Clear();	
                              			DateTime dt = DateTime.Now;
                              			StreamReader sr = new StreamReader(@"C:\test\test.txt");
                              			char[] c = sr.ReadToEnd().ToCharArray();
                              			progressBar1.Maximum = c.Length;
                              
                              			for (int i = 0; i < c.Length; i += 2)
                              			{
                              				if ((int)c[i] >= 97 && (int)c[i] <= 122)
                              					c[i] = (char)((int)c[i] - 32);
                              
                              				if (i % 10000 == 0)
                              					progressBar1.Value = i;
                              			}
                              
                              			textBox2.AppendText(new string(c));
                              
                              			MessageBox.Show((DateTime.Now.Ticks - dt.Ticks).ToString());
                              

                              Note that a textbox does only store 32k of text, but if your assignment says you just have to fill a textbox, then you are only doing what it says...

                              V Offline
                              V Offline
                              V 0
                              wrote on last edited by
                              #14

                              thanks, i'll try this tomorrow. Last day for submitting our tries :-). Maybe I'll post in the lounge next week how I did (or we did ;P)

                              V.
                              Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                              1 Reply Last reply
                              0
                              • A althamda

                                I'm not too sure about putting text into a textbox quicker, but if you definately know that every 2nd character is a letter, then using text[i] = (char)((int)text[i] - 32); is quicker than using text[i] = char.ToUpper(text[i]);

                                M Offline
                                M Offline
                                mav northwind
                                wrote on last edited by
                                #15

                                Unfortunately this will only work for ANSI characters, so you're actually introducing a bug in order to gain a few ms... Not a good deal IMHO.

                                Regards, mav -- Black holes are the places where God divided by 0...

                                A 1 Reply Last reply
                                0
                                • A althamda

                                  I'm not too sure about putting text into a textbox quicker, but if you definately know that every 2nd character is a letter, then using text[i] = (char)((int)text[i] - 32); is quicker than using text[i] = char.ToUpper(text[i]);

                                  L Offline
                                  L Offline
                                  lmoelleb
                                  wrote on last edited by
                                  #16

                                  I am not surprised it is faster as ToUpper actually converts to upper case. :) Subtracting 32 works in a limited subset of letters (the A-Z range and a few others), and it ignores cultural settings. Sure it might be fine enough for a "just for the fun of it" project like this, but unfortunately people end up beleiving they actually can convert using code like this in real projects as well. :(

                                  1 Reply Last reply
                                  0
                                  • M mav northwind

                                    Unfortunately this will only work for ANSI characters, so you're actually introducing a bug in order to gain a few ms... Not a good deal IMHO.

                                    Regards, mav -- Black holes are the places where God divided by 0...

                                    A Offline
                                    A Offline
                                    althamda
                                    wrote on last edited by
                                    #17

                                    It's not introducing a bug, if you had read the original thread from a few days ago you'd realise that this is what the OP wanted - 'convert "aabb" to "aAbB" and set it to the textbox as quickly as possible'. Thanks.

                                    M 1 Reply Last reply
                                    0
                                    • A althamda

                                      It's not introducing a bug, if you had read the original thread from a few days ago you'd realise that this is what the OP wanted - 'convert "aabb" to "aAbB" and set it to the textbox as quickly as possible'. Thanks.

                                      M Offline
                                      M Offline
                                      mav northwind
                                      wrote on last edited by
                                      #18

                                      Ok, if it's really just A's and B's then it'll work of course. I guess I'm already going on auto when seeing someone performing integer maths on characters :)

                                      Regards, mav -- Black holes are the places where God divided by 0...

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups