Handle all checkbox events on a single method - c#

I found some similar questions but none of them on C# Winforms. I have many similar methods for different Checkboxes I have on my form, but now the number of checks is about to double and I'm looking for a simpler way of doing this.
Here's an example:
private void CheckPET_Click(object sender, EventArgs e)
{
if (CheckPET.Checked)
{
CheckAdicionarMaterial("PET");
}
else
{
if (Confirmacao())
CheckRemoverMaterial("PET");
else
CheckPET.Checked = true;
}
}
I have one of these for each Checkbox, and all of them follow the same pattern, so if I could have a method that handles all clicks I could do something like this:
private void GenericCheckbox_Click(object sender, EventArgs e)
{
if (Checkbox.Checked)
{
CheckAdicionarMaterial(Checkbox.Text);//The text of the boxes is always the string I want to pass here.
}
else
{
if (Confirmacao())
CheckRemoverMaterial(Checkbox.Text);
else
Checkbox.Checked = true;
}
}
What can I do to achieve this?

If I understand the question correctly - binding a single function as an event handler for several checkboxes?
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox == null) return;
if (checkbox.Checked)
{
CheckAdicionarMaterial(checkbox.Text);//The text of the boxes is always the string I want to pass here.
}else
{
if (Confirmacao("Desmarcar removerá todas ocorrências do material. Continuar?"))
CheckRemoverMaterial(checkbox.Text);
else
checkbox.Checked = true;
}
}

Related

Disable button when the selected value in comboBox changed

I have a button and a comboBox. The comboBox has 2 value, 'yes and no' I want to disable the button if the selected value is no while i want to enabled if the value selected is yes what would I do, I dont know where will I put the code and also my code seems wrong.
if (ComboBoxCustType.SelectedIndex = 0)
{
Button1.Enabled = false;
}
else
Button1.Enabled = true;
Since you didn't specify WinForms or WPF, this is for WinForms.
You have to create an event on the ComboBox.SelectedIndexChanged and inside the event handler, you write your code to handle the selecteditem text.
public Form1()
{
comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
InitializeComponent();
CheckSelction();
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
CheckSelction();
}
void CheckSelction()
{
if (comboBox1.SelectedItem != null)
{
var item = comboBox1.SelectedItem.ToString();
button1.Enabled = item == "yes";
}
else
button1.Enabled = false;
}

How to make one event to identify whether multiple radio buttons are checked?

I have the following code which checks each radio button (Temp30, Temp40 and Temp60) and does the necessary things such as turning the wash temperature light on etc...
I want to create an event which handles all 3 radio buttons. I thought it could possibly have something to do with the groupbox they are in? (it is called TempGroupBox)
Any help would be much appreciated!
private void Temp30_CheckedChanged(object sender, EventArgs e)
{
if (Temp30.Checked)
{
MainDisplayLabel.Text = (" SELECT SPIN SPEED");
WashTempLight.Visible = true;
WashTempLight.Image = Properties.Resources._30degrees;
SpeedGroupBox.Enabled = true;
}
}
private void Temp40_CheckedChanged(object sender, EventArgs e)
{
if (Temp40.Checked)
{
MainDisplayLabel.Text = (" SELECT SPIN SPEED");
WashTempLight.Visible = true;
WashTempLight.Image = Properties.Resources._40degrees;
SpeedGroupBox.Enabled = true;
}
}
private void Temp60_CheckedChanged(object sender, EventArgs e)
{
if (Temp60.Checked)
{
MainDisplayLabel.Text = (" SELECT SPIN SPEED");
WashTempLight.Visible = true;
WashTempLight.Image = Properties.Resources._60degrees;
SpeedGroupBox.Enabled = true;
}
}
You can bind all radioButton's event to the same handler and use sender parameter to get the control that the action is for.
private void Temps_CheckedChanged(object sender, EventArgs e)
{
string checkedName = ((RadioButton)sender).Name;
if(checkedName == "Temp40")
{
...
}
else if(checkedName == "Temp60")
{
...
}
}
You can add event handler for all RadioBUttons's like that after InitializeComponent():
var radioButtons =this.Controls.OfType<RadioButton>();
foreach (RadioButton item in radioButtons)
{
item.CheckedChanged += Temps_CheckedChanged;
}

How to enable a button if an item in a listbox is selected

I tried this but it doesn't work. They're still greyed out even when I select stuff.
btnVoirFiche.Enabled = false;
btnEchangerJoueur.Enabled = false;
if (lstJoueurs.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
else
{
}
You'll want to handle the ListBox.SelectedIndexChanged event, and within your handler you're going to check if the specific value is the selected one, and then set you button's enable property accordingly.
Something like this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
else
{
//whatever you need to test for
}
}
Cheers
EDIT: I'm not too sure what your logic for button's enabled property is, so my answer is pretty generic. If you add details to you question, I'll adapt accordingly.
Hook into SelectedIndexChanged event and put your code inside of it
private void lstJoueurs_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstJoueurs.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
}
As an alternative, and using mrlucmorin's answer, you could use the listbox's SelectedItem which will return null if nothing is selected.

C# - Highlight wrong controls when validating

