[TUTORIAL] Windows forms
[TUTORIAL] Windows forms
12 Jun 2021, 21:12
Hi,
There is little documentation how to interact with forms created in Visual Studio, somenone can create a "how to do this" tutorial for future references?
1) Create in VS a form (named "MainForm") with:
- Gray background
- ComboBox with some symbol's name's
- TextBox for Volume
- TextBox for Label
- Current Ask and Bid price's for user reference
- Buy and Sell button's (with the respective green and orange colors).
2) When the Buy or Sell button is pressed, another form appears (named "formTrade") saying "Trade was successfully executed.", this one will have a close button.
3) Load the form "MainForm" in cTrader and execute it on OnStart().
With this tutorial it will be possible learn:
- Load and interact with fully forms created in VS
- Load information contained in cTrader in the form (Ask and Bid prices)
- Execute cTrader instructions via form (Buy and Sell button's)
I really appreciate any suport!!
Sincerely,
DelFonseca
Replies
DelFonseca
14 Jun 2021, 20:27
RE:
amusleh said:
Hi,
To use WinForms on your cBot/Indicator, you have to follow these steps:
- (...)
Open it with VS to see the cBot full code.
Thank you so much for your hel, it was perfect!!
@DelFonseca
jumel.donatien
20 Jun 2021, 09:44
( Updated at: 20 Jun 2021, 09:45 )
RE:
Hi amusleh and thank you for your help!
In order to understand your "tutorial", I downloaded your sample code, added the extracted folder on my robots folder, but it doesn't appear on my robots list in cTrader..
Where is the .algo file? or how could I create it to test your robot?
Thank you for your help.
@jumel.donatien
jumel.donatien
20 Jun 2021, 23:20
RE:
Hi amusleh,
I have another question: When I launch the robot, the form opens and if I want to change the action of the Buy Button (for example) to draw text on chart, it doesn't work.. I think, it's because when the form is launched, the chart is not activated..
Do you have a solution?
Here is the code changed in MainForm.cs:
private void BtnBuy_Click(object sender, System.EventArgs e)
{
_robot.ExecuteMarketOrder(TradeType.Buy, _robot.SymbolName, _robot.Symbol.VolumeInUnitsMin);
_robot.ChartObjects.DrawText("Sample Text", "Sample Text", StaticPosition.Center);
}
Thank you in advance!
@jumel.donatien
amusleh
21 Jun 2021, 14:51
RE: RE:
jumel.donatien said:
Hi amusleh,
I have another question: When I launch the robot, the form opens and if I want to change the action of the Buy Button (for example) to draw text on chart, it doesn't work.. I think, it's because when the form is launched, the chart is not activated..
Do you have a solution?
Here is the code changed in MainForm.cs:
private void BtnBuy_Click(object sender, System.EventArgs e)
{
_robot.ExecuteMarketOrder(TradeType.Buy, _robot.SymbolName, _robot.Symbol.VolumeInUnitsMin);_robot.ChartObjects.DrawText("Sample Text", "Sample Text", StaticPosition.Center);
}Thank you in advance!
Hi,
Invoke it on robot main thread:
private void BtnBuy_Click(object sender, System.EventArgs e)
{
_robot.ExecuteMarketOrder(TradeType.Buy, _robot.SymbolName, _robot.Symbol.VolumeInUnitsMin);
_robot.BeginInvokeOnMainThread(() => _robot.Chart.DrawStaticText("Sample Text", "Sample Text", VerticalAlignment.Bottom, API.HorizontalAlignment.Center, Color.Red));
}
@amusleh
jumel.donatien
21 Jun 2021, 21:19
RE: RE: RE:
That's great! Thank you.
One last question: is it possible to open a new chart by clicking on this button from a windowsform?
I tried with "SendKeys.SendWait (" ^ (N) ");" like simulating a shortcut key: but it only works when the main ctrader window is active, but not when the Windows form is open.
Thanks to you, I am now able to call actions by clicking on a button like drawing text as you showed me, but unable to open a new graph or activate the main ctrader window from the form Windows.
Do you have a solution for this? Thank you
@jumel.donatien
amusleh
13 Jun 2021, 08:27
Hi,
To use WinForms on your cBot/Indicator, you have to follow these steps:
As an example you can check my WinForms Test cBot sample:
Clone it from Github or download it as a zip file, extract the content on a new folder inside your cTrader cAlgo/Robots folder, it will appear in your cTrader automate cBots list, re-build it and run it.
Open it with VS to see the cBot full code.
@amusleh