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);
Related
I've written a code in which when one check box is clicked the others get disselected
image of buttons
3 of my check boxes are working fine byname,containing and bydate But when I try to do the same with the by category check box I get the following message.error image
This is my code
private void vieworder_Load(object sender, EventArgs e)
{
try
{
con.Open();
if (con.State == ConnectionState.Open)
{
lblstatus.Text = "Connected";
lblstatus.ForeColor = Color.Green;
}
else
{
lblstatus.Text = "Not-Connected";
lblstatus.ForeColor = Color.Red;
}
showdtgrid();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void hidecheckbox()
{
chkboxbyname.CheckState = CheckState.Unchecked;
chkboxbydate.CheckState = CheckState.Unchecked;
chkboxcontaining.CheckState = CheckState.Unchecked;
checkBox1.CheckState = CheckState.Unchecked;
}
private void chkboxbyname_CheckedChanged(object sender, EventArgs e)
{
hidecheckbox();
chkboxbyname.CheckState = CheckState.Checked;
}
private void chkboxcontaining_CheckedChanged(object sender, EventArgs e)
{
hidecheckbox();
chkboxcontaining.CheckState = CheckState.Checked;
}
private void chkboxbydate_CheckedChanged(object sender, EventArgs e)
{
hidecheckbox();
chkboxbydate.CheckState = CheckState.Checked;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
hidecheckbox();
checkBox1.CheckState = CheckState.Checked;
}
}
}
Please tell me what's wrong
At a glance, it looks like the stack overflow is directly related to the event handler for checkBox1_CheckedChanged.
It looks like that handler is calling hideCheckbox(), which in turn changes the CheckState of your checkbox to Unchecked. That would in turn fire off your event handler, and therefore create an infinite loop.
What's strange is that you claim everything is working for the other checkboxes. My guess is that those checkboxes aren't wired up to their corresponding event handlers, as they should also cause stack overflow.
Could you try removing your event handler entirely if it isn't needed? Otherwise, comment out the call to hideCheckbox()
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;
}
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.
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");
}
The Default is No Checkbox
When I run the program and Click the Yes Checkbox the program overflowed
private void checkEdit1_Click(object sender, EventArgs e)
{
checkEdit2.Checked = false;
textEdit1.Enabled = true;
answered = true;
optional = textEdit1.Text;
if (!checkEdit1.Checked)
{
checkEdit1.Checked = true;
checkEdit2.Checked = false;
textEdit1.Enabled = true;
optional = textEdit1.Text;
}
}
private void checkEdit2_Click(object sender, EventArgs e)
{
checkEdit1.Checked = false;
textEdit1.Enabled = false;
answered = false;
if (!checkEdit2.Checked)
{
checkEdit2.Checked = true;
checkEdit1.Checked = false;
textEdit1.Enabled = false;
answered = false;
}
}
What you think is the error ?
Instead of the Click event you should use the CheckedChanged event in this way:
checkEdit1.CheckedChenged += new EventHandler(checkEdit1_CheckedChanged);
checkEdit2.CheckedChenged += new EventHandler(checkEdit2_CheckedChanged);
private void checkEdit1_CheckedChanged(object sender, EventArgs e)
{
if(checkEdit1.Checked == checkEdit2.Checked)
checkEdit2.Checked = !checkEdit.Checked;
}
private void checkEdit2_CheckedChanged(object sender, EventArgs e)
{
if(checkEdit1.Checked == checkEdit2.Checked)
checkEdit2.Checked = !checkEdit.Checked;
}
But the best way in this case is to use a group of radio buttons.
Assuming that those methods are wired up to checkEdit1 and checkEdit2 I would advise that you don't make a change to checkEdit1 in checkEdit1_Click as it has already changed - only modify the state of the alternate.
However, when you modify the state of the other, unless you're careful, you're going to get called back. Eventually the computer gives up -- the overflow!
As mentioned in a comment by #Cyborgx37, radio buttons are the better UX choice here!
A possible solution, bind a single method to the OnClick to BOTH checkboxes:
private bool internallyUpdating = false;
private void CheckboxClick(object sender, EventArgs e)
{
if ( !internallyUpdating )
{
// Prevent subsequent changes
internallyUpdating = true;
// Exchange 'checked' state
if ( sender == checkEdit1 )
{
checkEdit2.Checked = !checkEdit2.Checked;
}
else // if (sender == checkEdit2)
{
checkEdit1.Checked = !checkEdit1.Checked;
}
// other logic here..
// restore 'on change' functionality.
internallyUpdating = false;
}