Where is my code problem on cTrader Customise Window Close and ReOpen
Where is my code problem on cTrader Customise Window Close and ReOpen
26 May 2024, 00:53
Hello anyone can help or cTrader Support Team,
I have the below code to enable me to use the shortcut ‘Alt+M’ to open a customised window, ‘Alt+N’ to hide and ‘Alt+B’ to close.
Why I couldn't reopen the window by ‘Alt+M’ after closing the window by ‘Alt+B’ or clicking the ‘X’ in the right-top corner?
Please kindly advise,
Thanks.
using cAlgo.API;
namespace cAlgo
{
[ Indicator( IsOverlay=true, TimeZone=TimeZones.UTC, ScalePrecision=1, AccessRights = AccessRights.None ) ]
public class TestWindow2 : Indicator
{
private readonly Window wn_CWvFlt = new() { Title="Currency Wave...", Topmost=true };
private readonly Canvas cv_canvas = new() { Height=600, Width=800, Margin="0 0 0 0", BackgroundColor=Color.FromHex("22000000"), HorizontalAlignment=HorizontalAlignment.Center, VerticalAlignment=VerticalAlignment.Center };
private readonly Button bt_Cncl = new() { Text="Hide" , Top=500, Left=200 };
protected override void Initialize()
{
cv_canvas.AddChild(bt_Cncl);
wn_CWvFlt.Child = cv_canvas;
bt_Cncl.Click += bt_Cncl_Click;
Chart.KeyDown += Chart_KeyDown;
}
public override void Calculate(int index) { }
private void Chart_KeyDown(ChartKeyboardEventArgs obj)
{
if (obj.AltKey)
{
if (obj.Key.Equals(Key.M ) ) { wn_CWvFlt.Show(); }
else if (obj.Key.Equals(Key.N ) ) { wn_CWvFlt.Hide(); }
else if (obj.Key.Equals(Key.B ) ) { wn_CWvFlt.Close(); }
}
}
private void bt_Cncl_Click(ButtonClickEventArgs obj) { wn_CWvFlt.Hide(); }
}
}
Replies
Capt.Z-Fort.Builder
26 May 2024, 10:44
RE: Where is my code problem on cTrader Customise Window Close and ReOpen
PanagiotisCharalampous said:
Hi there,
If you close the window, then it does not exist anymore, you would need to reinitialize it. Once a window is closed, the same object instance can't be used to reopen the window.
Best regards,
Panagiotis
Thanks Panagiotis,
ChatGPT helped me optimised the code as below, and it works well now:
using cAlgo.API;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, ScalePrecision = 1, AccessRights = AccessRights.None)]
public class TestWindow2 : Indicator
{
private Window wn_CWvFlt;
private Canvas cv_canvas;
private Button bt_Cncl;
protected override void Initialize()
{
CreateWindow();
Chart.KeyDown += Chart_KeyDown;
}
public override void Calculate(int index) { }
private void CreateWindow()
{
wn_CWvFlt = new Window { Title = "Currency Wave...", Topmost = true };
cv_canvas = new Canvas { Height = 600, Width = 800, Margin = "0 0 0 0", BackgroundColor = Color.FromHex("22000000"), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
bt_Cncl = new Button { Text = "Hide", Top = 500, Left = 200 };
cv_canvas.AddChild(bt_Cncl);
wn_CWvFlt.Child = cv_canvas;
bt_Cncl.Click += bt_Cncl_Click;
}
private void Chart_KeyDown(ChartKeyboardEventArgs obj)
{
if (obj.AltKey)
{
if (obj.Key.Equals(Key.M))
{
if (wn_CWvFlt == null || !wn_CWvFlt.IsVisible)
{
CreateWindow();
wn_CWvFlt.Show();
}
}
else if (obj.Key.Equals(Key.N))
{
wn_CWvFlt?.Hide();
}
else if (obj.Key.Equals(Key.B))
{
wn_CWvFlt?.Close();
wn_CWvFlt = null;
}
}
}
private void bt_Cncl_Click(ButtonClickEventArgs obj) { wn_CWvFlt?.Hide(); }
}
}
@Capt.Z-Fort.Builder
PanagiotisCharalampous
26 May 2024, 07:29
Hi there,
If you close the window, then it does not exist anymore, you would need to reinitialize it. Once a window is closed, the same object instance can't be used to reopen the window.
Best regards,
Panagiotis
@PanagiotisCharalampous