Set Font.Bold on inherited label - vb.net

I have to set .Font.Bold = True on label which havent defined .Font property but inherite Font (name, size, style) from form. For that I erases her .Font property from form's designer file.
Now I need to set text of this label to be bold without defining font name, size etc. for this label.
I try:
label6.Font.Bold = True
But this don't work (Property .Font.Bold is readonly).
If I set font for this label like:
label6.Font = New Font(myfontname, 10, FontStyle.Bold, GraphicsUnit.Point)
then I get bold text but label then don't inherite form's font size anymore.
Is here possible to keep form's font inheritance to label but get bold text on such label?

No, as you've already discovered the Font.Bold property is read-only. Font objects are immutable, meaning that their properties cannot be changed once they have been created. The only way to modify this property is to create a new Font object.
When creating this new Font, you can certainly copy the properties of an existing Font object (like the one used by your form), but there is no way to dynamically couple the two Font objects together. If the font size used by your form changes, a new Font object will be created with that new size for the form, but your custom bold Font object will not be updated.
The only thing that makes this confusing is that there is a bit of magic that goes on if you do not set a custom font for child controls. They automatically inherit the font of their parent (a container control, such as a form). Such properties that retrieve their value from their parent when they have not been explicitly set are referred to as ambient properties. Ambient properties are explained in the documentation where applicable. But the upshot is that the ambience goes away at the point where you explicitly set the property. So forget about that.
To achieve what you want, we need to get a notification when the form's font size changes and in response, you can create a new bold Font object with the new size for your Label control. Luckily, there is just such a mechanism in the form of the FontChanged event. Handle the FontChanged event for your form, and in response, create a new Font object for your Label control. For example:
Private Sub myForm_FontChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myForm.FontChanged
Dim oldFont As Font = myLabel.Font
myLabel.Font = New Font(myForm.Font, myForm.Font.Style Or FontStyle.Bold)
oldFont.Dispose()
End Sub
Although, I'm not sure if/why this is really necessary. It's rare that the font size of a form gets changed while the application is running. Generally that happens only at creation, in which case when you retrieve the value to create the custom Font object for your Label control, it would already be set correctly.

Related

How to change TextBox.text font style to Italic

I have a textbox control called tb_remarks ,tried to make texts written in tb_remarks using
tb_remark.Font.Italic = True but getting read only exception.
Note : I don't want to change control's property using designer
Use the Font-constructor and assign it to the textbox:
Dim font = New Font(tb_remark.Font, FontStyle.Italic)
tb_remark.Font = font
By passing the old Font you use all other properties as prototype.
You should initialize new style for the font as per Control.Font Property and assign italic-style
tb_remarks.Font = New Font(Font, FontStyle.Italic)

Opacity of Panel over Awesomium WebControl

I've got a normal Winforms Panel, but under it, I've sent a (Awesomium) WebControl form to the back, and was wondering if it's possible to make the panel transparent to reveal the colors of the webpage basically, but simple solutions such as
Panel1.BackColor = FromARGB(0,0,0,0) (Or any other color)
make the backcolor the same as the Form's backcolor (Goes right through the WebControl). Is there any workaround? My original intent was to see if the panel could display the color under of the webpage, rather than the Form's backcolor.
Look, you have not explained your problem properly, so I can't tell exactly what problem you are getting.
Panel's color is by default transparent but for your convenience you can set the background as transparent.
Here is the code:
Panel1.BackColor = Color.Transparent
Or, if your form's backcolor is a plain color, you can try the following:
Panel1.BackColor = Form1.BackColor
Hope it works perfectly!

How do I make a group box's text bold but not the text of controls contained in it?

