how can I set TextBox.ScrollBars to None or Both at call
-
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 sizethe 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
-
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 sizethe 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
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 ClientSizeChangedShimi