Toolbar Buttons not Working

Questions about MultiCharts .NET and user contributed studies.
Jobauma
Posts: 104
Joined: 16 Apr 2013
Has thanked: 24 times
Been thanked: 4 times

Toolbar Buttons not Working

Postby Jobauma » 16 Nov 2018

Hi.

I'm having some problems with buttons to work. With several clicks before the button fires works with "[RecoverDrawings(false)]", and does not work at all without it. :)

Code: Select all

private void TSB1_Buy( object Sender, EventArgs E )
{
m_Buy = true;
m_Sell = false;
}

Code: Select all

var TSB1 = new ToolStripButton
{
Text = "Buy"
};

TSB1.Click += TSB1_Buy;
AddItemToolStrip( TB, TSB1 );
I'm wondering if someone can help me to solve this, regardless of a bar is closed or open. I want the buttons to work at all times. :D

Jobauma
Posts: 104
Joined: 16 Apr 2013
Has thanked: 24 times
Been thanked: 4 times

Re: Toolbar Buttons not Working

Postby Jobauma » 16 Nov 2018

To add "ExecControl.Recalculate()" did it, but it removes all previous positions the buttons make.

Is there a work around? :)

Code: Select all

private void TSB1_Buy( object Sender, EventArgs E )
{
m_Buy = true;
m_Sell = false;

ExecControl.Recalculate();
}

TSB2_Buy( object Sender, EventArgs E )
{
m_Sell = true;
m_Buy = false;

ExecControl.Recalculate();
}

Jobauma
Posts: 104
Joined: 16 Apr 2013
Has thanked: 24 times
Been thanked: 4 times

Re: Toolbar Buttons not Working

Postby Jobauma » 18 Nov 2018

Ahh!

I solved it now... I discovered that code inside "Bars.LastBarOnChart" forced me to use "ExecControl.Recalculate();", to activate the code. It seems that there is something strange with "Bars.LastBarOnChart". But I don't know what else I could do. :)

Full Code:

Code: Select all

private bool m_Buy, m_Sell;



private bool m_Toolbar_Inited = false;



private void TSB_Buy( object Sender, EventArgs E )
{
m_Buy = true;
m_Sell = false;

// Used this with "Bars.LastBarOnChart".
//ExecControl.Recalculate();
}

private void TSB_Sell( object Sender, EventArgs E )
{
m_Sell = true;
m_Buy = false;

// Used this with "Bars.LastBarOnChart".
//ExecControl.Recalculate();
}



private void AddItemToolStrip( ToolStrip TB, ToolStripItem Item )
{
Item.Tag = this;
TB.Items.Add(Item);
}



protected override void StartCalc()
{
if (!m_Toolbar_Inited)
{
ChartToolBar.AccessToolBar( TB =>
{
{
var TSB = new ToolStripButton
{
Text = "Buy"
};

TSB.Click += TSB_Buy;
AddItemToolStrip( TB, TSB );
}

AddItemToolStrip( TB, new ToolStripSeparator() );

{
var TSB = new ToolStripButton
{
Text = "Sell"
};

TSB.Click += TSB_Sell;
AddItemToolStrip( TB, TSB );
}
});

m_Toolbar_Inited = true;
}
}



protected override void CalcBar()
{
if (Bars.LastBarOnChart)
{
// Code to be triggered ( inside "Bars.LastBarOnChart" ), didn't trigger ( with buttons ). :(

if (m_Buy)
{
// Code.
}
else if (m_Sell)
{
// Code.
}
}
}



protected override void Destroy()
{
if (m_Toolbar_Inited)
{
ChartToolBar.AccessToolBar( TB =>
{
var For_Erase = new List<ToolStripItem>();

foreach ( ToolStripItem Item in TB.Items )
if (ReferenceEquals( this, Item.Tag ))
For_Erase.Add(Item);

foreach ( var Item in For_Erase )
TB.Items.Remove(Item);
});
}
}
I will post some updates if I discover something else. :D


Return to “MultiCharts .NET”