I am trying to make the text of the button change colour when a checkbox is checked, but for some reason, I just don't know how. Would I need to write an If statement, if so how do I do that?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ColourCheckBox.ForeColor = Color.Red;
}
private void ColourCheckBox_CheckedChanged(object sender, EventArgs e)
{
ColourCheckBox.ForeColor = Color.Black;
}
}
Your question is so obscure, but based on the things that I understand, you should check the Checked property.
private void ColourCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (ColourCheckBox.Checked)
{
ColourCheckBox.ForeColor = Color.Black;
}
else
{
ColourCheckBox.ForeColor = Color.Red;
}
}
In the CheckedChanged event, you can use the Checked property:
ColourCheckBox.ForeColor = ColourCheckBox.Checked ? Color.Black : Color.Red;
In case of a Triple state checkbox, with 3 color you can switch on CheckState value :
Unchecked = 0
Checked = 1
Indeterminate = 2
using System.Drawing;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
button1.ForeColor = Color.Red;
}
Related
Hy
I have developed a calculator in C# (window applications). I have two textboxes on the form to get inputs from the user and combobox to select the operation to on the input numbers.
And my problem is that when I pressed the 1 button the code is passing 1 to both textboxes. I want to solve this problem like when i click on the textbox1 and then i press the button 1 the code will pass value to only textbox1. And when i click the textbox2 and then by pressing the button 1 the code will have to pass value only to textbox2.
Thanks in advance for answering.
Simply add a Bool to determine which textbox the currently selected focus is on.
Add focus event to textbox:
private void textBox1_Enter(object sender, EventArgs e)
{
Flag = true;
}
private void textBox2_Enter(object sender, EventArgs e)
{
Flag = false;
}
Code:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static bool Flag = true;
private void button1_Click(object sender, EventArgs e)
{
if (Flag)
{
textBox1.Text += "1";
}
else
{
textBox2.Text += "1";
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
Flag = true;
}
private void textBox2_Enter(object sender, EventArgs e)
{
Flag = false;
}
}
}
Output:
Alright, so I'm trying to practice my windows forms skills and I've been trying to figure out a simple thing such as making a button that says "Show message", when you click it, it makes an invisible label Visible that says "Hey man" and the text of the button should change to "Hide message".
When you click it the label should go invisible again. I tried to do it with if statements for a while but didn't really figure it out, so I just used 2 buttons which made it a million times more simple. I'm just wondering, how would I go about doing it with only 1 button?
My code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnShow_Click(object sender, EventArgs e)
{
lblShow.Visible = true;
btnShow.Visible = false;
btnHide.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnHide_Click(object sender, EventArgs e)
{
lblShow.Visible = false;
btnShow.Visible = true;
btnHide.Visible = false;
}
}
}
You could add a bool to your class, and change the bool when you click the button
bool showLabel = true;
private void btnToggle_Click(object sender, EventArgs e)
{
if (showLabel)
{
lblShow.Visible = true;
}
else
{
lblShow.Visible = false;
}
showLabel = !showLabel;
}
This can be done quite simply with only two lines of code:
private void btnToggle_Click(object sender, EventArgs e)
{
lblShow.Visible = !lblShow.Visible;
btnToggle.Text = lblShow.Visible ? "Hide Message" : "Show Message";
}
It can technically be done in one line, but I think it's less readable:
btnToggle.Text = (lblShow.Visible = !lblShow.Visible) ? "Hide Message" : "Show Message";
I suggest keeping a state variable and updating the form based on the value of the state variable. This is a flexible pattern where you can accommodate more than one state, which means you can have combinations of buttons that produce different results.
Try this:
public partial class Form1 : Form
{
public enum FormState
{
MessageHidden,
MessageVisible,
Default = MessageHidden
}
FormState state = FormState.Default;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
UpdateUI();
}
protected void UpdateUI()
{
switch (state)
{
case FormState.MessageHidden:
label1.Visible = false;
button1.Text = "Show Message";
break;
case FormState.MessageVisible:
label1.Visible = true;
button1.Text = "Hide Message";
break;
default:
throw new NotImplementedException();
}
}
private void button1_Click(object sender, EventArgs e)
{
switch (state)
{
case FormState.MessageHidden:
state = FormState.MessageVisible;
break;
case FormState.MessageVisible:
state = FormState.MessageHidden;
break;
default:
throw new NotImplementedException();
}
UpdateUI();
}
}
Example screenshots
FormState.MessageHidden
FormState.MessageVisible
You can refactor using one method to set the label visibility like that:
private void btnToggle_Click(object sender, EventArgs e)
{
lblShow.Visible = !lblShow.Visible;
btnToggle.Text = lblShow.Visible ? "Hide Message" : "Show Message";
}
With the VS Form Desiner you can set the label Visible property to true and the button Text to nothing.
And in the FormLoad event you call the method to set the things :
btnToggle_Click(this, null);
I have a set of 5 buttons which I created in winforms using c#,if i click the button1 it should change to green color.,then if button2 is clicked then it should change to green.,but button1 should change to its original color.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
{
button1.BackColor = Color.Green;
button2.BackColor = Color.Lavender;
button3.BackColor = Color.Lavender;
button4.BackColor = Color.Lavender;
button5.BackColor = Color.Lavender;
}
}
private void button2_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Lavender;
button2.BackColor = Color.Green;
button3.BackColor = Color.Lavender;
button4.BackColor = Color.Lavender;
button5.BackColor = Color.Lavender;
}
private void button3_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Lavender;
button2.BackColor = Color.Lavender;
button3.BackColor = Color.Green;
button4.BackColor = Color.Lavender;
button5.BackColor = Color.Lavender;
}
}
}
but this code will be hectic if i have many buttons.,please help me out of this.
You can create a single event handler and assign it to Click event of all your buttons:
private void ButtonClickHandler(object sender, EventArgs e)
{
// iterate over all buttons on form
foreach (var button in Controls.OfType<Button>())
button.BackColor = button == sender ? Color.Green : Color.Lavender;
}
As #CharlesMay stated in comment, be aware, that this code will find each and any Button that is direct child of your form. There is some ways to avoid it:
1. Hold you buttons in a container. For example create a Panel (say myPanel) and place all of this buttons on that panel, then iterate over that panel's controls:
foreach (var button in myPanel.Controls.OfType<Button>())
2. Store you active button in a private field. This way you don't need to iterate over controls at all:
private Button _activeButton = null;
private void ButtonClickHandler(object sender, EventArgs e)
{
// disable previosly active button
if (_activeButton != null) _activeButton.BackColor = Color.Lavender;
// set new button
_activeButton = sender as Button;
// enable currently active button
if (_activeButton != null) _activeButton.BackColor = Color.Green;
}
You could do it like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SetActive(Button active)
{
foreach(var btn in new[] {button1, button2, button3, button4, button5})
{
btn.BackColor = btn == active ? Color.Green : Color.Lavender;
}
}
private void button1_Click(object sender, EventArgs e)
{
SetActive(button1);
}
private void button2_Click(object sender, EventArgs e)
{
SetActive(button2);
}
private void button3_Click(object sender, EventArgs e)
{
SetActive(button3);
}
}
}
I would like to have my text deleted only once in my Textbox so that it wouldn't clear Textbox every time I click. My current code looks like:
private void textBox1_Click(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
But how can I get it to only delete the text once?
You could use a simple boolean flag:
public partial class Form1 : Form
{
bool firstClick = true;
And in your event handler:
private void textBox1_Click(object sender, EventArgs e)
{
if (firstClick)
{
textBox1.Text = string.Empty;
firstClick = false;
}
}
I use Mouse events MouseEnter and MouseLeave with a pictureBox. The Back Color changes with Mouse Enter but do not change in normal with mouse Leave Event.
public void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.Blue;
}
public void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.BackColor = SystemColors.Control;
}
Maybe you should remember what the previous color was?
Color prevColor = Color.Black;
public void pictureBox1_MouseEnter(object sender, EventArgs e)
{
prevColor = pictureBox1.BackColor;
pictureBox1.BackColor = Color.Blue;
}
public void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.BackColor = prevColor;
}
you have to know what;s the previous color and change it accordingly. also make sure the control it registered the both the events:
for example, if the color was gray before then:
public void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.Blue;
}
public void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.Gray;
}
It seems correct.
You should put a breaking point in the mouseleave event.
Maybe the pictureBox1_MouseLeave event is not set correctly.