How to fix TextBox height in C# - c#

Hi, guys..
I'm working on a small project with C# language in Visual Studio 2010 for my own business (Not using WPF). So, In the designer, I normally added a TextBox from ToolBox to the Form and, then normally changed the name of the TextBox from Properties Box. After that, I wanted to change Font Name & Size (The default values for Font name and size are "Tahoma" & "8.75" respectively). So, when I changed the Font name to " Times New Roman", the size of the Font was small, So I decided to change it From the default value to "11.5". So, when I changed the Font size to required value, it got changed, but the TextBox height increased. So, How can I change the Font size for the TextBox to get bigger without any effect on the height property?, In other words, how to fix the TextBox height, when I changed the Font size to get bigger?..
Thank you so much for your interest, I hope I got the problem clearer..
(Note: I haven't used a code yet. Just from the Designer)

The TextBoxBase.AutoSize property determines whether the height of the TextBox will change to fit the the size of the font. If you set it to false, you can change the height to whatever you want regardless of the font size.
This property is not visible in the Properties window, so you will have to set it in code. If the name of your TextBox is textBox, then:
textBox.AutoSize = false;

Related

How do you calculate pixel size of string?

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.

Determine Control's 'AutoSize'-size at runtime

I want to layout controls during runtime (dynamically created). For the purpose of this question, let's restrict to a Button control. I want to set the control's properties (such as Text) and then determine the minimum size for the control for it to display properly; the size that setting AutoSize = true would give. In C# example code, with GetAutoSizeSize being this minimum size:
Button button = new Button();
this.Controls.Add(button);
button.Text = "Example";
button.Size = GetAutoSizeSize(button);
button.Location = /* Some calculation based on button.Size */
Possible solution: AutoSize
One can set button.AutoSize = true and button.AutoSizeMode = AutoSizeNode.GrowAndShrink. After that, the button.Size can be fetched, after which AutoSize can be turned off and the control size can be changed.
Potential issues:
It looks odd and I can't help but feel that this could easily break, but maybe I am wrong?
Possible solution: GetPreferredSize
button.GetPreferredSize can be used to get a size that the control wants to be.
Problems with this:
Its usage is internal and/or meant for flow layout.
GetPreferredSize takes a suggested size as a parameter, so one needs to guess at what would be appropriate.
The size returned is wrong, in that it returns the 'comfy' size of a control, which can be much larger than the minimum size that AutoSize gives.
EDIT: From the comments and some trial-and-error, I was able to conclude that the problems I originally listed with the AutoSize-method were due to needing both the control to be added to the control collection first and AutoSizeMode set to GrowAndShrink.
I would like to know if there is a function (and/or more 'robust' way) of determining the AutoSize-size: a function like GetPreferredSize that returns the size without actually having to toggle AutoSize.
This works when you are drawing on a control.
String sMyString = "this is my string";
Font fntFont = new Font("Arial", 8);
SizeF sfMySize = new SizeF(5,5);
sfMySize = System.Graphics.MeasureString(sMyString, fntFont, sfMySize);
This will give you the dimensions of the bounding box around the control text. You would have to work out the appropriate buffer around the text to set the button size.

How to force font dialog inside property grid to use pixel size unit

I need to make sure my image text to be same size when DPI is changed therefore I must use font with "Pixel" unit but font dialog changing unit to "Point". I'm using font dialog inside property grid:
public Font TextFont { get; set; }
How can I force font dialog to use "Pixel" unit so watermark text size will be same in every DPI?
Create a custom font dialog.
Change the Font that's being displayed by the property grid and the property grid will show the units that the font currently has. The default font dialog creates a new Font that has Point units for any font change.

How do I set button font to Marlett

I'm attempting to set a font of button to system's Marlett font. However, though I manually set the font-face, other font is used. Also, Marlett is not listed, when I use the font dialog to choose a font for that button.
Why is it so? What can I do to use Marlett font in .NET Windows Forms controls?
Though I do not know what code is behind the designer, I have always found that custom installed fonts do not show up in the designer. The good news is that the Font property is ambient so if you wanted all controls to have the same Font you would only have to set it at the Form. However, it seems like you just want one control to have the Font so let's do this:
ctrl.Font = new Font("Marlett", 8.5f);
which will set that control's Font to Marlett and a size of 8.5 for example.
If you wanted an entire set of controls to have the same Font, if they can be placed in a container like a Panel, then you would only have to set the Font of the Panel; because again, it's an ambient property.
button1.Font = new Font("Marlett",8, FontStyle.Regular);
put this code for your button name Button1 , where you want change ( in from constrcutor after iinitializecomponet or in form Load event )
It would seem that the designer by default wants to set the GdiCharSet to 0. This causes the Marlett font to fall back to another font.
If you change the GdiCharSet to 1 it will render normally.
Also note the changes it makes in the .designer.cs, this will also explain why it did work when you would set the font manually from code.
This is what finally worked for me.
ctrl.Font = new System.Drawing.Font("Marlett", 12f, FontStyle.Regular, GraphicsUnit.Point, ((byte)(1)));
The last ((byte)(1)) sets the GdiCharSet to 1, which #Paul noted in one of the other answers.

How can I change the font in a DataGrid?

I am using:
dg.ForeColor = System.Drawing.Color.Black;
dg.BackColor = System.Drawing.Color.Beige;
to set the background and foreground color of a DataGrid. How can I change the font to say Calibri or any others using the built-in methods.
If this is a web application, the DataGrid class already provides a Font property that you can set to
a font of your choosing. You can set it either from the designer or in your source code, just as you've set the BackColor and ForeColor properties.
However, as I mentioned in a comment, you should be very careful about setting controls to use a font that the user might not have installed on their computer. I recommend checking a list of web-safe fonts.
You can change the font with this code
dg.Font=new Font(string familyName,int size);

Categories

Resources