By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Button executes twice?

P: n/a
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;
}
}
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" wrote:
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;
}
}

Nov 16 '05 #2

P: n/a
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" wrote:
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" wrote:
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;
}
}

Nov 16 '05 #3

P: n/a
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
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;
}
}

Nov 16 '05 #4

P: n/a
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
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.