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.
Related
I'm trying to make (using Visual Basic) a rudimentary questionnaire that measures multiple attributes and stores their values in an array and afterwards measures the attributes on a scale of 1 to 10.Now I've had the dumb idea to use a label for every single value of every attribute, and highlighting the right number by changing the label's image to yellow, instead of white(basically making the area behind the number a different color).
Now here's the issue: I can't seem to find out how to change a label's background image with code, which is what I'm asking. (I'm guessing the command should look something like "Label1.image = >image path<")
Pretty simple.
If text = No then
Label.Text = No
Else
Label.Text = Yes
End IF
You just have to change the text value of the label which is .Text = Text you want
And if your trying to show images, you should use the image control in the tool box, not a label
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
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
I'm creating a label dynamically, and then adding text to it dynamically (and the amount of text added to it will be different each time). So, the label needs to always be the same width as the text inside it.
This happens by default when creating a label in the Windows Designer. But when creating a label dynamically, it appears to be "set" at a specific width, regardless of how much text is in it (which means that it often "cuts off" some of the text).
So... any idea how I can get the dynamically created label to always stay the same width as the text inside it?
If you want to do it manually, you can do something like this, every time you change the text:
Dim g As Graphics = Label1.CreateGraphics()
Label1.Width = CInt(g.MeasureString(Label1.Text, Label1.Font).Width)
However, it's much easier to simply set the label's AutoSize property to True and let the label do the work for you.
Label1.AutoSize = True
I have added Label to my ViewController.xib.
The problem is that when the text of the label is too long, and only part of the text is seen. It would not go to the next line.
How can this be done?
Set the property either programatically with:
#property(nonatomic) NSInteger numberOfLines
or in interface builder.
Here are the docs:
This property controls the maximum number of lines to use in order to fit the label’s text into its bounding rectangle. The default value for this property is 1. To remove any maximum limit, and use as many lines as needed, set the value of this property to 0.
If you constrain your text using this property, any text that does not fit within the maximum number of lines and inside the bounding rectangle of the label is truncated using the appropriate line break mode.
When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.