问题描述
我正在研究Some Project,现在我遇到了一些问题,问题是
我有一个EventHandler和一个Function.我想通过该函数调用该EventHandler.调用函数到函数很容易,但是我不知道如何通过函数调用EventHandler.请帮忙.
预先感谢:)
Hi,
Hi, I am working on Some Project, and now i am stuck with some issue, and the issue is
I have one EventHandler and one Function. I wants to call that EventHandler through that function. Calling function to function is easy but i dont know how to call a EventHandler through Function. Please Help.
thanks in advance:)
推荐答案
private void btnSearch_Click(object sender, EventArgs e)
{
int rowIndex = -1;
bool found = false;
dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
//Set a string to search for
int searchValue = int.Parse(tbSearchValue.Text);
foreach (DataGridViewRow row in dgvProjects.Rows)
{
int compareValue = int.Parse(row.Cells[2].Value.ToString());
if (compareValue.Equals(searchValue))
{
found = true;
rowIndex = row.Index;
dgvProjects.Rows[row.Index].Selected = true;
dgvProjects.FirstDisplayedScrollingRowIndex = rowIndex;
break;
}
}
if (!found)
MessageBox.Show("Project number not found.\n\nBe sure you're searching for the right project number.", "Project not found",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (FormatException)
{
MessageBox.Show("Your input is not a number.", "Input Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
现在,我想在另一个函数中调用此代码(在我的示例中为另一个EventHandler,但这是相同的想法).因此,我将此代码复制到一个新函数中:
Now I want to call this code in an other function (In my example an other EventHandler, but it''s the same idea). So I copy this code to a new function:
private void SearchProject()
{
int rowIndex = -1;
bool found = false;
dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
//Set a string to search for
int searchValue = int.Parse(tbSearchValue.Text);
foreach (DataGridViewRow row in dgvProjects.Rows)
{
int compareValue = int.Parse(row.Cells[2].Value.ToString());
if (compareValue.Equals(searchValue))
{
found = true;
rowIndex = row.Index;
dgvProjects.Rows[row.Index].Selected = true;
dgvProjects.FirstDisplayedScrollingRowIndex = rowIndex;
break;
}
}
if (!found)
MessageBox.Show("Project number not found.\n\nBe sure you're searching for the right project number.", "Project not found",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (FormatException)
{
MessageBox.Show("Your input is not a number.", "Input Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
然后,将之前的EventHandler代码更改为:
Then I change my previous EventHandler code to:
private void btnSearch_Click(object sender, EventArgs e)
{
SearchProject();
}
现在我也可以在其他函数中调用SearchProject()
了:
And now I can call SearchProject()
in an other function too:
private void tbSearchValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
SearchProject();
}
protected void btnTest_Click(object sender, EventArgs e)
只需调用:
simply call:
btnTest_Click(null, null);
,但这仅在方法不使用这些参数中的任何一个时才有效.老实说,我仍然坚持以前的解决方案.
But this will only work when the method isn''t using either of these parameters. Honestly I''d still stick with my previous solution.
这篇关于如何使用C#通过Function调用EventHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!