No overload for '...' matches delegate 'System.Windows.Forms.ItemCheckEventHandler' - c#

After reading this question, it has become apparent to me that I am writing my event incorrectly. However, I have no idea how I am going to be able to re-write what I have written using Object sender. My event adds the text from selected checkboxes to a two-dimensional list (report), and the order in report must be the same as the order of the selected checkboxes. Also, no more than two checkboxes can be selected at a time. Here is the event:
void checkedListBox_ItemCheck(CheckedListBox chkdlstbx, ItemCheckEventArgs e)
{
int index = Convert.ToInt32(chkdlstbx.Tag);
if ((chkdlstbx.CheckedItems.Count == 0) && (e.CurrentValue == CheckState.Unchecked))
{
Var.report[index].Add(chkdlstbx.Text);
}
if ((chkdlstbx.CheckedItems.Count == 1) && (e.CurrentValue == CheckState.Checked))
{
Var.report[index].RemoveAt(0);
}
if ((chkdlstbx.CheckedItems.Count == 1) && (e.CurrentValue == CheckState.Unchecked))
{
if (chkdlstbx.SelectedIndex < chkdlstbx.CheckedIndices[0])
{
Var.report[index].Insert(0, chkdlstbx.Text);
}
else
{
Var.report[index].Add(chkdlstbx.Text);
}
}
if ((chkdlstbx.CheckedItems.Count == 2) && (e.CurrentValue == CheckState.Checked))
{
if (chkdlstbx.SelectedIndex == chkdlstbx.CheckedIndices[0])
{
Var.report[index].RemoveAt(0);
}
else
{
Var.report[index].RemoveAt(1);
}
}
if ((chkdlstbx.CheckedItems.Count == 2) && (e.CurrentValue == CheckState.Unchecked))
{
e.NewValue = CheckState.Unchecked;
}
updateReport();
}
It is being called by this line:
chkdlstbx.ItemCheck += new ItemCheckEventHandler(checkedListBox_ItemCheck);
If anyone could help me re-write my event using object, that'd be awesome. I'm not really sure how else I would go about solving this problem!

This should suffice:
void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox chkdlstbx = sender as CheckedListBox;
if (chkdlstbx == null)
{
throw new InvalidArgumentException();
}
....
}

Related

Multiple Correct If Statements from a Mouse Click Event

I'm trying to do waste collection program and these are part of codes. My problem is if the picturebox shows image that on second if statements (magazine), there is no problem. But if shows first image that on first if statements (newspaper) and if NewWaste(); gives magazine then there is a problem. Because it adds both of them to listbox but I don't see the second image on picturebox. How can I solve that?
private void NewWaste()
{
Image[] images = new Image[] { newspaper.Image, magazine.Image, glass.Image };
int wastes = rnd.Next(images.Length);
wastePictureBox.Image = images[wastes];
}
//(part of class)
public bool Add(Waste waste)
{
if (FilledVolume + waste.Volume <= Capacity)
return true;
else
return false;
}
private void addPaperWasteBtn_Click(object sender, EventArgs e)
{
if (paperWasteBox.Add(newspaper) == true && wastePictureBox.Image == newspaper.Image)
{
paperWasteListBox.Items.Add("Newspaper");
NewWasteImage();
}
if (paperWasteBox.Add(magazine) == true && wastePictureBox.Image == magazine.Image)
{
paperAtikListBox.Items.Add("Magazine");
NewWasteImage();
}
}
If you only want the second if statement to run if the first one didn't, then you want an else if statement before the second conditional check.
Change:
if (paperWasteBox.Add(newspaper) == true && wastePictureBox.Image == newspaper.Image)
{
paperWasteListBox.Items.Add("Newspaper");
NewWasteImage();
}
if (paperWasteBox.Add(magazine) == true && wastePictureBox.Image == magazine.Image)
{
paperAtikListBox.Items.Add("Magazine");
NewWasteImage();
}
To:
if (paperWasteBox.Add(newspaper) == true && wastePictureBox.Image == newspaper.Image)
{
paperWasteListBox.Items.Add("Newspaper");
NewWasteImage();
}
else if (paperWasteBox.Add(magazine) == true && wastePictureBox.Image == magazine.Image)
{
paperAtikListBox.Items.Add("Magazine");
NewWasteImage();
}
Notice the difference in the SIXTH line!

