Change Label Font Size Dynamically Not Updating On Screen - c#

Am trying to change a label font size depending on screen resolution. Have tried when the form is loading, shown and also in the form constructor, but on screen the font size is the same as design time.
Rectangle resolution = Screen.PrimaryScreen.Bounds;
if (resolution.Width == 1024 && resolution.Height == 768)
{
this.labelEnterRegistration.Font = new Font(this.labelEnterRegistration.Font.FontFamily, 40f);
}
I've added a double click event to the label to check the font size, and it says it's 40 in a message box (MessageBox.Show(this.labelEnterRegistration.Font.ToString());), so why doesn't the form display reflect this?
I have tried the label Invalidate()but that didn't work either.

Have fixed it. As it was done before it was setting the fonts emSize, I did the following so it changes the pixel size:
FontStyle style = this.labelEnterRegistration.Font.Style;
this.labelEnterRegistration.Font = new Font(this.labelEnterRegistration.Font.FontFamily, 40f, style, GraphicsUnit.Pixe
And now keeps the same font style as well!!
Thanks for the comments #HEPÄ°MÄ°ZYARBAYMEHMETALKANIZ, made me think about it some more.

Related

How to add an image icon before the text of a WinForms button?

I'm trying to add .ico 48x48 image before text in WinForms button 87x30 size:
button1.BackgroundImageLayout = ImageLayout.Stretch;
button1.BackgroundImageLayout = ImageLayout.None;
button1.BackgroundImageLayout = ImageLayout.Zoom;
button1.ImageAlign = ContentAlignment.MiddleLeft;
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.TextAlign = ContentAlignment.MiddleRight;
Result is:
I'm trying to figure out, how to align image on the left side with text on related distance, like this:
edit:
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.TextAlign = ContentAlignment.MiddleLeft; /// MiddleRight; // MiddleCenter;
button1.ImageAlign = ContentAlignment.MiddleRight; /// MiddleLeft;
Result:
The background image property is like the operating system desktop background, it is a wallpaper, that can be stretched, adapted, repeated...
Therefore here you don't need to use BackgroundImage for a button icon style image associated to its text.
If you use the Image property and set alignments to left for it and right for text, all works fine:
Then you can adapt these alignments as well as width and height to the desired result depending on the image size and/or text size and length.
Also, as indicated by the duplicate I finally found, to simply center all, you can use the TextImageRelation and set it to TextImageRelation.ImageBeforeText without changing alignments, and if necessary by increasing the height according to the size of the displayed image to have a clean result:

Auto Scale text to fit the screen in mono

I'm trying to scale a given text, e.g "123\nV.1-4", to fit the screen in mono.
I tried increasing the font size till the screen is not bigger.
My main problem is that I cannot retrieve the size of the label with a new font quickly?
(I'm retrieving with label.GetSizeRequest(out width, out height);)
So any solutions on how to scale the font/text?
Thanks
When the label is resized as you change the font size, the SizeRequest and SizeAllocated events will fire. What you could do is set the font size, then in the SizeAllocated event for the label, set the font size again. You will clearly have to stop the loop at some point, I assume you would cease increasing the font size when your label dimensions outstrip those of the window or screen etc.
However, have you considering using Pango and a drawing area instead?

TextBlock shown without text in expanded WPF Canvas

I'm using C# with WPF, to draw some graphics and texts into a WPF Canvas.
For adding texts to the canvas I use a TextBlock and this works fine when I don't change the size of the Canvas.
When I extend the width of the canvas, sometimes the text is not drawn (from the x-position where I extended the canvas). The text is there, because I can see the text background (and also a strikethrough which I added for testing).
When I change the length of the text and/or set the TextBlock length in code, the behavior changes. Meaning with some changes the text is shown.
The graphics are shown just fine in all circumstances.
I've tried to make sure the Width and ActualWidth of the Canvas are correct before I draw the texts.
I use this code to draw the text onto the Canvas:
private void drawHeartrate(string hr, double xHr)
{
TextBlock hrTextBlock = new TextBlock();
hrTextBlock.Text = hr;
hrTextBlock.Foreground = Brushes.Black;
hrTextBlock.TextDecorations = TextDecorations.Strikethrough;
hrTextBlock.Background = Brushes.Yellow;
hrTextBlock.FontFamily = new FontFamily("Soho Gothic Pro");
hrTextBlock.FontSize = 14;
Canvas.SetLeft(hrTextBlock, xHr);
Canvas.SetTop(hrTextBlock, -2);
CnvsEcg.Children.Add(hrTextBlock);
}
The screen where the texts is not displayed anymore looks like this (from the above code):
Screenshot of problem text

Fit Label size to drawn text

I'm using a Label to display progress to the user. This occurs many times and the partly user-defined text changes every time.
Problem: I should know if the drawn text is bigger than the Label's size.
I tried this approach:
using (Graphics g = lbl.CreateGraphics()) {
SizeF size = g.MeasureString(lbl.Text, lbl.Font);
// Change size of label if too small
}
but it is too slow and uses a lot resources when calling at every update.
So is there a way to find out when the drawn text is bigger than the Label?
EDIT:
As stated by Hans Passant, AutoSize will do it.
Sorry, I didn't say that other controls below the Label has to move then.
It seems that there is no other way to do it.
So I have to use the above solution:
using (Graphics g = lbl.CreateGraphics()) {
SizeF size = g.MeasureString(lbl.Text, lbl.Font);
// Change size of label if too small
}

setting textBox.MinWidth to largest required space for current culture, font, etc

I'd like my wpf textbox to have a minimum width large enough to accomodate exactly 1 character as it would be drawn depending on the font family, font size, and other properties of the wpf text(box) AND the current culture (could be any). Can I just set the MinWidth property to the FontSize property to achieve this? That's probably too simple to work..
You can use TextRenderer.MeasureText(string, font).
Here is an example:
TextBox textBox = new TextBox();
Size size = TextRenderer.MeasureText("A", textBox.Font);
textBox.Width = size.Width;

Categories