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. .NET (Core and Framework)
  4. how can I set TextBox.ScrollBars to None or Both at call

how can I set TextBox.ScrollBars to None or Both at call

Scheduled Pinned Locked Moved .NET (Core and Framework)
question
2 Posts 1 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.
  • S Offline
    S Offline
    Shimmy Weitzhandler
    wrote on last edited by
    #1

    I want to create a method that maked the scrollbars of a textbox control visible only if needed. I tried to use the following method but it doesn't always work, because some times is the control's width much bigger than the Height (I am talking when the WordWrap is set to False): Private Sub TextBox1_TextChanged(ByVal sender As TextBox, ByVal e As System.EventArgs) _ Handles TextBox1.TextChanged, TextBox1.Resize, TextBox1.FontChanged Dim fS As Size = TextRenderer.MeasureText(sender.Text, sender.Font) Dim cS As Integer = fS.Height * fS.Width Dim dS As Integer = sender.Size.Height * sender.Size.Width If dS < cS Then sender.ScrollBars = ScrollBars.Both Else sender.ScrollBars = ScrollBars.None End If End Sub so here is some findings about the text box's control behaviour, maybe it's gonna be useful:

    these numbers just bellow (123456) charachterize th number of lines
        1 	2 	3 	4 	5 	6 	
    

    10 6 22 38 54 70 86 16^
    11 6 24 42 60 78 96 18^
    12 6 26 46 66 86 106 20^
    13 6 28 50 72 94 126 22^
    0 2 4 6 8 10 ^the numbers in this col. are the control's increment per font against line
    ^this column is the textbox's font's size

    the numbers within the table (6,22,38~72,94,126) are the Size.Height of the control, corresponding to font size and line numbers.

    well I don't want to waste your time, does any body have any solution? thanks for your time.

    Shimi

    S 1 Reply Last reply
    0
    • S Shimmy Weitzhandler

      I want to create a method that maked the scrollbars of a textbox control visible only if needed. I tried to use the following method but it doesn't always work, because some times is the control's width much bigger than the Height (I am talking when the WordWrap is set to False): Private Sub TextBox1_TextChanged(ByVal sender As TextBox, ByVal e As System.EventArgs) _ Handles TextBox1.TextChanged, TextBox1.Resize, TextBox1.FontChanged Dim fS As Size = TextRenderer.MeasureText(sender.Text, sender.Font) Dim cS As Integer = fS.Height * fS.Width Dim dS As Integer = sender.Size.Height * sender.Size.Width If dS < cS Then sender.ScrollBars = ScrollBars.Both Else sender.ScrollBars = ScrollBars.None End If End Sub so here is some findings about the text box's control behaviour, maybe it's gonna be useful:

      these numbers just bellow (123456) charachterize th number of lines
          1 	2 	3 	4 	5 	6 	
      

      10 6 22 38 54 70 86 16^
      11 6 24 42 60 78 96 18^
      12 6 26 46 66 86 106 20^
      13 6 28 50 72 94 126 22^
      0 2 4 6 8 10 ^the numbers in this col. are the control's increment per font against line
      ^this column is the textbox's font's size

      the numbers within the table (6,22,38~72,94,126) are the Size.Height of the control, corresponding to font size and line numbers.

      well I don't want to waste your time, does any body have any solution? thanks for your time.

      Shimi

      S Offline
      S Offline
      Shimmy Weitzhandler
      wrote on last edited by
      #2

      this what I have made, and it does the trick (my WrapLines is set to false I didn't check when true): private void tbScroll(object sender, EventArgs e) { TextBox tb = (TextBox)sender; Size tS = TextRenderer.MeasureText(tb.Text, tb.Font); bool Hsb = tb.ClientSize.Height < tS.Height + int.Parse(tb.Font.Size); bool Vsb = tb.ClientSize.Width < tS.Width; if (Hsb && Vsb) { tb.ScrollBars = ScrollBars.Both; } else if (!Hsb && !Vsb) { tb.ScrollBars = ScrollBars.None; } else if (Hsb && !Vsb) { tb.ScrollBars = ScrollBars.Vertical; } else if (!Hsb && Vsb) { tb.ScrollBars = ScrollBars.Horizontal; } sender = (object)tb; } this event is raised at TextChanged and at ClientSizeChanged

      Shimi

      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