How to fix an operator to a void call in a button?

I'm a beginner C# dev and I am forgetting something. The (!ValidateCleared(actionsChecked))code is throwing and error the Operator ! cannot be applied to a void. Not sure how to fix this in this example. I tried keeping it a bool and using a break, but the actions didn't behave as expected. It only checked the one action.
protected void btnSubmit_Click(object sender, EventArgs e)
{
ValidateActionSelected();enter code here
ValidateCleared(actionsChecked);
if (!ValidateCleared(actionsChecked))
{
if (String.Equals(ddlActionsAndDocuments.SelectedItem.Text.ToString(), "XXX YYY") || String.Equals(ddlActionsAndDocuments.SelectedItem.Text.ToString(), "XXX ZZZ"))
{
reqEffectiveDate.ErrorMessage = "";
}
if (ddlActionsAndDocuments.SelectedValue == ActionTypes.XXXYYY ||
ddlActionsAndDocuments.SelectedValue == ActionTypes.XXXYYYDenial ||
ddlActionsAndDocuments.SelectedValue == ActionTypes.XXXzzz)
{
ValidateXXXYYY(actionsChecked, ddlActionsAndDocuments.Text);
}
if (ddlActionsAndDocuments.SelectedValue == InsuranceActionTypes.WWW ||
ddlActionsAndDocuments.SelectedValue == InsuranceActionTypes.YYYZZZ ||
ddlActionsAndDocuments.SelectedValue == InsuranceActionTypes.YYYWWW ||
ddlActionsAndDocuments.SelectedValue == InsuranceActionTypes.YYYWaived)
{
ValidateCertificate(actionsChecked, ddlActionsAndDocuments.Text);
}
}
}
private void ValidateCleared(List<xxCaseEntity> actionsChecked)
{
//bool error = false;
foreach (xxCaseEntity ACTIONyy in actionsChecked)
{
if (ACTIONyy.XXStatusCode == 40 || ACTIONyy.XXStatusCode == 45)
{
//error = true;
DisplayErrorMessage("FR Action Cannot Apply to " + ACTIONyy.CaseIdentifier);
actionsChecked.Remove(ACTIONyy);
//break;
}
}
return;
//return error;
}
The fixed code is below
protected void btnSubmit_Click(object sender, EventArgs e)
{
ValidateActionSelected();enter code here
ValidateCleared(actionsChecked);
if (!ValidateCleared(actionsChecked))
{
if (String.Equals(ddlActionsAndDocuments.SelectedItem.Text.ToString(), "XXX YYY") || String.Equals(ddlActionsAndDocuments.SelectedItem.Text.ToString(), "XXX ZZZ"))
{
reqEffectiveDate.ErrorMessage = "";
}
if (ddlActionsAndDocuments.SelectedValue == ActionTypes.XXXYYY ||
ddlActionsAndDocuments.SelectedValue == ActionTypes.XXXYYYDenial ||
ddlActionsAndDocuments.SelectedValue == ActionTypes.XXXzzz)
{
ValidateXXXYYY(actionsChecked, ddlActionsAndDocuments.Text);
}
if (ddlActionsAndDocuments.SelectedValue == InsuranceActionTypes.WWW ||
ddlActionsAndDocuments.SelectedValue == InsuranceActionTypes.YYYZZZ ||
ddlActionsAndDocuments.SelectedValue == InsuranceActionTypes.YYYWWW ||
ddlActionsAndDocuments.SelectedValue == InsuranceActionTypes.YYYWaived)
{
ValidateCertificate(actionsChecked, ddlActionsAndDocuments.Text);
}
}
}
private bool ValidateCleared(List<xxCaseEntity> actionsChecked)
{
List<xxCaseEntity> removeItems = new List<xxCaseEntity>();
foreach (xxCaseEntity ACTIONyy in actionsChecked)
{
if (ACTIONyy.XXStatusCode == 40 || ACTIONyy.XXStatusCode == 45)
{
DisplayErrorMessage("FR Action Cannot Apply to " + ACTIONyy.CaseIdentifier);
//actionsChecked.Remove(ACTIONyy);
removeItems.Add(ACTIONyy);
}
}
foreach(xxCaseEntity ACTIONyy in removeItems)
{
actionsChecked.Remove(ACTIONyy);
}
return true;
}

