Looking at some old VB 6.0 code we had created a global mFont variable and on Form_Load we had said richTextbox.Font = mFont, then later in code that there is a toolbar button to increase the font size we had just increased the size but did not have to do richTextbox.Font = mFont one more time. It was just doing it, but looks like in C# is it different? each time I change that font size do I have to assign it again so it takes effect? ( assuming still I have assgined richTextBox.Font = mFont at Form_Load event )
Most of the properties in Font are only settable through the constructor, you would need to create a new Font instance and reassign that to the RTB.
Yes that is correct. You need to set the Font property to a new Font object whenever you want to change any aspect of the font.
Related
So I'm making a C# Windows Forms application in which I have a label. This label's size is 100x100 by default*, but I want to automatically increase the label's height so that any string fits in it regardless of its "height". How would I do this? I haven't tried anything myself yet because I don't really have an idea of what to do. I'm just a beginner, after all!
Thanks in advance.
*100x100 is just an example, the real size is different (I'm not sure yet what it'll be)
Thanks everyone, I managed to get what I want by setting the label's MaximumSize property. (Answered by #LarsTech in comments)
AutoSize defaults to true - so the default behaviour of the textbox should resize to any font size/content that is in use.
The label control in Windows Forms is a container which accepts your input strings. Therefore, if you do change its initial values it will get to its content size anyways. So, just change its Text Property.
You could try it first.
here's an example code you can use. It uses the AutoSize property.
If you label is called Label1 you can change it like this:
Label1.AutoSize = true;
Label1.Text = "The text in this label is longer than the set size.";
And it will automatically change it.
C#, Windows Forms app.
We're re-skinning our application and I'm also changing the awful default font we used in the old one. So I thought I'd call the following function to change the font of all controls on a form when the form is loaded.
internal static void SetFonts(Control control)
{
Font oldFont = control.Font;
if (oldFont.Name != GlobalFontName)
{
string familyName = GlobalFontName;
Font newFont = new System.Drawing.Font(familyName,
oldFont.Size, oldFont.Style, GraphicsUnit.Point, 0);
control.Font = newFont;
//oldFont.Dispose();
}
foreach (Control child in control.Controls)
SetFonts(child);
}
I thought it would keep resources down if I disposed of the old font after reassigning the control with the new one, but on closing the form I'm receiving access violation exceptions from one control type from a set of third party controls.
If I comment out the line "oldFont.Dispose()" then I don't get the exception.
Is this a bug of the third party control set or is this to be expected?
Memory wise, can I get away with not explicitly disposing of the old font (The app runs on kiosks for 12hr+ a day) ?
Don't dispose of the old Font, that's the job of the Control whose font you're changing. Also, use tools such as GDIView to monitor your handles (such as fonts).
Control fonts are very weird, as a consequence of the fact that a Font object actually encapsulates two different things:
Information about typeface, typestyle, etc.
A GDI font handle.
The latter aspect of a Font object encapsulates a resource; the former does not. Calling Dispose on a Font object releases the latter, but does not destroy the former.
While I don't think I've seen their behavior "officially" documented anywhere, it seems that the built-in controls are never actually drawn using the Font object that was used to set the Font property; instead, they use the attributes of the assigned Font object to generate a new GDI font object and then use that to draw the control. Although calling Dispose on a font object will make it unusable as an argument to DrawString or other such methods, it will not prevent its use as a "template" for making new font objects.
A consequence of this is that controls don't care if the assigned Font object is disposed (whether before or after it's assigned), nor do they ever dispose it. Reading the Font property from a control will always return the same Font object that was last assigned to it, without regard for whether that Font object was disposed. Consequently, it seems that if nothing will ever read a control's Font property and expect to draw with it, the safest way to assign a control's Font property without a temporary resource leak would be the very-bizarre-looking:
using f = new Font(...)
theControl.Font = f;
That's arguably safer than reading the control's Font property and disposing that before assigning the new value, since the using approach above knows that the Font which is assigned to the control won't be used for anything else, while the latter approach can't know whether the same Font is used by some other code that would object to its disposal.
I really wish MS had documented how Font resources are supposed to be handled. Unfortunately, so far as I know they haven't.
I decided to store the created fonts into a list as they were created and dispose of them during form.Dispose(). But on some windows I got errors doing this too.
What I've just realized is that the third party control must also be changing it's fonts during skinning/painting, so my font in the list was no longer valid for a control and may have been disposed by GC.
So storing the fonts for later disposal doesn't appear to be safe, and I wondered then if I should instead store controls that had had their fonts changed.
By this time the prospect of continuing along this line lost its attractiveness and instead I've manually gone through all 200+ forms in my app with search/replace instead lol
Im not very pleased.
I am trying to perform edit functions in a RichTextBox on a C# (Windows Forms) application.
I would like to be able to select any number of text characters in the script then change targeted font characteristics.
The trouble I have is that each characters font properties may be set to different font. In this case the textbox ignores the event that I request.
How can I solve my problem?
Take a look at this:
Changing font for richtextbox without losing formatting
I think it's the same issue. LarsTech's solution is working perfectly for me.
I have a code to change the size:
RichTextBox1.Font.Size == new System.Drawing.Font(RichTextBox1.Font.Name, yoursize)
And if you want to change only the selected text size:
RichTextBox1.SelectionFont.Size == new System.Drawing.Font(RichTextBox1.SelectionFont.Name, yoursize)
Hope it will help.
I'm using ZedGraph in my c# project.
My X axis has text labels (used for bar chart), but with the default setting of XAxis.Scale.IsPreventLabelOverlap = true every second label is missing. When I change it to false with XAxis.Scale.MajorStep = 1 every label is shown, but font size remains the same, and labels overlap.
Is there any way to change font size of labels ? or preferably switch it to autosize ?
I hate to let you know but as far as I know you cannot change the axis label font size directly. You can change the axis title font size, but not the labels themselves. You can change whether or not they autosize though and scale at which they autosize and it seems that is sort of what you want and that may end up helping you. This is the resource at which I was looking.
Set the PaneBase.IsFontsScaled property to true and then you can change the scale factor by using the PaneBase.ScaleFactor() method. Look through that resource I linked and I think you will be able to get it done. I don't have ZedGraph installed so I can't test it but I'm sure it will be something like that.
Good luck!
my solution is ;
curve.Label.FontSpec = zg1.GraphPane.Legend.FontSpec.Clone();
curve.Label.FontSpec.Size = 6;
I've forgotten about this question long ago.
I've found my own solution, which isn't so clean. I've rewritten the PaneBase.CalcScaleFactor() method by changing return scaleFactor; to something like return scaleFactor * 0.75f;. Now it works as it should.
Let say we have controls c1 and c2
I want c2 to have width of c1 (c1 doesn't have fixed with, It should stretch automatically).
c2.Width = c1.Width;
c2.Width = c1.Width.Value;
These doesn't work.
Can this be done?
Thanks.
Where are you running that code? When you assign to the width, it will assign the value at the time that you run the code. This is an example of a value assignment. If you want C2 to resize whenever c1 resizes, you need to assign it every time C1 changes. You can do this in the control's resize event.
you can folllow this way
c2.Size = New Size(c1.width, c2.height)
Please also make sure that Control.AutoSize property has been set to false before setting new Size. If “true”, it will not resize itself.
Furthermore, some controls like TextBox has not AutoSize property, thus cannot be resized as you wish. Only width of TextBox can be reset successfully.
For more information of specific control’s AutoSize property, please refer to its MSDN document.
Thanks.