How to execute code the second a checkbox is checked? C# - c#

When I usually work with checkboxes, I check to see if the box is checked with the code below:
if (checkBox1.Checked)
{
Label1.BackColor = Color.Red;
}
This code is usually attached to a button that sets into motion when it's clicked by the user. This time, however, I want to do something, like change the color of a label, the very SECOND the checkbox is checked by the user. That is, I don't want to wait until the user pushes some other button to check if the checkbox is checked in order for the label's color to change.
How do I do this?

Sounds like you need a CheckedChanged event handler. That one's for ASP.Net, but there's a version for winforms as well (and xaml, and so on.)

Then you have to write code inside CheckedChanged Event of checkBox as below:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
label1.ForeColor = Color.Red;
}

Why don't you hook up to the CheckChanged event and implement your ideas there?

Related

What Event for ComboBox to change text color while typing

I'm programming in WinForms.
I have a ComboBox set with an initial gray ForeColor. My goal is to change the text color of this ComboBox when the user start to type something.
I tried to use _TextChanged and TextUpdate Events but don't work.
private void ComboBox1_TextChanged(Object sender, EventArgs e)
{
ComboBox1.ForeColor = SystemColors.ControlText;
}
I already used the Event _SelectedIndexChanged to change the text color when the user select an item from the drop-down list, and it works well, but the text remains gray if the user types something (there is an AutoCompleteCustomSource collection associated to the ComboBox so the user can write instead to use the drop-down list).
Any suggestion?
EDIT
I have solved this way:
Registering in Form1.Designer.cs:
this.ComboBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.ComboBox1_KeyUp);`
Using this code:
private void ComboBox1_KeyUp(Object sender, KeyEventArgs e)
{
ComboBox1.ForeColor = SystemColors.ControlText;
}
Use KeyUp, KeyDown, or KeyPress events fired by the textbox. You probably don't want KeyPress for this purpose. TextChanged fires when the text has already been changed, which is why the new character being typed doesn't have a different color when you subscribe to it.
Have you looked at KeyUp event? MSDN Keyup
You should be able to subscribe to this event and do whatever you want inside it.

What event should I use to something when I select any text in a RichTextBox in WinForms?

I want to perform some action when some text is selected from a RichTextBox. So in what event of the RichTextBox should I write my logic?
I'm designing a notepad. I have one Menustrip and in the Menustrip I have Copy, Paste, Undo and Redo option. I want these options to be visible to the users only when a user selects any text in the RichTextBox.
I've tried it in many events but haven't succeeded so far.
I've tried in RichTextBox the Mousecapturechanged event and Toolstripmenu menu active event.
I am using C#.
Kindly help.
The best solution would be to subscribe to the MouseUp event of the RichTextBox and within the event handler, check if the length of the selected text is greater than 0 like this:
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
if (richTextBox1.SelectedText.Length > 0)
{
// Show the Copy, Paste, Cut Buttons...
}
}
This is because the SelectionChanged event fires whenever the selection is changed and so will not even let you select text properly.
You can use SelectionChanged event and check if the cursor has changed or a part of text has been selected.
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength < 2) return;
//Show the menu
}
You can use the SelectionChanged event which:
Occurs when the selection of text within the control has changed.
MSDN Link - RichTextBox.
This event would be ideal for what you aim to achieve.
Now you also need to use this properly. This event will fire even if you type something. So you have to verify that something is selected like this:
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectedText.Length > 1)
{
// Show the Copy, Paste, Cut Buttons...
}
}

When combobox changes do something ( C# )

I've been trying to make a program with 3 comboboxes where depending on what you pick different things happen.
Here is a screenshot of what I'm stuck with.
The only thing missing in the screenshot is the following which is in the private void Form1_Load event
cBxColor1.Items.Add("Black");
cBxColor2.Items.Add("Black");
cBxTest.Items.Add("Something");
In the screenshot above I try two methods to write something in the textbox. One whenever the text changes and then checks for the choosen item. In this case Something, Black and Black. I'm planning on adding more later but so far I'm trying to get this to work with one.
The original plan was to have while(the selected texts in the comboboxes are Something, Black and Black) then add some text to the textbox if that is true.
Screenshot of the error I get when trying the other method, I'm not sure what this means.
I've googled and searched around for a solution but I truly couldn't find anything that would help to solve my problem. I would appreciate if the 1337 hax0rz on here would help me out.
TextChanged is a Event. Use it in a methode like this:
private void ComboBox_TextUpdate(Object sender, EventArgs e)
{
//Your code here
MessageBox.Show("You are in the ComboBox.TextUpdate event.");
}
Add the event with += to your combobox in your initialisation:
ComboBox.TextUpdate += ComboBox_TextUpdate;
So at every TextUpdate your Methode ComboBox_TextUpdate gets called and you can code there.
Instead of using the if condition to see if the text has changed, you should use the ComboBox event SelectedValueChanged.
To create that event right-click on your ComboBox and select properties. Select "Events" and double-click the textbox next to the SelectedValueChanged event.
Then you want to check the values of each ComboBox like you did.
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (cBxColor1.SelectedText.Equals("Black") || cBxColor2.SelectedText.Equals("Black") || cBxTest.SelectedText.Equals("Something"))
{
tbxTest.Text = "TEST";
}
}
Also, that while statement is almost a death threat because once it enters that condition, it will not leave.
You won't be able to change the ComboBox value due to the while being executed.

Order of radio button events in C#

I have some radio buttons on a form. When a user changes the radio button selection, I update another object with that info. Each of the radio buttons has a different event handler, but all of them are for the CheckedChanged event.
Which event is fired first? The CheckedChanged from the radio button that is losing selection, or the CheckedChanged from the radio button that is being selected? Or is it a race?
It takes one minute to create a new WinForms project, drag two radiobuttons on it and assigning two separate event handlers to their CheckedChanged events. So the answer is, in this case: uncheck fires first.
However, it is not documented, so it could change with any .NET update (unlikely, but still). Don't bind your application logic to it.
If you could explain your actual problem, your question can be answered more specifically. I guess your code looks something like this:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
SomeLabel.Text = "Option 1";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
SomeLabel.Text = "Option 2";
}
Change it to take in account the state of the radiobutton:
if ((sender as RadioButton).Checked)
{
SomeLabel.Text = "Option 1";
}
So your code doesn't depend on the order of the events anymore and doesn't execute when it's actually not required.

how to make visible cadre around buttons

how can i visible cadre around my buttons in c# forms. that whenever I click on it or it runs the cadr will be visible around it?
please get me the code example
private void button6_Click(object sender, EventArgs e)
{
}
If you mean the dotted line, that shows where the focus is, there is nothing you need to do; tab to the button (during runtime!) and it will show.
If you mean the change in appearence during the click, this also happens by itself.
If you want any other changes during the click, set and reset them during the MouseDown and MouseUp events, not the Click, as this will be too late.
If you are looking for still something else, please specify more precisely.
I assume you want to change the border appearance of button. Use the Button.FlatAppearance to change the border styles. But this will work only when the style is Flat.
Sample code:
button1.FlatStyle = FlatStyle.Flat;
button1.FlatAppearance.BorderSize = 2;
button1.FlatAppearance.BorderColor = Color.Red;

Categories