How to give condition in keydown DataGridView C#

I want to make a condition like this
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
}
If the user selects the first column it will execute insert and if they select the third column it will execute edit
How do I go about doing this?
You need to check the selected columns, assuming they can only select one you can do this check
if(dataGridView1.SelectedColumns[0] == dataGridView1.Columns[0])
{
//Insert code;
}
else if(dataGridView1.SelectedColumns[0] == dataGridView1.Columns[2])
{
//Edit code;
}
If they can select multiple columns you should first do this check as well
if(dataGridView1.Columns.GetColumnCount(DataGridViewElementStates.Selected) == 1)
{
if(dataGridView1.SelectedColumns[0] == dataGridView1.Columns[0])
{
//Insert code;
}
else if(dataGridView1.SelectedColumns[0] == dataGridView1.Columns[2])
{
//Edit code;
}
}
Try this
if( dataGridView1.SelectedColumns[2] != null)
{
//Insertion
}
if( dataGridView1.SelectedColumns[0] != null)
{
//Editing
}

c# event fires windows form incorrectly

I'm trying to understand what's happening here. I have a CheckedListBox which contains some ticked and some un-ticked items. I'm trying to find a way of determining the delta in the selection of controls. I've tried some cumbersome like this - but only works part of the time, I'm sure there's a more elegant solution. A maybe related problem is the myCheckBox_ItemCheck event fires on form load - before I have a chance to perform an ItemCheck. Here's what I have so far:
void clbProgs_ItemCheck(object sender, ItemCheckEventArgs e)
{
// i know its awful
System.Windows.Forms.CheckedListBox cb = (System.Windows.Forms.CheckedListBox)sender;
string sCurrent = e.CurrentValue.ToString();
int sIndex = e.Index;
AbstractLink lk = (AbstractLink)cb.Items[sIndex];
List<ILink> _links = clbProgs.DataSource as List<ILink>;
foreach (AbstractLink lkCurrent in _links)
{
if (!lkCurrent.IsActive)
{
if (!_groupValues.ContainsKey(lkCurrent.Linkid))
{
_groupValues.Add(lkCurrent.Linkid, lkCurrent);
}
}
}
if (_groupValues.ContainsKey(lk.Linkid))
{
AbstractLink lkDirty = (AbstractLink)lk.Clone();
CheckState newValue = (CheckState)e.NewValue;
if (newValue == CheckState.Checked)
{
lkDirty.IsActive = true;
}
else if (newValue == CheckState.Unchecked)
{
lkDirty.IsActive = false;
}
if (_dirtyGroups.ContainsKey(lk.Linkid))
{
_dirtyGroups[lk.Linkid] = lkDirty;
}
else
{
CheckState oldValue = (CheckState)e.NewValue;
if (oldValue == CheckState.Checked)
{
lkDirty.IsActive = true;
}
else if (oldValue == CheckState.Unchecked)
{
lkDirty.IsActive = false;
}
_dirtyGroups.Add(lk.Linkid, lk);
}
}
else
{
if (!lk.IsActive)
{
_dirtyGroups.Add(lk.Linkid, lk);
}
else
{
_groupValues.Add(lk.Linkid, lk);
}
}
}
Then onclick of a save button - I check whats changed before sending to database:
private void btSave_Click(object sender, EventArgs e)
{
List<AbstractLink> originalList = new List<AbstractLink>(_groupValues.Values);
List<AbstractLink> changedList = new List<AbstractLink>(_dirtyGroups.Values);
IEnumerable<AbstractLink> dupes = originalList.ToArray<AbstractLink>().Intersect(changedList.ToArray<AbstractLink>());
foreach (ILink t in dupes)
{
MessageBox.Show("Changed");
}
if (dupes.Count() == 0)
{
MessageBox.Show("No Change");
}
}
For further info. The definition of type AbstractLink uses:
public bool Equals(ILink other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(this, other)) return true;
return IsActive.Equals(other.IsActive) && Linkid.Equals(other.Linkid);
}
There's little point that I see to do this in the ItemCheck event. Just calculate the delta when you save. Cuts out a bunch of code and trouble with spurious events.

