Refresh a control - c#

I am trying to change a link label's fore color but the color won't graphically change.
I have a timer that updates the fore color of the control
private void Timer_Tick(object sender, EventArgs e)
{
MyLbl.ForeColor = shouldUpdate? Color.Blue: Color.Gray;
}
The update is successful and while debugging, I can see that the fore color property of myLbl is different. So why doesn't it change it graphically?
I also tried
MyLbl.ForeColor = Color.Gray;
And tried adding Application.DoEvents() after the change of the fore color.
Any solutions?

Unlike vanilla labels, link-labels don't use the ForeColor property in this manner to colour their text.
Use the LinkColor property instead.
Gets or sets the color used when displaying a normal link.
In your case, you need:
MyLbl.LinkColor = shouldUpdate? Color.Blue: Color.Gray;
Note that this not an update problem - you don't have to explicitly call Application.DoEvents (which is almost never the right thing to do) or Invalidate or Refresh to get the link-label to respond to the colour-change.

Related

removing an object from a control box

I want to change the forecolor of radio buttons texts when the Group box Enabled property is set to false. The disabled color is so absurdly dark that you can't read the text in this state. I tried many things (like overriding the system default disabled color mode) without success.
I have no choice but to use this kind of controls, so I'm looking for a workaround. My idea was to superpose a lightly greyed label on the text, but this cause errors when initializing the control box.
I tried the following code to get the label out of the parent control box and avoid this problem:
RadioButtonLabel.Parent= Main_menu.Activeform;
I have no more errors, but the label now basically disappear in runtime.
Any solution?
instead of using dark color, try using this: (this is just an idea code)
if(groupbox.enable == false)
{
radiobutton.text = ""; //it means show nothing
}
else
{
radiobutton.text = "Whatever you like";
}

Why does custom WinForms control not adhere to properties set in constructor when drawn?

I have a custom text box control that inherits from System.Windows.Forms.TextBox.
Basically I set it up so that it automatically checks whether the value entered is a number every time the text changes. Sample code:
public class MyTextBox : TextBox {
public MyTextBox() : base() {
base.TextChanged += MyTextBox_TextChanged;
base.BackColor = Color.White;
base.ForeColor = Color.Black;
}
private void MyTextBox_TextChanged(object sender, EventArgs e) {
try {
int.Parse(base.Text);
base.BackColor = Color.White;
base.ForeColor = Color.Black;
} catch(FormatException) {
base.BackColor = Color.Red;
base.ForeColor = Color.White;
}
}
}
As indicated above, I have the default background and foreground to White and Black respectively, but the WinForms designer draws the component as having a red background, and it comes up that way when I launch the program as well. When I start typing numbers in, however, it does change back to black/white and otherwise behaves as expected.
But why does the control seem to override the properties I set in the constructor? When the control is initially drawn, the text isn't changed... or is it?
When you dropped the control onto the form, its current properties are saved by the form designer.
This includes things like:
ForeColor
BackColor
Text
When you construct the form, these properties are set back to the control.
In the constructor of your control you set up the background color to be white, but you don't change the text, so the text property stays as an empty string.
This empty string cannot be parsed so now we have two possible scenarios:
BackColor is set by the form designer code before Text, in which case the event handler for TextChanged wins. Since the text is an empty string, which cannot be parsed, the background color turns red immediately though it was white for a short while
The opposite happens, Text is set, parse fails, background color is set to red, and then the form designer sets back the saved background color to white.
Control properties are set in alphabetical order (if I'm not mistaken) so the first scenario is the one you see.
In short, you got a couple of ways to handle this:
Decide if an empty string is legal, though possibly with some default value (null perhaps?)
Make sure the constructor of your control assigns it a text that is a legal parsable integer value (0 comes to mind)

How to change the fore color of a disabled multiline TextBox?

I have a multiline Text Box. How can I change the fore color when it is disabled ?
I fond a solution here
Change a textbox colour when disabled C#
But, when MultiLine is true, it is a little bit complicated to find out currently visible portion of the text (i.e. currently visible lines and characters).
So any one know how to play with WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC messages to change the fore color of Text Box in C#.NET ?
OR
How to change the fore color of a disabled Text Box without overriding
OnPaint()
method ?
instead of using enbled property of textbox why dont you use keypress event
private bool isDisabled = false;
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (isDisabled == true)
{
e.KeyChar = (char)0;
}
}
you can change the fore color everytime isDisable = true
Hope it Helps
You can either do:
richTextBox1.ForeColor = System.Drawing.Color.Red
or if you want to use black color, you have to use this trick:
richTextBox1.ForeColor = System.Drawing.Color.FromArgb(0,0,0);
Why don't you make it simple?
Keep the textBox enabled, and set both foreground and background colors as you want them. And if you want that the user can not focus it, then use the event textBox.Focused to detect its focusing, as the event raises, set any other control to focus by programming.

Telerik RadPageView in BackStage mode: howto change selected item color without whole new theme?

I'm looking for a way to set the color of an item in a RadPageView (in backstage mode) at runtime. There are a lot of properties that you can reach via the designer and the "Edit UI elements" section, they work well and they can simply override any theme settings as they are "inline", if you want to put it like that. All I need is to change the background color of the selected item. Creating and applying a custom theme just for that seems a little over the top. Any ideas on how to set this property via code?
You can use the SelectedPageChanging event for this:
void radPageView1_SelectedPageChanging(object sender, RadPageViewCancelEventArgs e)
{
e.Page.Item.BackColor = Color.Red;
e.Page.Item.DrawFill = true;
e.Page.Item.GradientStyle = GradientStyles.Solid;
radPageView1.SelectedPage.Item.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
radPageView1.SelectedPage.Item.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
radPageView1.SelectedPage.Item.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
}

Windows Phone Checkbox Strikethrough/Change fontstyle

I have a list of checkboxes which, when checked, the content (text) should then be strikethrough or even just to change the fontstyle (colour) of the checked item. I'm trying to do this in a checkbox_Tap event handler which is in my codebehind page.
Any ideas as to how I could change the fontstyle or how I could make it strikethough?
The Design Guidelines mention that you should always use the system font unless you have a specific "brand", although there isn't anything forcing you to follow this.
The CheckBox itself, though, has properties you can set in the code-behind for FontFamily, FontSize, FontStretch, FontStyle, Foreground, etc.
In your CheckBox_Tap event handler, you could do something like:
void CheckBox_Tap(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
cb.FontFamily = //<your new font here>
cb.FontStyle = // <new style here>
}
As for Strikethrough - it doesn't look like there is a built in way to do that (there isn't a specific property for Strikethrough that you can just set to True, for example).
However, I did run across this post, and there is an answer that shows a hacky way to do this in XAML (which I haven't tried) using a Border. I'm wondering if you could try this, and wrap your CheckBox in a Border which is invisible by default, and then make it visible when you want the Strikethrough?
Hope this helps!

Categories