Button executes twice?

G

Guest

Hello all,

I have an application that has a ListView and a DataSet. When btnRemove is
clicked, the method btnRemove_Click is being fired, which compares 2 keys to
find a unique match on a dataRow which then should be removed, through a
foreach loop and then the loop is being exited by break statement.

The problem is that for some reason, my (custom control) button is being
fired again, which may trigger an Exception since there might not be any
records left in the DataSet and ListView.

Below is the snippet of btnRemove_Click(object sender, EventArgs e)
Any thoughts why this is being fired twice?

Muchly appreciated,
Vincent

// Reference the selected ListViewItem
ListViewItem lvi = new ListViewItem();
lvi = lvSources.SelectedItems[0];
// Loop through the DataSet and find the corresponding record.
foreach(DataRow row in dsMaster.Tables["SrcTable"].Rows)
{
if(row["Source"].Equals(lvi.SubItems[0].Text) &&
row["OrigSource"].Equals(lvi.SubItems[1].Text))
{
row.Delete();
lvSources.Items.Remove(lvi);
dsMaster.Tables["SrcTable"].AcceptChanges();
return;
}
}
 
G

Guest

hi!

for that i would ask 'if(lvSources.SelectedItems.Count > 0){...}' ...
and i would disable the button at beginning method and enable (if
items-Count > 0) it at the end of the click-method

ciao
 
G

Guest

Hi Darkblue,

I came to that conclusion, but still it seems odd to me that the click
method is being called twice. Are there any known issues with custom controls
derived from System.Windows.Forms.Button?

darkblue progger said:
hi!

for that i would ask 'if(lvSources.SelectedItems.Count > 0){...}' ...
and i would disable the button at beginning method and enable (if
items-Count > 0) it at the end of the click-method

ciao

Vincent said:
Hello all,

I have an application that has a ListView and a DataSet. When btnRemove is
clicked, the method btnRemove_Click is being fired, which compares 2 keys to
find a unique match on a dataRow which then should be removed, through a
foreach loop and then the loop is being exited by break statement.

The problem is that for some reason, my (custom control) button is being
fired again, which may trigger an Exception since there might not be any
records left in the DataSet and ListView.

Below is the snippet of btnRemove_Click(object sender, EventArgs e)
Any thoughts why this is being fired twice?

Muchly appreciated,
Vincent

// Reference the selected ListViewItem
ListViewItem lvi = new ListViewItem();
lvi = lvSources.SelectedItems[0];
// Loop through the DataSet and find the corresponding record.
foreach(DataRow row in dsMaster.Tables["SrcTable"].Rows)
{
if(row["Source"].Equals(lvi.SubItems[0].Text) &&
row["OrigSource"].Equals(lvi.SubItems[1].Text))
{
row.Delete();
lvSources.Items.Remove(lvi);
dsMaster.Tables["SrcTable"].AcceptChanges();
return;
}
}
 
G

Guest

Hi Vincent,

I think that your problem lies not in the handler's method.
It seems that you've assign the same event handler twice to
the "Click" event.

HTH
Marcin
 
G

Guest

Hi Marcin,

I did a Find in Visual Studio on btnRemove.Click but I can only find it once.
Are there any other places except in the form where this can be done?

Thanks,
Vincent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top