3

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)
    }
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
user4891693
  • 35
  • 1
  • 2
  • 5

6 Answers6

4

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
}
Steve
  • 9,335
  • 10
  • 49
  • 81
4

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());
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
0

just call it with current parameters.

if (Condition)
{
    butSubmit(sender, null)
}
George Chen
  • 6,592
  • 5
  • 21
  • 23
0

Unbelievable, but

btnSubmit_Click(null,null); 

Or other arguments if needed.

MagisterCrazy
  • 225
  • 1
  • 10
0
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{    
    if (Condition)
    {
        button1_Click(sender, EventArgs.Empty);
    }
}

button1_Click is similar to a normal method which accepts two inputs of type object and EventArgs So you can call them by giving the same parameters. if you are not going to use these arguments inside the method then you can call them by passing null,null don't use null if you want to use e or sender inside the method. in such situation call them as like i suggested above.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

Thanks Steve and Hari - this worked well

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
}
user4891693
  • 35
  • 1
  • 2
  • 5
  • If one of the answers help you, you should click the check mark next to it to mark it as accepted, rather than posting a "thanks" answer. – Steve Mar 20 '16 at 04:13