Click here to Skip to main content
15,507,526 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:)
Posted

I solve this to myself by copying the code inside the EventHandler to an other function, let's say SomeFunction. Then, inside my EventHandler I call code>SomeFuntion and you can call SomeFunction anywhere you'd like. In this way, you are still executing the same code as the EventHandler.

An example:

This is the first code of an EventHandler:
C#
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);
            }
}

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:
C#
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);
            }
        }

Then I change my previous EventHandler code to:
C#
private void btnSearch_Click(object sender, EventArgs e)
{
    SearchProject();
}

And now I can call SearchProject() in an other function too:
C#
private void tbSearchValue_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
        SearchProject();
}
 
Share this answer
 
v2
Comments
Nikhil@123 30-Nov-12 6:09am    
Hi Sir,
First of All thanks for replying,

Plz dont mind, Could u just elabrate it with some Example.

It would be so gratefull.
Thanks
Frans Jan 30-Nov-12 6:11am    
Already worked on that ;)
Nikhil@123 30-Nov-12 6:31am    
Hi sir,

After going through with your given code, i have got to learn that, you are Calling EventHandler to Function, But i wants to call Function to EventHandler. Please Help me thanks..
Frans Jan 30-Nov-12 6:46am    
In my example I wanted to call the button click EventHandler in the KeyDown EventHandler, but why? I asked myself. Why should I want to call a buttonClick EventHandler in a KeyDown EventHandler, that's too confusing, because, after all, one EventHandler is for a buttonClick where the other is for the KeyDown, right? So why would I want to call a buttonClick EventHandler in my KeyDown EventHandler. Or, if you are not calling it in an other EventHandler, but in a function, why would you want to call the buttonClick EventHandler (or some other EventHandler) if you are not performing that action? So that's why I decided to put my code of the button click EventHandler in it's own function, and then have my button click EventHandler and my KeyDown EventHandler to call that function. Both are calling the same function, so both are doing the same.

Calling an EventHandler when you are not raising that event is confusing.
To answer your question with the answer you want:

If this is the EventHandler you'd like to call:
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.
 
Share this answer
 
Comments
Nikhil@123 30-Nov-12 7:16am    
sir, I have parameters to be passed so, i cannot go for the second solution. The first solution is quite complicate to understand so i now here I am mentioning my code and help me regarding the same.


//THIS IS MY METHOD TO CALL EVENTHANDLER

private void ListenToServer()
{
if (receivedData == "###LOCK###")
{
}
}

//THIS IS MY EVENTHANDLER CODE

public void KeyBoard_Lock(object sender, System.EventArgs e)
{
if (receivedData == "###LOCK###")
{
actHook = new UserActivityHook(); // crate an instance with global hooks
// hang on events
//actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
actHook.KeyDown += new KeyEventHandler(MyKeyDown);
actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp += new KeyEventHandler(MyKeyUp);
}
}

SO NOW I WANTS TO CALL MY KeyBoardLock (eVENT) THROUGH FUNCTION ListenToServer()
Please Help thanks
Frans Jan 30-Nov-12 7:47am    
Yes you have parameters to be passed, no you don't use them. This means that you can use the second solution. Simply call: KeyBoard_Lock(null, null);

To use my first solution do this (you can name SomeFunction() to whatever you like):

public void SomeFunction()
{
if (receivedData == "###LOCK###")
{
actHook = new UserActivityHook();
// crate an instance with global hooks
// hang on events
//actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
actHook.KeyDown += new KeyEventHandler(MyKeyDown);
actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp += new KeyEventHandler(MyKeyUp);
}
}

Then do this:

private void ListenToServer()
{
SomeFunction();
}

public void KeyBoard_Lock(object sender, System.EventArgs e)
{
SomeFunction();
}

So now, you call your SomeFunction() in ListenToServer(), but as you see, KeyBoard_Lock(object sender, System.EventArgs e) does the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900