I am trying to programmatically click a boolean button that is on a custom grid created by an Indicator.
I’ve tried just changing the boolean variable that clicking the button sets, but that leaves the button in the wrong state, and then it has to be clicked twice to get back to its proper functionality. I want the button to actually be clicked, and then have that action trigger all of the things that clicking the button does (which includes setting the variable, but also changes the appearance of the button).
Here’s what doesn’t work:
if (goStop == true && orderButtonClicked == false)
{
OnMyButtonClick(null, new RoutedEventArgs());
}
This crashes the Indicator, saying “Object reference not set to an instance of an object.”
Something else that doesn’t work is:
if (goStop == true && orderButtonClicked == false)
{
ChartControl.Dispatcher.InvokeAsync((Action)(() =>
{
OnMyButtonClick(null, new RoutedEventArgs());
}));
}
This doesn’t crash the Indicator, but it doesn’t do anything else, either. There is no change in the appearance of the button, nor is there any output from the Prints that trigger when the button is clicked.
I have seen some references to people auto-clicking buttons on the Chart Trader Panel, but I don’t know how that would translate to a custom button that I created.
OnMyButton() click is throwing an exception for some reason. When run directly the exception is in the ninja script context. When run via invoke the exception occurs on the UI thread and is probably being caught and suppressed.
What is in your OnMyButton() handler? does it use the sender argument, because you are passing null?
This is borrowed code. I changed the name and appearance of the button, but otherwise I cut-and-pasted. It works fine as long as I manually click the button.
System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
if (button != null)
{
orderButtonClicked = !orderButtonClicked;
}
if (orderButtonClicked)
{
//do stuff with button
button is sender and sender is null. The “button !=null” condition just protects the orderButtonClicked assignment. It does not protect all the following code that tries to access button properties. Because button is null in this case, the call to button.Content will throw.
You’ll need to either pass in a reference to the button as sender when you call OnMyButtonClick(), or change your handler code the be fully null sender/button safe.
“I’ve tried just changing the boolean variable that clicking the button sets, but that leaves the button in the wrong state, and then it has to be clicked twice to get back to its proper functionality. I”
For me I set Triggers on the buttonStyle, then this deals with all the styling of various states of the button, example:
The code I included in my last post works. It works very well, in fact. I am just ironing out some last few bugs in my system, but right now it’s already working quite well.