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
Related
I'm trying to change the color of a text in a label per condition.
I have tried the following option but none of them works:
if (gameNameTXT.Text != "")
{
pubName.ForeColor = System.Drawing.Color.Green;
pubName.ForeColor = System.Drawing.Color.FromArgb(11, 102, 35);
pubName.Attributes.CssStyle.Add("color", "green");
}
(pubName in the ID of the label)
I have located the snipped code in the Page_Load section and/or the Page_Init section.
What I want is that the color of the text changes to green if the user wrote something in the text box.
do you try on onChange event of gameNameTXT?
may be you need set AutoPostBack=true
I have 2 comboboxes for the font and the fontsize. When I click them it changes the font size or the font in my richtextbox. Now I want it to work like in word. If the line you just moved to is in a different font or size. It should detect that and change the comboxes to match the font and size of the current line. Somoeone else asked this same question and got a result which didn't work for me. It was as follows
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
MessageBox.Show("we got here"); // this is my added part to let me know if the code is even getting executed. It is not.
richTextBox1.SelectionStart = 1;
richTextBox1.SelectionLength = 1;
comboBox1.Text = richTextBox1.SelectionFont.ToString();
comboBox2.Text = null;
comboBox2.Text = richTextBox1.SelectionFont.Size.ToString();
}
I held out hope that it was my answer but I could not see how SelectionFont would make any difference when nothing was selected. Also the richTextBox1_SelectionChanged event seems to not be being called when I move through the document with the up/down arrows. The problem is not with the comboboxes, the problem is that as I arrow through my document I need to be able to know what font and size it is at the caret position so it can fire an event to change the combo boxes to match.
The code that you are using will always make the selection from character at index 1 and are of the length 1. instead for that you need to use which will give you the the following code without specifying the selection(so it will take the selection from the ritchTextBox).
string fontName = richTextBox1.SelectionFont.Name;
float fontsize = richTextBox1.SelectionFont.Size;
You should save the values for the new comboBox position temporarily in variables, otherwise if you do it directly
comboBox1.SelectedIndex = comboBox1.FindStringExact(richTextBox1.SelectionFont.Name);
the comboBox1_SelectedIndexChanged event will be immediately called and could affect the results.
So just try:
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
int comboBox1Index = comboBox1.FindStringExact(richTextBox1.SelectionFont.Name);
int comboBox2Index = comboBox2.FindStringExact(richTextBox1.SelectionFont.Size.ToString());
comboBox1.SelectedIndex = comboBox1Index;
comboBox2.SelectedIndex = comboBox2Index;
}
I adapted Sujith's solution and half of Markus's solution and came up with the following which works just fine for me:
Private Sub Description_SelectionChanged(sender As Object, e As EventArgs) Handles Description.SelectionChanged
Dim fontName As String = Description.SelectionFont.Name
Dim fontSize As Single = Description.SelectionFont.Size
tbSelectFont.Text = fontName
tbSelectSize.Text = fontSize
End Sub
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 need some code to convert standard C# TextBox to temperature TextBox which means adding "°C" to end of the text in the textbox with another color than the default color.
To get the degree symbol you can use character code 176 e.g.
Char degree = (Char)176
You can then append this to your textbox content or I would just add a label to the right of the textbox with the degree symbol if you want to control the forecolor easily.
TextBox is a plain text editor. To get different colours you would have to muck around with a rich text box. Why not put the "°C" in a label positioned to the right of the text box? That would also make your parsing and rendering code much easier.
You could probably create your own control which inherits from TextBox and then override Text property to automaticaly add °C though other color inside the same TextBox could be problem.
Why you want to have °C in TextBox ?
Can't it just be label right after TextBox ?
You can set static text and color to what you want.
The other solutions proposed here are probably sufficient for your application; however, if you had the need to implement this with re-usability in mind, here is a custom control solution which you may extend to better suit your application:
public class TemperatureTextBox : ContainerControl
{
private const int BORDER_SIZE = 1;
// Exposes text property of text box,
// expose other text box properties as needed:
public override string Text
{
get { return textBox.Text; }
set { textBox.Text = value; }
}
private TextBox textBox = new TextBox()
{
Text = string.Empty,
BorderStyle = BorderStyle.None,
Dock = DockStyle.Fill
};
private Label label = new Label()
{
Text = "°C",
TextAlign = ContentAlignment.MiddleCenter,
Size = new Size()
{
Width = 32
},
BackColor = SystemColors.Window,
Dock = DockStyle.Right
};
public TemperatureTextBox()
{
this.BackColor = SystemColors.Window;
this.Padding = new Padding(BORDER_SIZE);
this.Controls.Add(label);
this.Controls.Add(textBox);
this.PerformLayout();
}
// Constrain control size to textbox height plus top and bottom border:
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.Height = (textBox.Height + (BORDER_SIZE * 2));
}
// Render a border around the control:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawRectangle(
SystemPens.ControlDarkDark,
new Rectangle()
{
Width = (this.Width - BORDER_SIZE),
Height = (this.Height - BORDER_SIZE)
});
}
}
Simply create a new class and drop this code in and rebuild you solution. It will create a new TemperatureTextBox control in the toolbox which can be dropped onto a new form and visually designed.
This example exposes the Text property of the underlying text box by overriding the custom control's text property. You may want to expose other properties, and events depending on what your application needs to accomplish.
I want to have some labels on a form with the same font color as the caption on my group boxes, and furthermore I want these colors to change if the user has applied a different Theme on their system.
Can I do this without changing the GroupBox caption from its default?
UPDATE:
I have tried setting the Label ForeColor to ActiveCaption, this looks okay for the Default (Blue) scheme, but when I change the scheme to Olive Green, the label and group box captions are not the same.
Also, the GroupBox normal behaviour is that setting the FlatStyle to Standard sets the caption colour to ForeColor, however to create a new GroupBox and set its ForeColor to ControlText, you must first set it to something other than ControlText and then set it back again. (If you don't follow what I mean, then try it and see.)
Hmm, same question? I'll repeat my post:
using System.Windows.Forms.VisualStyles;
...
public Form1()
{
InitializeComponent();
if (Application.RenderWithVisualStyles)
{
VisualStyleRenderer rndr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal);
Color c = rndr.GetColor(ColorProperty.TextColor);
label1.ForeColor = c;
}
}
The label exposes a ForeColorChanged event. You can then do something like this:
this.label1.ForeColorChanged += (o,e) => { this.groupBox1.ForeColor = this.label1.ForeColor;};
If however you're trying to detect when the user changes their Theme, you can hook into the SystemEvents which can be found in the Microsoft.Win32 namespace. Something like this:
Microsoft.Win32.SystemEvents.UserPreferenceChanged += new Microsoft.Win32.UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
void SystemEvents_UserPreferenceChanged(object sender, Microsoft.Win32.UserPreferenceChangedEventArgs e)
{
this.groupBox1.ForeColor = this.label1.ForeColor;
}
I assume you use Windows Forms and not WPF. When you apply colors use the system colors (e.g. Control or HighlightText) these will be changed when the user switch the windows theme. Here is the code to set the color of the group box to system color and then apply this color for a label:
groupBox1.ForeColor = SystemColors.ActiveBorder;
label1.ForeColor = groupBox1.ForeColor;