I went and created a tab containing a good amount of controls, most of which are contained within what I'll just call the top-level group box. Now I decide I'd like the text of the top-level group box to be bold, but nothing else. When I set the top-level group box's font to bold, however, all of the controls contained within it become bolded as well, which is what I don't want. I can set each individual control's bold property to false, but it seems like there should be an easier way to do this. Any ideas?
I'm probably missing something obvious, like a group box property that is staring me in the face--and apologize if this turns out to be the case.
Thanks in advance for any help.
You could bypass the problem by placing a label over the caption for the GroupBox, but I wouldn't necessarily recommend this.
A better solution emerges once you understand what is happening and why it is happening. The issue is that a control's font (among other things) is an ambient property, meaning that child controls inherit their parent/container control's properties. So if you set the GroupBox to use a bold font, all of its child controls automatically inherit the bold property by default.
The key there is, of course, by default. Ambient properties only apply if you don't explicitly set the properties of the children to something else. If you don't want the child controls to be bold, select them all and turn off bold. The settings of the parent/container will no longer override the new custom settings.
To make things even easier, you can add a Panel control to your GroupBox, dock/anchor it to fill the entire client area of the GroupBox control, and set it to use a standard, non-bold font. Then, the rules of ambient controls stipulate that the child controls you add to the Panel will not be bold by default. This way, you only have to change the font property of one control as opposed to every child control that you add to the GroupBox.
The reason that this is better than trying to add a Label control over the GroupBox caption is because a GroupBox is designed to contain controls. You can take advantage of the docking and anchoring properties to make sure that everything gets arranged correctly, and you won't have to fight the designer when doing so to make sure that your custom Label correctly covers up the default label drawn by the GroupBox control. Additionally, you won't run into Z order issues or have other redrawing problems rear their ugly heads at runtime when, for example, the Label control gets accidentally hidden behind the GroupBox and no one can see it (and a host of other potential snafus).
I came across this old question when searching for the same, and realised it could be solved in code without adding a separate control just to overcome the ambience issue that Code Gray mentions in his answer.
Add an extensions in a module like so:
<Extension()>
Public Sub UnBold(Of T As Control)(cc As Control.ControlCollection)
For Each c As Control In cc
If Not TypeOf c Is T AndAlso c.GetType.GetProperty("Font") IsNot Nothing Then
Dim RegularFont As New Font(c.Font.FontFamily, c.Font.Size, FontStyle.Regular)
c.Font = RegularFont
ElseIf c.HasChildren Then
UnBold(Of T)(c.Controls)
End If
Next
End Sub
Then unbold all the controls in all the GroupBoxes on a form (including any child GroupBoxes) by using as follows in the form's OnLoad event:
Me.Controls.UnBold(Of GroupBox)()
Or for all controls in a single GroupBox (again, including any child GroupBoxes):
MySpecificGroupBox.UnBold(Of GroupBox)()
With the proviso that if you actually want control within the GroupBox to actually stay emboldened you will have to set that in code after calling the extension.
Consider bypassing the problem by placing a label over the GroupBox's text area and make the label's font bold.
I did it once and even used a CheckBox (for enabling/disabling the whole group). Worked like a charm.
Place all of your controls inside of a ContentControl and reset the font parameters
<GroupBox Header="Group" FontSize="16" FontWeight="Bold">
<ContentControl Margin="0" FontSize="12" FontWeight="Regular">
...
...
...
</ContentControl>
</GroupBox>
Programatically you can do it in order. Assume you want to make font style bold in groupbox but not in child controls. First set the font to a new Font in child controls, in this case you can pass groupbox font property. Then change groupbox font style to bold.
var grpBox = new GroupBox()
{
Text = "",
Width = 780,
Height = 70,
Parent = panel1,
Dock = DockStyle.None,
AutoSize = false,
Visible = true,
Location = new Point(20, grpY)
};
var label = new Label()
{
AutoSize = true,
Parent = grpBox,
Enabled = true,
Name = "label" + btnNum++,
Location = new Point(5, 50),
Text = "",
Font = new Font(grpBox.Font, FontStyle.Regular)
};
var txtBox = new TextBox()
{
Width = 550,
Height = 23,
Location = new Point(65, 20),
Name = "txtBox" + btnNum++,
Parent = grpBox,
Enabled = true,
Tag = label,
Font = new Font(grpBox.Font, FontStyle.Regular)
};
grpBox.Font = new Font(grpBox.Font, FontStyle.Bold);

ContextmenuStrip Width

I need to change the contextmenustrip width dynamically, by default the contextmenustrip width depends on the text length of the ToolstripmenuItems.
And BTW I really don't wanna redraw the control again!!!
Thanks in advance.
You need to set the ContextMenuStrip AutoSize property to false. Then you can set the Width property to whatever you want. When AutoSize is set to true, the Width property is ignored and is calculated dynamically.
Example:
Dim menu As New ContextMenuStrip()
menu.AutoSize = False
menu.Width = 100
AutoSize does a VERY poor job of guessing at the "correct" size anyway.
When TRUE, the menu is far wider than any of the text would need it to be.
When you set it manually... you also have to set the HEIGHT to be far less than you would expect... if you want it to display in the correct size menu.

Bold text for a tab control

I'd like to bold the text for a tab page under certain conditions (not, necessarily, GotFocus). Is it true the only 'er easiest way to do this is by overriding the DrawItem event for the tab control?
http://www.vbforums.com/showthread.php?t=355093
It seems like there should be an easier way.
Like ...
tabControl.TabPages(index).Font = New Font(Me.Font, FontStyle.Bold)
That doesn't work, obviously.
When you set the Font property on a TabPage, you are setting the default font for all controls on that tab page. You are not setting it for the header, however.
When you execute the following code:
tabControl.TabPages(index).Font = New Font(Me.Font, FontStyle.Bold)
Any controls on that page will now be bold by default, which is not (I'm assuming) what you want.
The header's font (that is, the tab itself) is controlled by the TabControl's Font property. If you were to change your code to:
tabControl.Font = New Font(Me.Font, FontStyle.Bold)
You will see that in action. However, it changes the font for all the tabs on display, which is also not, I'm assuming, what you want.
So, using the default WinForms tab control, you are (I believe) limited to the technique in the link you've posted. Alternatively, you can begin looking at 3rd-party controls, such as those discussed in these questions on StackOverflow.
An easy way to give tab controls different labels depending on a field value is to change the caption itself:
For example:
Private Sub Form_Current()
If IsNull(Me.Subform.Form.Field_Name) Then
Me.Tab_Name.Caption = "Tab One"
Else
Me.Tab_Name.Caption = "Tab One +++"
End If
End Sub
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Font BoldFont = new Font(tabControl1.Font, FontStyle.Bold);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, BoldFont, Brushes.Black, e.Bounds);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
}