xsharp.eu • Creating a component fails when adding a visual custom class to a form
Page 1 of 1

Creating a component fails when adding a visual custom class to a form

Posted: Tue Jan 14, 2025 6:26 pm
by xinjie
Hi, Development Team

I'm having problems trying to add a custom visual class (myCheckBox) to a form.

I got the C# source code after asking the AI.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClassTest
{
    public class myCheckBox:CheckBox
    {
        public myCheckBox()
        {
            this.TabStop = true;
            this.KeyDown += myCheckBox_KeyDown;
        }

        private void myCheckBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                this.Checked = !this.Checked; 
                e.Handled = true; 
            }
            else if (e.KeyCode == Keys.Enter) 
            {
                this.Parent.SelectNextControl(this, true, true, true, true);
                e.Handled = true; 
            }
        }
    }
}
I then created the C# class library project and successfully built a DLL.
I then added the class to the toolbox and successfully added myCheckBox to a form in a C# winform project.

I used ILSpy to get the X# equivalent. And I managed to build the DLL using the foxpro dialect.
When I try to add the X# version of myCheckBox from the toolbox to the form I do see the error:
(The English description in the error message is the translated version via Deepl)

Code: Select all

Failed to create component “myCheckBox”. The error message is: “Description : Variable does not exist
 “Description : Variable does not exist
Subsystem : BASE
GenCode : EG_NOVAR Variable does not exist
FuncSym : MemVarGet
Severity : ES_ERROR
Can Default : False
Can Retry : False
Can Substitute : False
Argument Number : 1
Argument : cName
Arguments : {myCheckBox_KeyDown}
Stack Trace : {myCheckBox_KeyDown}	
   In XSharp.Error.VOError(UInt32 dwGenCode, String cFuncName, String cArgName, UInt32 iArgNum, Object[] aArgs)
   In XSharp.MemVar._Get(String cName)
   In XSharp.RT.Functions.__MemVarGet(String cName)
   In XSharp.RT.Functions.__VarGet(String cName)
   In tq.libs.myCheckBox..ctor() Location E:\ManhattanX\tq\tq.libs\myCheckBox.prg:Line No. 27
   in System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   in System.SecurityUtils.SecureConstructorInvoke(Type type, Type[] argTypes, Object[] args, Boolean allowNonPublic, BindingFlags extraFla... ”
myCheckBox.prg:Line No. 27 :

Code: Select all

This.KeyDown += myCheckBox_KeyDown
I'm not sure what's causing the current problem. bug or my code ?

Re: Creating a component fails when adding a visual custom class to a form

Posted: Tue Jan 14, 2025 11:18 pm
by Chris
HI xinjie,

Can you please zip and post the complete solution so we can have a look? This will help with reproducing for sure the problem, because with the forms designer there can be a lot of variables at play.

Re: Creating a component fails when adding a visual custom class to a form

Posted: Wed Jan 15, 2025 2:40 am
by xinjie
Hi, Chris

Attached is a DEMO that reproduces the described problem.
Demo.zip
(964.48 KiB) Downloaded 10 times

Re: Creating a component fails when adding a visual custom class to a form

Posted: Wed Jan 15, 2025 11:41 am
by Chris
Hi xinjie,

Thanks, I see the problem, it's because in this line:

This.KeyDown += myCheckBox_KeyDown

the compiler does not know what "myCheckBox_KeyDown" represents, so adds code that tries to resolve it at runtime (to a memvar or similar) and that fails. You can fix this by explicitly specifying that it's a member of the object with this:

this.KeyDown += this.myCheckBox_KeyDown

But I will open a ticket for this, maybe the compiler should had already figured out it's the object method even without "this."

Re: Creating a component fails when adding a visual custom class to a form

Posted: Wed Jan 15, 2025 11:46 am
by xinjie
HI, Chris

It now succeeds after following your advice. Thank you very much!