Fit the text inside a TextBox in VB.net windows forms - vb.net

In one of my application i require to place the text inside a textbox and textbox width and height must change according to the length of text. So i am using
TextSize = gr.MeasureString(textcontent, TextFont)
where textcontent is content of text and TextFont is type of font.(refer this link)
But if text contains large number of character say it require about 2 lines inside a form(if the text will not fit in single line) then i need to set height also. So I want the text to be fit properly inside the textbox for any given text. Also if there are is case of multiple lines(for large text) then there should be not be extra space at the end of first line and between second line. So how it can be done?

It's going to be harder if you use a regular TextBox control since the properties are limited. So I've taken the liberty to suggest to use the RichTextBox control instead since it's still a TextBox but with more features... So try to put a RichTextBox control onto your form and add this code...
Private Sub RichTextBox1_ContentsResized(sender As Object, e As System.Windows.Forms.ContentsResizedEventArgs) Handles RichTextBox1.ContentsResized
RichTextBox1.Height = e.NewRectangle.Height + 12
End Sub

Do something like this...
Decide maximum width (Mx) of textbox according to size of form or as you wish.
Calculate length L required for a string as you are doing.. TextSize = gr.MeasureString(textcontent, TextFont)
If L is less than or equals to Mx then change the width of textbox to L.
If L is Greater than Mx then Height factor (Hf) = L/Mx.
Set txtBox1.Multiline = true and change txtBox1 height to txtBox1.Height * Hf and set txtBox1 width = Mx

Related

Dynamically Resize forms and controls inspite of change in Display size (Small/Medium/Large) in VB.net

We have developed windows application which is including many forms and controls with default small display size and it is working fine.
but in client systems,displays are not consistent. so when the same code runs with medium/large screen size, the controls are overlapping
It would be great help if anyone gives solution for dynamic re-size/fit the form & controls for any display (small-100%, medium-125%, large-150%)
and also Is there any way to find the Display size (small-100%, medium-125%, large-150%) in vb.net
You can change display size in control-Panel.
You can use the Anchor property to automatically adjust the control size according to its form size. You can anchor any side of the control to that side of the form. For example, if I anchor all four sides of a picturebox to the form, the picturebox will mimic the shape of the form when the form is resized. If you anchor only the top and the bottom of the picturebox, for example, it will change height with the form, but the width and horizontal position will remain constant.
Finally found a solution by doing R&D
call the below function in the Form_Load like ScaleForm(me)
Public Sub ScaleForm(WindowsForm As System.Windows.Forms.Form)
Using g As System.Drawing.Graphics = WindowsForm.CreateGraphics
Dim sngScaleFactor As Single
Dim sngFontFactor As Single = 1
If g.DpiX > 96 Then
sngScaleFactor = g.DpiX / 96
'sngFontFactor = 96 / g.DpiY
End If
If WindowsForm.AutoScaleDimensions = WindowsForm.CurrentAutoScaleDimensions Then
'ucWindowsFormHost.ScaleControl(WindowsForm, sngFontFactor)
WindowsForm.Scale(sngScaleFactor)
End If
End Using
End Sub

Access Report with dynamically changing text box size

Is it possible to have a report with flexible text width and height? I sometimes have two words in this text and some times hundreds. I want to have small text for the first and big text for the second. How do I do that?
I would advise you to set a your text box size to what you think is optimum and use the CanShrink and CanGrow properties (tap the text box and then open the properties windows and you can find them there).
The CanGrow property indicates whether the size of the text box can increase vertically according to its content. Similarly, CanShrink decreases the height of the text box according to its content. Here is a link for better understanding these two properties.
Use the Detail_Format event.
It fires before each line, and you can change the formatting based on the length of the text.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Len(Field1) < 10 Then
txtField1.FontSize = 18
Else
txtField1.FontSize = 12
End If
End Sub

Moving label to fit according to length vb.net

Hi I have a button which will add a string in to a label. (this can be done multiple times.) As the user can add multiple strings with each one been a different length, is there anyway I can get it to find the length of the label and those around it so that it spaces out correctly?
Thanks
Label.Width will return the current width of the label control, but it sounds like you already know this. Since the label can be narrower than the text it is trying to display, you would need to measure the full text using the graphics object. This method will return the width of the text in a label:
Private Function getFullTextWidth(lbl As Label)
Using g As Graphics = Label1.CreateGraphics()
Return g.MeasureText(Label1.Text, Label1.Font).Width
End Using
End Function
Alternatively, you could just set the label's AutoSize property to true, and then just check the label's Width property after you set the Text.

Label position should fixed right and grow to left

How can I set my labels to align on the right even when they have diffrent lenghts.
I have a set of labels which are occuring next to each other and also underneath each other.
The problem now is that they always align from the left within the label row,but I need them to align on the right as they are showing sums from other rows. Just to verify I am not talking about the text align I am looking for a solution to align my labels.
Thanks in advance
Simply set the AutoSize property to False in the designer. Adjust the size to fit the column. Then set the TextAlign to one of the right-alignment ones.
You should be able to do it at runtime using the following code:
'find the current right alignment position
Dim rightAlign As Integer = Label1.Left + Label1.Width
'set the text (assumes AutoSize is set to True)
Label1.Text = value
'adjust position so the right hand point is in the same position as before
Label1.Left = rightAlign - Label1.Width
My method is even more strange. I create the labels and then when laying out the fields for the report adjust the labels for number (etc) that are to be right aligned
Note: all labels end with 'lbl'
- txtNew is the report column text box.
- get the column's left edge plus the width of the column minus the width of the label. Works! Just not my favorite way to do it.
' *** NEED TO CALC POSITION FOR RIGHT JUSTIFY OF LABEL !!!!!
If ShouldRightJustify(rs.Fields(i).Type) Then
rpt.Section(acPageHeader).Controls(rs.Fields(i).Name & "lbl").Left = _
(lblCol + txtNew.Width) _
- rpt.Section(acPageHeader).Controls(rs.Fields(i).Name & "lbl").Width
End If
If you are asking how to do this from the designer, use the Format Menu.
Select all the controls you want to align, then click the control you want the other aligned to. Do Format > Align > Rights.
If you are trying to do this at run-time you can loop through the controls you want to align and set their .X property according to their width. For example. To align a label so that it's right side is at X=200... SomeLabel.X = 200 - SomeLabel.Width.

How to increase the size of a text box base on the text content

I have a report that I need to increase the size of the textbox base on the length of the text. There some formula to calculate the resulting lenght size?
You can either go for a Label with the AutoSize property set to True. Or if you want to stick to TextBox, you can make use of MeasureString method and get the width of the text from there. Use that width to set the width of TextBox.