I'm new to C# language. So I've created a form in Visual Studio and there are 3 checkboxes on it named Bold, Italic and Underlined. There is also a textbox. When I check the "Bold" checkbox, it makes my text bold and all the other checkboxes work like this but when I try to check 2 or all of them, only one of them works. Here's the code I wrote for making the text both bold and italic by checking the first and second checkbox and absolutely doesn't work:
if (checkBox1.Checked == true && checkBox2.Checked == true)
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold);
textBox1.Font = new Font(textBox1.Font, FontStyle.Italic);
}
I also have a combobox for font size and it works well but when I check the "Bold" checkbox and then change the font size, it goes back to regular font style and doesn't stay bold.
What should I do?
You're setting the font twice rather than combining the values. If you look at the definition of FontStyle you will see that it is a bitfield (it has the flags attribute). Just OR the values. (Note that we use the bitwise OR rather than the Boolean OR)
{
textBox1.Font = new Font(textBox1.Font, FontStyle.Bold | FontStyle.Italic);
}
It would, however, be better to structure your code slightly differently :
FontStyle style;
if (checkBox1.Checked)
{
style = style | FontStyle.Bold;
}
if (checkBox2.Checked)
{
style = style | FontStyle.Italic;
}
textBox1.Font = new Font(style);
Related
I want to make part of the text bold in a textbox, for example the textbox contains.
"This is a text box"
So it will be "This is a text box"
How can I do it in C# Windows Forms?
You can do this with the help of FontStyle interface.
Just add a button in your form and name it Bold and create a click event for that.
You have to use RichTextBox for this, you cannot do this with TextBox.
This code will convert the selected text to bold.
private void btnBold_Click(object sender, EventArgs e)
{
FontStyle style = tbMessage.SelectionFont.Style;
if (tbMessage.SelectionFont.Bold)
{
style = style & ~FontStyle.Bold;
btnBold.Font = new Font(btnBold.Font, FontStyle.Regular);
}
else
{
style = style | FontStyle.Bold;
btnBold.Font = new Font(btnBold.Font, FontStyle.Bold);
}
tbMessage.SelectionFont = new Font(tbMessage.SelectionFont, style);
tbMessage.Focus();
}
You cannot do it in a standard TextBox control, you need to use a RichTextBox control with appropriate formatting.
To be clear, you cannot do that in a TextBox. Use a RichTextBox.
In a RichTextBox, start by selecting the desired text by setting the SelectionStart and the SelectionLength properties or let the user select text interactively. Then apply a formatting by setting one of the Selection... properties:
richTextBox1.Text = "This is a text box";
richTextBox1.SelectionStart = 5;
richTextBox1.SelectionLength = 2;
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold);
I load font.size in a file that this format is string and i want to set textbox.font.size by
this value but say "this value is readonly not set"
how i can set font.size in coding?
By Using this it is possible to programmatically choose the best font. This also allows you to set different sizes on the various alternative fonts.
Font font = new Font("Times New Roman", 16.0f,
FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
textBox1.Font = font;
For more details, check here
You can Set the Font Property of TextBox Control.
Font Property of TextBox Control Expects Font Class Object.
you can create the Font class oject with different styles by passing different parameters to its constructors.
Font Class Constructor Description :
FontFamily - FontFamily (EnumType) : used to specify Font name
ex:Arial,Times New Roman etc.,
FontSize - float(DataType) : it's a float value of font size.
FontStyle - FontStyle (EnumType) : it is a FontStyle of different
types ex: FontStyle.Regular,FontStyle.Bold,FontStyle.Italic etc.,
Now See sample Example:
Font fnt=new Font(textBox1.Font.FontFamily,12.0F);//Edit your size asper your requirement. it's float value
textBox1.Font = fnt;
Create new font from current font (use it as prototype) and provide font size (parse your string to float):
textBox1.Font = new Font(textBox1.Font, Single.Parse(sizeString));
You have to set it at start of the Initialization of textbox
like
var textbox = new TextBox()
{
FontFamily = "Segoe WP",
FontSize = 18
};
Font.Size is read only. You must set the Font object itself.
My first assignment is to create a program that can dynamically change the text color, alignment etc of a string a user has entered in a text box. Here's my problem: first of all my bold and underline button work, but not the italic one:
label5.Font = new Font(label5.Font.Name, label5.Font.Size, label5.Font.Style ^ FontStyle.Italic);
Secondly, I Have to use Radiobuttons to change my text color, and have managed to do this button per button but I wanted to make it more efficient by making a single procedure that would use my radiobutton's name to change the font, here's what I mean:
protected void Colorchange(object sender, EventArgs e)
{
RadioButton selectedRadioButton = (RadioButton)sender;
selectedRadioButton.Name = sender.ToString();
label5.ForeColor = Color.???????; <---Can't figure how to put the name string here....
}
Changed, due your comment:
label5.ForeColor = System.Drawing.Color.Red
//or other option:
label5.Style.Add("color", "Red");
change italic:
label5.Font.Italic = true;
//or other option
label5.Style.Add("font-style", "italic");
the second option in case you want to pass string as you described.
What you search is:
Color red = Color.FromName("Red");
Color blue = Color.FromName(label5.Name);
To change the color of text in TextBox use following code
textBox1.ForeColor=System.Drawing.Color.Green // or choose any color from dropdown
You can do same fo label.
label5.ForeColor=System.Drawing.Color.Red // or any color
I feel like a real noob posting this, but I can't seem to find anything for this...
I have a control that I'm basically trying to toggle the fontstyle between bold and not bold. This should be simple...
However, you can't acccess the Control.Font.Bold property as it is read only, therefore, you need to change the Font property.
To make it bold, I just do this:
this.btn_buttonBolding.Font = new Font(this.btn_buttonBolding.Font, FontStyle.Bold);
Not ideal, but it works. However, how do I go about removing this bold style (once it is bold already)?
I looked hard for duplicates; closest I could find was this, but it doesn't quite answer my situation:
Substract Flag From FontStyle (Toggling FontStyles) [C#]
And this which gives how to set it, but not remove it: Change a font programmatically
Am I missing a simple constructor for the font that could do this? Or am I just missing something easier?
I know this is a bit old, but I was faced with the exact same problem and came up with this:
Font opFont = this.btn_buttonBolding.Font;
if(value)
{
this.btn_buttonBolding.Font = new Font(opFont, opFont.Style | FontStyle.Bold);
}
else
{
this.btn_buttonBolding.Font = new Font(opFont, opFont.Style & ~FontStyle.Bold);
}
The magic is in the "~" which is the bitwise NOT. (See the MSDN KB article "~Operator")
VB.NET version:
Dim opFont As Font = me.btn_buttonBolding.Font
If (value)
me.btn_buttonBolding.Font = new Font(opFont, opFont.Style Or FontStyle.Bold)
Else
me.btn_buttonBolding.Font = new Font(opFont, opFont.Style And Not FontStyle.Bold)
End if
The FontStyle enum contains 5 distinct values.
The one that reset your previous set is FontStyle.Regular
Regular Normal text.
Bold Bold text.
Italic Italic text.
Underline Underlined text.
Strikeout Text with a line through the middle.
It's a bitwise enum where Regular is 0. So setting this value alone reset all other flags
Try this:
private void btn_buttonBolding_Click(object sender, EventArgs e)
{
var style = btn_buttonBolding.Font.Bold ? FontStyle.Regular : FontStyle.Bold;
btn_buttonBolding.Font = new Font(this.Font, style);
}
I'm using the following code to make my treenodes bold:
Font font = new Font(tvQuestionSequence.Font, FontStyle.Bold);
foreach (QuestionnaireBuilder_Category cat in categories)
{
TreeNode node = new TreeNode();
node.Text = cat.Description;
node.Name = cat.Id.ToString();
node.NodeFont = font;
tvQuestionSequence.Nodes.Add(node);
}
But the text of the bold nodes is not displayed correctly. The last letter(s) are not shown. How come? And how to solve this problem?
I found this Post when searching through the web because I am facing the exact same problem.
However, appending a white space to the end of the node was not an option, and I found an alternative way that seems to fix the issue.
After setting my node font Bold, all I need to do is reset the node text with the same value.
Here is the Code Sample:
Font boldFont = new Font(treeview.Font, FontStyle.Bold);
node.NodeFont = boldFont;
node.Text = node.Text;
It seems that the node is redrawn after changing the text, which is exactly what I wanted in the first place.
I've found that this is a Windows issue. A workaround for this problem is this:
In the form constructor set the font of the treeview to bold. When adding nodes which must not be bold, change the font to regular:
// Constructor of your form
public Form()
{
InitializeComponent();
Font font = new Font(tvQuestionSequence.Font, FontStyle.Bold);
tvQuestionSequence.Font = font;
}
// Add regular nodes (not bold)
Font font = new Font(tvQuestionSequence.Font, FontStyle.Regular);
TreeNode treeNode = new TreeNode();
treeNode.Text = "Foo";
treeNode.NodeFont = font;
TreeNode parent = tvQuestionSequence.Nodes.Find("parent", true);
parent.Nodes.Add(treeNode);
Simply use treeView.BeginUpdate() before you bold the node then treeView.EndUpdate() after you've bolded the node.
This is a known Windows bug.
The simple solution is just to append an extra space character at the end of your strings. The space character will not be visible, but it will increase the number of pixels needed to draw the string, so the entire string will be visible.
This is all not helping for me.
What DID the trick is making the font a little bigger and bold at DESIGN time.
(In the Properties window)
So make sure you define the treeview with big enough font, then later you can add nodes with smaller font. They will fit.
I do agree with the solution provided. I just want to add to it to shed a little more light on what the problem is.
The treeview has its own font which determines the width of items at the root level. That compensates for the fact that there is only an item height property available and no item width property.
The solution to your problem is to determine what the font of your root node should be, then set the tree to that same font. You can do that at design time also.
Hope that helps someone.
A workaround for this problem is this:
Set the defaul font of treeview to bold in the properties.
And chnage to not bold when you need.
I do the following, I set the DrawNode Event to call, it sets the node to bold and removes the highlighted colour.
You can set any colour you like using the first parameter of the e.Graphics.FillRectangle function.
private void SetNodeBoldWhenSelected(object sender, DrawTreeNodeEventArgs e)
{
if (e.Node == null) return;
var font = e.Node.NodeFont ?? e.Node.TreeView.Font;
if (e.Node.IsSelected)
{
font = new Font(font, FontStyle.Bold);
}
var bounds = new Rectangle( e.Bounds.X,e.Bounds.Y,e.Bounds.Width+20,e.Bounds.Height);
e.Graphics.FillRectangle(SystemBrushes.ControlDarkDark, bounds);
TextRenderer.DrawText(e.Graphics, e.Node.Text, font, bounds, SystemColors.HighlightText, TextFormatFlags.GlyphOverhangPadding);
}
Now when I select a node I get 20 pixels more space, for my font, this works well, one can calculate the "real" size needed but there is no specification stating it needs to do this but you can use Graphics.MeasureString if you feel you need to do that.
Very easy and works fine
treeView1.SelectedNode.NodeFont = new System.Drawing.Font(treeView1.SelectedNode.TreeView.Font, treeView1.SelectedNode.TreeView.Font.Style | FontStyle.Bold);
this.treeView1.SelectedNode.Text += string.Empty;
I realize this is an old thread and it may have been answered. I just ran across this problem as I'm learning to use TreeViews. What worked for me was changing the font size for the entire TreeView to the same size, or bigger than the font of the level you want to bold. The default font size is 8.something. I changed mine to 10, which was the size I wanted my nodes, and the truncating was gone.
What worked for me: Hooking into the load event in the Control's constructor and tweaking the node as explained in BlunT's answer.
public MyControl()
{
InitializeComponent();
_head = new TreeNode();
this.Load += (s, e) =>
{
trvParts.Nodes.Clear();
_head.NodeFont = new Font(trvParts.Font, FontStyle.Bold);
trvParts.Nodes.Add(_head);
_head.Text = "Node Name";
};
}
It's in vb.Net however the solution to re-enter the value of the TEXT field gets around this nicely. As in:
With myNode
Dim myText As String = .Text 'capture the text
.NodeFont = New Font(<name of your treeview>.Font, FontStyle.Bold)
.Text = myText 'reset the text to the original value
End With
Based on MSDN Library, try change your code to:
Font font = new Font(tvQuestionSequence.Font, FontStyle.Bold);
foreach (QuestionnaireBuilder_Category cat in categories)
{
TreeNode node = new TreeNode();
node.Text = cat.Description;
node.Name = cat.Id.ToString();
node.NodeFont = font;
tvQuestionSequence.BeginUpdate(); //added newline here <--
tvQuestionSequence.Nodes.Add(node);
tvQuestionSequence.EndUpdate(); //added newline here <--
}
It work for me