I'm using a TextBox to create an editable title on something that looks like a post-it note. I changed to a TextBox from a RichEditBox to see if that could solve my problem, which it didn't, so I'm willing to change back if that helps.
My problem is that I don't want the user to be able to enter more characters than fit in the set width of the TextBox, because I want the whole title to be visible. Setting a fixed limit to the amount of characters that can be entered doesn't really work since for example 10 large M's would fill the width of my TextBox, but other characters will only fill half of it. So I would like to compare the width of the text to the width of the TextBox, so I can restrict input beyond that point.
Edit: I'm using the Segoe UI font, and I don't really consider changing the font to one with characters of equal width as a solution.
Set the TextBox Font to a Courier Font, then all characters are equal width and calculating max becomes trivial.
I am suck in programming world. i m try help you..
Related
I have created a document in an open office with a multi-line form field:
The issue I am having is when the dynamic content exceeds the initial size of the multi-line text box:
Sure I can re-size the Text Box in the original template but the dynamic content may be from 1 to 50 lines and I want the text after the Text Box to be close to the last line of dynamic content.
Can someone suggest a way to solve this?
I have once coded a solution to set the width and height of a textbox programmatically based on the characters supplied. I think this was a school assignment a long time ago.
This can be done with both VBA in a code behind or probably with a macro even. Or with VB.net.
I don't have the code I used way back when, but basically determine the maximum width you are able to provide in character width, the preferred width. Determine the pixel requirement per character for that width. This becomes the textbox width.
Divide your total string character count by your preferred width character count. Round up, calculate the pixel height per character. And use this value times the rounded result for the textbox height. Dirty but it should work.
Any chance you can change to a label and set AutoSize to True? You can fix the width and let the height auto adjust. This should be done before converting to a pdf. In fact all of the sizing should be resovled before pdf conversion.
Another Down voter without a comment, should not be allowed.
I have a problem with character spacing.
Basically I have something like this which comes from a txt file:
****************
*System Details*
****************
Looks nice and uniform, however, when I open have this go into a RichTextBox this happens:
Irregular character spacing example:
I've tried all different properties to try and stretch it, render it etc. but nothing works.
The data is coming in from code-behind OpenDialogBox that stores all the lines of the file in a string[]. A foreach loop then sends the lines into the RTB. (It needs to be a loop as each line gets checked)
Any help it greatly appreciated!
Many thanks
This is most likely a font choice problem. By default WPF uses Segoe UI on Windows 7 and above which is a non-monospaced font. This means that each character will not necessarily take up the same amount of space as each other character leading to issues if you are trying to align characters between lines. The easiest way to get alignment to work is by changing the font to a monospaced font by setting the FontFamily property on the RichTextBox.
I have a string. I know the font family and the font size it is going to be displayed in. I need to know how many pixels the text will take up in the ui. So that I can determine whether to show another element. How would I do that?
I found a couple of things, but none of them were available in my Windows universal project. For example:
Graphics.MeasureString
TextRenderer.MeasureText
Edit:
This is not a web project.
I want to calculate the size it will take in the ui before it is in the ui.
I think you need to create a textblock in code and assign the desired text to it. Then you can get the actual height and width from it. See the below code
TextBlock txt=new TextBlock();
//set additional properties of textblock here . Such as font size,font family, width etc.
txt.Text = "your text here";
var height = txt.ActualHeight;
var width = txt.ActualWidth;
You can do further operations based on this height and width
I am not saying this is the optimized solution .But this will work for you
Try checking the values of the Width and Height properties of the control you use to display your text (eg. your TextBox), after setting your string as text/content, to decide whether to show another element.
I've been making my own event management system because I don't want to pay money for Playmaker:
I'm getting this really terrible spacing between labels and their components (for example, in the picture above, between Nickname and it's text field)
The script can be found here (keep in mind, its a work in progress. I haven't had time to clean it all up):
http://pastebin.com/w2cLWBvh
It looks like that layout system you've been using aims to give both the nickname label and text input field nearly the same amount of space within their combined area.
Without knowing an awful lot about what layout options Unity's GUI system has, you can probably do with setting the length of the label and textbox arbitrarily.
Here's how you can get the size of a label given a specific string:
GUI.skin.GetStyle("Label").CalcSize(new GUIContent(widestIdString));
So say you have a new BeginArea containing your label and textbox combo. You'd set the label length to this newly calculated width, and the textbox width to the difference of the Area's width and the label's width, plus an arbitrary distance to seperate them, such as 23f;
If I have a Control, I can know the Font it is using by the Font Property. Now how can I know the average Size of the Font( or lets say I would like to know what would it be the width of the 'M' char)
I know that Graphics.MeasureString() would solve my problem but implies creating a graphic object since I'm not in any paint event.. or wouldn't like to use the CreateGraphics. or nothing like that. I'm raising a grid and would like to set my column Width since I have all my columns text information.
(THis is for Winforms..... NOT WPF)
I am afraid there might not be a guaranteed accurate measurement, you can try TextRenderer.MeasureText.
A good post in SO here on "Accuracy of TextRenderer.MeasureText results"