DataGridView: How to make some cells unselectable?

How can I make some cells in DataGridView unselectable?
By 'unselectable' I mean: It cannot be selected in any way and trying to select it won't unselect any other cell.
I don't mean ReadOnly. My cells already have this property as true.
DataGridView.MultiSelect needs to be false.
Thanks to JYL's answer I wrote a code:
private int selectedCellRow = 0;
private int selectedCellColumn = 0;
private void grid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.Cell == null || e.StateChanged != DataGridViewElementStates.Selected)
return;
if (e.Cell.RowIndex == 0 || e.Cell.ColumnIndex == 0 || e.Cell.RowIndex == 1 && e.Cell.ColumnIndex == 1)
{
e.Cell.Selected = false;
grid.Rows[selectedCellRow].Cells[selectedCellColumn].Selected = true;
}
else
{
selectedCellRow = e.Cell.RowIndex;
selectedCellColumn = e.Cell.ColumnIndex;
}
//this was only for seeing what is happening
//this.Text = selectedCellRow + " " + selectedCellColumn;
}
But this leads to StackOverflow. What condition and where I need to put to prevent that?
Added and commented the condition you were asking about.
private int selectedCellRow = 0;
private int selectedCellColumn = 0;
private void grid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.Cell == null || e.StateChanged != DataGridViewElementStates.Selected)
return;
//if Cell that changed state is to be selected you don't need to process
//as event caused by 'unselectable' will select it again
if (e.Cell.RowIndex == selectedCellRow && e.Cell.ColumnIndex == selectedCellColumn)
return;
//this condition is necessary if you want to reset your DataGridView
if (!e.Cell.Selected)
return;
if (e.Cell.RowIndex == 0 || e.Cell.ColumnIndex == 0 || e.Cell.RowIndex == 1 && e.Cell.ColumnIndex == 1)
{
e.Cell.Selected = false;
grid.Rows[selectedCellRow].Cells[selectedCellColumn].Selected = true;
}
else
{
selectedCellRow = e.Cell.RowIndex;
selectedCellColumn = e.Cell.ColumnIndex;
}
}
You can use the event "CellStateChanged".
private void DataGridViewXYZ_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.Cell == null
|| e.StateChanged != DataGridViewElementStates.Selected)
return;
if (! [condition here : can this cell be selectable ?])
e.Cell.Selected = false;
}
EDIT : if you leave the MultiSelect property of gridView to True, you can manage yourself a "single select" gridview with unselectable cells : il the cell is selectable, clear the other selection...
I believe this article may prove useful to you:
http://blog.spencen.com/2009/04/25/readonly-rows-and-cells-in-a-datagrid.aspx
The ReadOnly property can be applied to the entire grid, a column, a row, or an individual cell.

Categories

Resources