I am trying to validate windows form with try catch and so far I succeeded. My goal is when someone forgot to fill the gap or put in incorrect entry, catch returns messagebox with a warning. Now I also have Validating event on every control I want to validate so when somebody leave it empty or in incorrect format it will show the error next to the control. That seems ok so far (for me, at least) but my issue is, that if user doesn't even click to one box it only shows message box, but it won't highlight wrong controls.
Below is my code:
private void createButton_Click(object sender, EventArgs e)
{
try
{
Book newBook = new Book(titleBox.Text, authBox.Text, Convert.ToInt32(yearBox.Text), Convert.ToInt32(editBox.Text), pubComboBox.Text, descBox.Text);
bookList.Add(newBook);
booklistListBox.DataSource = bookList;
}
catch (FormatException)
{
MessageBox.Show("You probably missed a gap or put in incorrect form");
}
}
and those validating events:
private void titleBox_Validating(object sender, CancelEventArgs e)
{
if (titleBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(titleBox, "Title is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(titleBox, "");
}
}
private void authBox_Validating(object sender, CancelEventArgs e)
{
if (authBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(authBox, "Author is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(authBox, "");
}
}
private void yearBox_Validating(object sender, CancelEventArgs e)
{
if (yearBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(yearBox, "Year is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(yearBox, "");
}
}
private void editBox_Validating(object sender, CancelEventArgs e)
{
if (editBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(editBox, "Edition is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(editBox, "");
}
}
private void pubComboBox_Validating(object sender, CancelEventArgs e)
{
if (pubComboBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(pubComboBox, "Publisher is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(pubComboBox, "");
}
}
private void descBox_Validating(object sender, CancelEventArgs e)
{
if (descBox.Text.Trim() == String.Empty)
{
errorProvider.SetError(descBox, "Description is required");
e.Cancel = true;
}
else
{
errorProvider.SetError(descBox, "");
}
}
So is there way to, I don't know, change focus or something like that, forced with pressing the create button?
Thank You
Try using ValidateChildren():
private void createButton_Click(object sender, EventArgs e)
{
bool gotIssues = this.ValidateChildren();
if (gotIssues)
{
// someone didn't validate well...
}
}
So, the issue here is that you want to have it highlight in either of two scenarios:
1) When you leave the field and its contents are invalid (empty in this case)
2) When you click the create button and the field in question has invalid contents
And so I would create a single textBox_checkIfEmpty(object sender, EventArgs e) method:
private void textBox_checkIfEmpty(object sender, EventArgs e)
{
var asTb = sender as TextBox;
if (asTb != null && asTb.Text.Trim() == String.Empty)
{
errorProvider.SetError(asTb, "I'll leave it to you to abstract the error message appropriately");
e.Cancel = true;
}
else
{
errorProvider.SetError(asTb, "");
}
}
Then, you can set this method as the handler for your Validate event on your desired required controls, and you can also call the same method from the create button's handler, looping through the required TextBox instances and executing the method on each.
UPDATE
J. Hudler's ValidateChildren solution would be a more (developer) efficient tail to mine, as opposed to looping through the desired controls. That said, if the form has many children, and you only need to validate several, it might be helpful to loop still. Just depends on your specific scenario. My only other question is whether or not ValidateChildren is infinitely recursive, or if it only goes one level down (immediate children rather than all descendants).
the event validating for control call when the mouse click on the control and then leave it from the control. In your case when the user does not click on the control it will not trigger the validating event. U can do this by making your own function and call them on creat event.
private void button1_Click(object sender, EventArgs e)
{
textBox1_Validating(sender);
}
public void textBox1_Validating(object sender)
{
MessageBox.Show("validating");
errorProvider1.SetError(textBox1, "provide");
}

ListViewItem check by user, not by program

I have a ListView with various items and a ItemCheck handler as below:
private void ListView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == System.Windows.Forms.CheckState.Checked)
{
if (e.Index == 0)
{
ListView1.Items[1].Checked = false;
ListView1.Items[2].Checked = false;
ListView1.Items[3].Checked = false;
ListView1.Items[4].Checked = false;
ListView1.Items[5].Checked = false;
ListView1.Items[6].Checked = false;
ListView1.Items[7].Checked = false;
}
else
{
ListView1.Items[0].Checked = false;
}
}
}
Essentially the first item is "none", so when it is checked all the other items become unchecked (and vice-versa). Occasionally the program checks items in the code and I think this is causing problems. I know TreeViewEventArgs has a field called Action which is equal to TreeViewAction.Unkownif the call is coming from the program and not from the user.
Is there a way to check if a ListViewItem is being checked by a user as opposed to being checked by code?
There's no way to tell from the event arguments so you would have code for it yourself, something like
private bool raisedFromCode;
private void button2_Click(object sender, EventArgs e)
{
raisedFromCode = true;
listView1.Items[1].Checked = !listView1.Items[1].Checked;
raisedFromCode = false;
}
private void listView1_ItemCheck(object sender, ItemCheckEventArgs args)
{
if (!raisedFromCode)
MessageBox.Show("User checked");
}
Alternatively if you just don't want your logic to fire when you change the check state through code you could unsubscribe from the event handler
listView1.ItemCheck -= new ItemCheckEventHandler(this.listView1_ItemCheck);
listView1.Items[1].Checked = false;
listView1.ItemCheck += new ItemCheckEventHandler(this.listView1_ItemCheck);

Categories

Resources