Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call private void from another private void in C#

I would like to call btnSubmit if certain conditions in axTws1_tickPrice are true. How do i do this?

private void btnSubmit_Click(object sender, EventArgs e)
{
  //code here
}

private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{    
    if (Condition)
    {
        Call butSubmit (how do i do this)
    }
}
like image 668
user4891693 Avatar asked Jan 23 '26 05:01

user4891693


2 Answers

You're better off having a common method that both of your control handlers call, rather than trying to call one handler from another. That way your code is more extensible and testable, and you don't have to worry about the event arguments or senders.

For example:

private void btnSubmit_Click(object sender, EventArgs e)
{
    DoStuff();
}

private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{    
    if (Condition)
    {
        DoStuff();
    }
}

private void DoStuff()
{
    // code to do stuff common to both handlers
}
like image 105
Steve Avatar answered Jan 24 '26 19:01

Steve


Multiple options.

Option 1 :

Preferred approach, move common logic to another method.

private void btnSubmit_Click(object sender, EventArgs e)
{
    CommonLogic();
}

private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{    
    if (Condition)
    {
        CommonLogic();
    }
}

private void CommonLogic()
{
    // code for common logic
}

Option 2:

Executing PerformClick() method which generates a Click event for a button.

btnSubmit.PerformClick();

Option 3:

Invoke the event method like any other normal method.

btnSubmit_Click(sender, new EventArgs());
like image 41
Hari Prasad Avatar answered Jan 24 '26 20:01

Hari Prasad