Put bold font on the title of your tabcontrol c #
I have this tab control:
I need to put the title of the Notes tab in bold, but I donβt know how.
I tried this code:
tabControl2.Font = new Font(this.Font, FontStyle.Bold);
However, all tabs are in bold. Then I tried this:
tabControl2.TabPages["Notes"].Font = new Font(this.Font, FontStyle.Bold);
I've also tried: How to make the title text of the TabPage bold?
Graphics g = e.Graphics;
Brush _TextBrush;
// Get the item from the collection.
TabPage _TabPage = tabControl2.TabPages["Notes"];
// Get the real bounds for the tab rectangle.
Rectangle _TabBounds = tabControl2.GetTabRect(1);
_TextBrush = new System.Drawing.SolidBrush(e.ForeColor);
// Use our own font. Because we CAN.
Font _TabFont = new Font(e.Font.FontFamily, (float)9, FontStyle.Bold, GraphicsUnit.Pixel);
// Draw string. Center the text.
StringFormat _StringFlags = new StringFormat();
_StringFlags.Alignment = StringAlignment.Center;
_StringFlags.LineAlignment = StringAlignment.Center;
g.DrawString(tabControl2.TabPages["Notes"].Text, _TabFont, _TextBrush, _TabBounds, new StringFormat(_StringFlags));
However, it puts all the contents of the tab in bold, not the title. I don't know how to put the title of this particular tab. Anyone have an idea?
+3
source to share
1 answer
Hope I can help you.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl.DrawItem += TabControlOnDrawItem;
}
private FontStyle HasNotification(string tabText)
{
return tabText.Equals("Notes") && true
? FontStyle.Bold
: FontStyle.Regular;
}
private void TabControlOnDrawItem(object sender, DrawItemEventArgs e)
{
var tab = (TabControl) sender;
var tabText = tab.TabPages[e.Index].Text;
e.Graphics
.DrawString(tabText
, new Font(tab.Font.FontFamily
, tab.Font.Size
, HasNotification(tabText))
, Brushes.Black
, e.Bounds
, new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
});
}
}
0
source to share