Cannot add Winform to .Net 6 cBot

Created at 15 Sep 2022, 14:27
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
EY

eynt

Joined 08.05.2020

Cannot add Winform to .Net 6 cBot
15 Sep 2022, 14:27


From my .net  6 cBot (default code) I've added a reference to WinForm class library which contains a form and its code is empty. To the OnStart Method I added the following code:

WinFormsLibrary1.Form1 f = new WinFormsLibrary1.Form1();
string s = f.ToString();

I got the following error:

 

Severity    Code    Description    Project    File    Line    Suppression State
Error    CS0012    The type 'Form' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.    
 

I've added a reference to the dll that WinFormsLibrary1 uses:

C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\6.0.9\ref\net6.0\System.Windows.Forms.dll

 

and the code:

f.ShowDialog();

 

but got the following error:

15/09/2022 14:40:32.641 | CBot instance [New cBot, EURUSD, h1] started.
15/09/2022 14:40:33.547 | Crashed in OnStart with BadImageFormatException: Cannot load a reference assembly for execution.
15/09/2022 14:40:33.594 | CBot instance [New cBot, EURUSD, h1] crashed with error "Crashed in OnStart with BadImageFormatException: Cannot load a reference assembly for execution."
 

 

 

Please help

 

 

 

 


@eynt
Replies

PanagiotisCharalampous
19 Sep 2022, 10:23

Hi yuval.ein.

Here is a guide on how to use WinForns in .NET 6.0 cBots/Indicators

https://help.ctrader.com/ctrader-automate/guides/ui/WinForms/#opensave-file-dialogs

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

eynt
20 Sep 2022, 13:16 ( Updated at: 21 Dec 2023, 09:22 )

RE:

Thank you,

 

I've done the changes described in the link you provided so now I can use Winforms on my cBot. However, when I added a reference to my old dll which is called utils I got the following error:

I also added a screenshot of Windows.Forms refereces both from the cBot and the utils. I can see there's a difference but I'm not sure how to fix it without making a problem. It's also prefered that the cBot will use the newer version inseatd of Utils to use the loder one.

 

 

Severity    Code    Description    Project    File    Line    Suppression State
Error    CS1705    Assembly 'Utils' with identity 'Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Windows.Forms, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' which has a higher version than referenced assembly 'System.Windows.Forms' with identity 'System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'    New cBot (2)    C:\Users\yuval\Documents\cAlgo\Sources\Robots\New cBot (2)\New cBot (2)\CSC    1    Active
 

 

cbot:

 

 

utiils


@eynt

PanagiotisCharalampous
20 Sep 2022, 13:28

Hi yuval.ein.

The message indicates the problem. You use a library which uses a version fo the assembly that is higher than the one you use in the cBot.

Best Regards,

Panagiotis 

Join us onTelegram andFacebook


@PanagiotisCharalampous

eynt
20 Sep 2022, 13:38

RE:

Hi

 

I understand that. I also updated the original post and uploaded screenshots of the versions of the references. It seems that when a new cBot is created it uses version 6.0.0.0 which is old. Is there a way to make a new cBot to automatically use the highest version available?

 

 

 


@eynt

eynt
20 Sep 2022, 14:12

RE: RE:

I also don't understand how can I upgrade the version on the cbot or downgrade the version on the Utils project


@eynt

PanagiotisCharalampous
20 Sep 2022, 14:21 ( Updated at: 21 Dec 2023, 09:22 )

Hi yuval.ein.

You can find the different versions in the folder below

Best Regards,

Panagiotis 

Join us onTelegram and Facebook


@PanagiotisCharalampous

eynt
20 Sep 2022, 14:41 ( Updated at: 20 Sep 2022, 14:45 )

RE:

Hi

 

Yes, I can navigate to the folder, however:

1. On my computer there are only version 6.0.8 and up, no 6.0.0

2. More importantly, I don't know how to change the version of the framework. It seems the framework properties is read only, there is no edit/add/remove option.

 


@eynt

eynt
21 Sep 2022, 11:28

RE: RE:

Hi

 

How do I change the framework from 6.0.0 to 6.0.8 or from 6.0.8 to 6.0.0?

 

Thanks


@eynt

PanagiotisCharalampous
21 Sep 2022, 11:59

Hi yuval.ein.

You don't need to have the same folders. You just need to use the same assembly from the same folder for both projects.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

eynt
21 Sep 2022, 13:25 ( Updated at: 21 Dec 2023, 09:22 )

RE:

 

Hi

I'm sorry but I don't understand your answer.

The bot's framework Microsoft.WindowsDesktop.App.WindowsFroms was set automatically when the bot was created to C:\Users\yuval\.nuget\packages\microsoft.windowsdesktop.app.ref\6.0.0 with version 6.0.0.

As well the Utils's framework was set automatically when the project was created to C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\6.0.9 with version 6.0.9.

I did not find any way to change the framework on the bot nor the Utils. They were added automatically and i did not find a way to update them or remove them and add another one instead the one which was removed.

I tried to add a reference on the bot to C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\6.0.9\ref\net6.0\System.Windows.Forms.dll however that did not solve the problem.

 


@eynt

PanagiotisCharalampous
21 Sep 2022, 14:11

Hi yuval.ein,

Can you send me the solution at community@ctrader.com? I would like to have a look at this.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

eynt
21 Sep 2022, 15:29

RE:

Hi

 

I sent it by mail a minute ago. Please confirm that you received it, as I got some sort of a antivirus message

 


@eynt

eynt
22 Sep 2022, 09:04

RE: RE:

Hi

 

I tried to create a new bot with a new WinForm class library instead of the old one which was upgraded from .net4 and I got the same error.

I am sending you the solution by email now.

These are the steps I've made:

 

1. Created a new cBot and changed its cproj file so it can run with winforms

2. Created a new WinForm class library and added a form to it

3. Added the WinForm class library to the bot's solution and added its project reference

4.From the bot I tried creating an instance of the form and its ShowDialog method

5. rebuilt the solution

 

 

 

 

 

 

 

 


@eynt

eynt
25 Sep 2022, 13:12

RE: RE: RE:

Hi

 

Is there anything new on the subject?

 

Thanks


@eynt

PanagiotisCharalampous
26 Sep 2022, 10:58

Hi yuval.ein,

Unfortunately I have not received any email.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

eynt
28 Sep 2022, 01:09

RE:

Hello

 

The following contains a solution with the error above. GuiTester solution and project should have a project reference to WinFormsLibrary3 which will cause following error:

 

Severity    Code    Description    Project    File    Line    Suppression State
Error    CS1705    Assembly 'WinFormsLibrary3' with identity 'WinFormsLibrary3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Windows.Forms, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' which has a higher version than referenced assembly 'System.Windows.Forms' with identity 'System.Windows.Forms, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'    GuiTester    D:\Downloads\AssemblyError\GuiTester\GuiTester\CSC    1    Active
 

 

 

 

 


@eynt

eynt
29 Sep 2022, 12:26

RE: RE:

Hello

 

Were you able to download the solution and recreate the error?


@eynt

PanagiotisCharalampous
29 Sep 2022, 12:30

Hi yuval.ein,

I did but did not find a solution. Maybe try asking on StackOverflow.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

eynt
29 Sep 2022, 12:46

RE:

Hi

 

 

I am reattaching the solution link. I was able to download it from an external computer and open \AssemblyError\GuiTester\GuiTester.sln which has this error.

 

 

 

 


@eynt

PanagiotisCharalampous
29 Sep 2022, 12:48

Hi yuval.ein,

I mean I did not find a solution to your problem :)

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous