Textbox automatic sizing on giving input string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karan1981
    New Member
    • Apr 2008
    • 6

    Textbox automatic sizing on giving input string

    Hi,

    I have a user control that has a textbox.
    This user control is placed in a form at runtime. This form gives a string to the user control and the textbox is to show this in multiple lines.(i do so using a property to set the textbox's text property)
    So in designer view i set the multiline property to TRUE and
    the word wrap property to TRUE.
    This enables the text to be shown in multiple lines at runtime.
    But the textbox size does not grow according to the number of lines of text that it contains.
    I dont want to use scrolls.
    Please suggest how to set the size of the textbox to adjust according to the number of lines it has at runtime. So that all lines are shown. and i can accordingly place other contols below it.

    Thanks
  • vanc
    Recognized Expert New Member
    • Mar 2007
    • 211

    #2
    Originally posted by karan1981
    Hi,

    I have a user control that has a textbox.
    This user control is placed in a form at runtime. This form gives a string to the user control and the textbox is to show this in multiple lines.(i do so using a property to set the textbox's text property)
    So in designer view i set the multiline property to TRUE and
    the word wrap property to TRUE.
    This enables the text to be shown in multiple lines at runtime.
    But the textbox size does not grow according to the number of lines of text that it contains.
    I dont want to use scrolls.
    Please suggest how to set the size of the textbox to adjust according to the number of lines it has at runtime. So that all lines are shown. and i can accordingly place other contols below it.

    Thanks
    You can calculate the number of characters that will reach the textbox's edge and resize the textbox according to the number of entered characters. Cheers.

    Comment

    • karan1981
      New Member
      • Apr 2008
      • 6

      #3
      Originally posted by vanc
      You can calculate the number of characters that will reach the textbox's edge and resize the textbox according to the number of entered characters. Cheers.
      Hi Vanc

      Thanks for replying.
      Basically In designer view, My textbox just fits in one line of string.
      At runtime it could get a very long string.
      Thus as Multiline is true, the rest of the text comes in the next line and so on.
      But unfortunetely my textbox size remains the same.
      I need it to grow(vertically ) with the numbers of line at runtime.
      I dont want to use scroll bars.
      could u please suggest how to go about this.
      Is the best way to know how many characters can fit in a single line and then divide total characters by that, to know the number of lines. and then resize by that many times?
      or is it better to use a label instead. i never have to take user input. i just need to display the string at runtime in multiple lines.

      Thanks alot. really appreciate your help.
      Karan

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        As was suggested, compute the number of characters in the string you have, divide by the number of allowable characters wide in the textbox (it's a property, you can set it) and determine how many rows to make your textbox show.

        Comment

        • karan1981
          New Member
          • Apr 2008
          • 6

          #5
          Originally posted by Plater
          As was suggested, compute the number of characters in the string you have, divide by the number of allowable characters wide in the textbox (it's a property, you can set it) and determine how many rows to make your textbox show.
          Hi Plater

          Thanks for replying too.
          My textbox is "_noteBody"
          This is what i wrote in a function after the textbox gets the long string :-

          Graphics g = _noteBody.Creat eGraphics();
          SizeF size = g.MeasureString (_noteBody.Text , _noteBody.Font) ;
          float lines = size.Width/_noteBody.Width ;
          int linecheck = (int)lines;
          if (linecheck < lines)
          linecheck += 1;
          _noteBody.Heigh t = _noteBody.Heigh t * linecheck;

          I did the "+=1" incase the division ended up to be something like 3.5 or something. So that i get a another line for that.

          Please suggest if this is appropriate.
          Thanks alot.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I think it could work. Not sure if it has any advantages/disadvantages over doing it other ways.

            Comment

            • karan1981
              New Member
              • Apr 2008
              • 6

              #7
              Originally posted by Plater
              I think it could work. Not sure if it has any advantages/disadvantages over doing it other ways.
              Hi Plater

              Thanks for replying.
              A) But how else to calculate the number of strings that can fit in a single line?
              B) Also how to make a textbox transparent? it does not let me set the backcolor property to transparent. Is there any way to do this. I did google for this but could not find a easy solution.
              C) lastly how to vertically center the text in a textbox.

              I really appreaciate your help.
              Thanks

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                well you have a string that is going into the textbox yes?
                You know how many characters are in that with .Length property.
                In web applications, the multi-line textboxes have a Rows and Columns property where you can set/get how man characters per row (Columns) and how many Rows to show.
                If you set your textbox to 80 Columns, dividing your string's length by 80 (and possibly adding 1 to handle "rounding") you should get the value you would set to Rows.

                Comment

                • karan1981
                  New Member
                  • Apr 2008
                  • 6

                  #9
                  Originally posted by Plater
                  well you have a string that is going into the textbox yes?
                  You know how many characters are in that with .Length property.
                  In web applications, the multi-line textboxes have a Rows and Columns property where you can set/get how man characters per row (Columns) and how many Rows to show.
                  If you set your textbox to 80 Columns, dividing your string's length by 80 (and possibly adding 1 to handle "rounding") you should get the value you would set to Rows.
                  Thanks Plater.
                  I have a system.Forms application. Will try on that.
                  Also regarding the issue of making the textbox transparent, any ideas...

                  thanks
                  karan

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Originally posted by karan1981
                    Thanks Plater.
                    I have a system.Forms application. Will try on that.
                    Also regarding the issue of making the textbox transparent, any ideas...

                    thanks
                    karan
                    Ugh, sorry. Was stuck on web application.
                    Even when I went "but web applications don't have graphics objects for their textboxes...." it didn't occur to me.

                    The graphics method would be prefered then I think.

                    Comment

                    • karan1981
                      New Member
                      • Apr 2008
                      • 6

                      #11
                      Hey thats fine. My mistake i didnt spcify.
                      Thanks alot :)

                      Comment

                      Working...