Design-time size of Label (Compact Framework) - c#

I refer to the Visual Studio design view. When adding a label using the full .NET framework, System.Windows.Forms.Label controls have an AutoSize property set to True by default. If I drop a Label control onto a form, the default font is "Microsoft Sans Serif, 12pt, Regular" and the height of the control is 20. Change the font to (say) "Tahoma, 14pt, Bold" and the height automatically changes to 23. Fine.
But in the CF (Compact-Framework), Label controls do not have the AutoSize property. When I change the font as described above the height of the Label does not change (staying at 20) and some of the text is chopped off).
My question is: Short of opening a full .NET project and testing my font selection, is there a way to know what height to set my Label at design-time?

You can use the graphics object for to measure the height of the string, for example in Paint event or OnPaint method:
Graphics g = e.Graphics;
float lineHeight = g.MeasureString(this.Text, this.Font).Height;
it can be used in inherited label. for example you can override OnPaint method or somewhere else and do this, then it will run in design-time.
More resources:
Base on Font height in compact framework
Aslo you can take a look at Multi-Line Graphics.MeasureString on .Net CF that uses DrawText from coredll.dll.

Related

How to fix TextBox height in 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;

In C#, in tooltip how to change different color for differnt part of text?

In the MS chart, I am displaying tooltip using below code.
ToolTip ToolTip = new ToolTip();
ToolTip .Show(" X value:"+s+"\nLine 1 Y value: =" + ss + "\nLine 2 Y value:=" + ss1, chart, (int)e.Location.X, (int)e.Location.Y);
I am able to set only one foreground color using ToolTip .ForeColor = System.Drawing.Color.Red;.
I am new to C#.
How to assign a different colour and draw text in custom tooltip class or how to use HTML renderer to achieve my requirement?
I could not assign a different colour for a different part of the tooltip text.
How to achieve it?
You can owner-draw the tooltip
Example:
ToolTip ToolTip = new ToolTip();
ToolTip.OwnerDraw = true;
ToolTip.Popup += (ss, ee) => { ee.ToolTipSize = new Size(200, 50); };
ToolTip.Draw += (ss, ee) =>
{
ee.DrawBackground();
ee.DrawBorder();
ee.Graphics.DrawString("Warning", Font, Brushes.Red, 10, 1);
ee.Graphics.DrawString(ee.ToolTipText, Font, Brushes.Black, 1, 22);
};
ToolTip.Show("Demo only", somecontrol..);
This is just a simple example; there are many more parameters to style the tooltip, including drawing stuff, images, brushes of all types, etc..
It is also recommended to use TextRenderer instead of the classic GDI+ DrawString.
Note how I set the Size in the PopUp event!
All sorts of formatting is possible with the text; for multiline text it is recommended to use an overload with bounding rectangle instead of x/y coordinates and maybe also alignment with a StringFormat. Do note though, that is is always tricky to embed formatted parts inside of a text.
Possible, but tedious to get really right, as always with GDI drawing. -
The basic trick is to determine a bounding rectangle first; this can be done with MeasureString.
The short answer would be "natively you simply can't" (Except you count drawing the label yourself as natively). But as always in programming there are creative ways to get the result you want.
The answer in the Question Orel suggested basically makes use of a renderer for HTML Markup to render the styled text inside your application.
If you have a look at the newer version of this library here they actually provide a WinForms ToolTip Control which accepts HTML Markup to render inside it's content area.
To use this renderer they provide a nuget package which makes installation trivial. Simply manage your projects nuget packages and search for HtmlRenderer.WinForms and install the latest version. (Check if it also installs the latest Version of HtmlRenderer.Core because it didn't on my end and I had to update the Core package)
After this rebuild your project to get the new controls in your designer toolbox.
To test the package I dragged a textbox and a HtmlToolTip onto my Form. To set the new toolTip you use it just like a normal WinForms tooltip.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.htmlToolTip1.SetToolTip(this.textBox1, "<h1 style=\"color:red\">Hello</h1><i style=\"color:blue\">World</i>");
}
}
Now you can style your toolTip content with HTML markup and change the foreground colors as you like:

LinkLabel no underline - Compact Framework

I am developing a Windows CE app with Microsoft Compact Framework. I have to use a LinkLabel and it has to be white and no underline.
So in the designer, I modified font color by white and unchecked "underline" in the font dialog.
However, when I run the application, the font is still blue and underlined.
Is there a way to remove the underline of a LinkLabel and change its color?
You can use LinkBehavior:
Me.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;
It wont be visible in the designer at Design-Time but will be correct in Runtime.
Otherwise do it in Code (which should be the same as the designers code):
Font f = LinkLabel1.Font;
LinkLabel1.Font = New Font(f, f.Style && !FontStyle.Underline)

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.

DomainUpDown (spinner) control is cutting off the bottom pixel of displayed text

I'm using winforms and the DomainUpDown control's height is locked at 20 pixels, which results in "y"'s and other characters with descenders cut off on the bottom.
My initial thought about how to fix the problem was to change the controls height, but I couldn't do so. In the designer I only have controls to drag it's size by width. The property page immediately reverts any change to height I make. Attempts to change the value in code silently fail; no error, no exception, but no change to the value either.
In this sample form the "g" in the DomainUpDown will be cut.
public partial class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DomainUpDown domainUpDown1 = new System.Windows.Forms.DomainUpDown();
public Form1()
{
this.domainUpDown1.Location = new System.Drawing.Point(16, 8);
this.domainUpDown1.Size = new System.Drawing.Size(212, 20);
this.domainUpDown1.Text = "why are descenders like g cut?";
this.ClientSize = new System.Drawing.Size(328, 64);
this.Controls.Add(this.domainUpDown1);
}
}
I see the same fixed height behaviour when using DomainUpDown controls. You can adjust the size of the font that is used, which changes the height of the control to match the text. Perhaps adjusting the size of your text slightly can help with the clipping of the characters with "descenders". I see no clipping using the default 8.25pt font.
EDIT:
After replicating on XP running the classic theme and with Dan's testing, the problem appears to be the thickness of the borders and padding, which cut off the g.
Setting the BorderStyle to either FixedSingle or None fixes the problem.
domainUpDown1.BorderStyle = BorderStyle.FixedSingle;
or
domainUpDown1.BorderStyle = BorderStyle.None;
You will need to see what looks best in your application. Oh, and setting your theme to XP (rather than classic) will work too.

Categories

Resources