Winforms 在C++中扩展类(System .Windows窗体.Trabox)

Winforms 在C++中扩展类(System .Windows窗体.Trabox),winforms,c++-cli,derived-class,Winforms,C++ Cli,Derived Class,在C语言中,我经常喜欢创建一个名为IntTextBox的自定义类,它只允许文本属性中存在有效的整数 public class IntTextBox : TextBox { string origin = "0"; //A string to return to if the user-inputted text is not an integer. public IntTextBox() { Text = "0"; TextChan

在C语言中,我经常喜欢创建一个名为IntTextBox的自定义类,它只允许文本属性中存在有效的整数

public class IntTextBox : TextBox
{
    string origin = "0";
    //A string to return to if the user-inputted text is not an integer.
    public IntTextBox()
    {
        Text = "0";
        TextChanged += new EventHandler(IntTextBox_TextChanged);
    }
    private void IntTextBox_TextChanged(object sender, EventArgs e)
    {
        int temp;
        if(int.TryParse(Text,out temp))
        //If the value of "Text" can be converted into an integer.
        {
            origin = Text;
            //"Save" the changes to the "origin" variable.
        }
        else
        {
            Text = origin;
            //Return to the previous text value to remove invalidity.
        }
    }
}
<>我试图在C++中模仿这一点,没有错误是显而易见的,但是当我试图把它添加到我的窗体时,VisualStudio说无法加载项目“ItTrimBox”。它将从工具箱中删除。这是我到目前为止尝试过的代码

public ref class IntTextBox : public System::Windows::Forms::TextBox
{
    public:
        IntTextBox()
        {
            Text = "0";
            TextChanged += gcnew System::EventHandler(this, &AIMLProjectCreator::IntTextBox::IntTextBox_TextChanged);
        }
    private:
        String^ origin = "0";
        System::Void IntTextBox_TextChanged(System::Object^ sender, System::EventArgs^ e)
        {
            int temp;
            if (int::TryParse(Text, temp))
            {
                origin = Text;
            }
            else
            {
                Text = origin;
            }
        }
};

很可能您的C++/CLI项目设置为生成混合模式程序集,该程序集部分是独立于CPU的CIL MSIL,部分是本机代码。本机代码是特定于体系结构的,这意味着您必须为32位x86或64位x64重新编译它

如果C++/CLI DLL是与Visual Studio不同的体系结构,则设计器无法加载它


尝试编译x86,以便使用设计模式。

这不是C++ +“重复”。@它不是以什么方式C++?我正在尝试让它在C++中工作。它是C++ + CLI,它是连接标准C++和C以及其他.NET的微软语言。我想我是RePro。您可以通过再次启动VS并使用“调试”>“附加到进程”附加到第一个进程来调试设计时异常。让它在所有CLR异常上中断。当我尝试它时,它失败并出现FileLoadException,因为我不喜欢加载无法验证的EXE程序集。解决方法是在其自己的类库项目中拆分该类,在主项目中添加对该类的引用。@Hans:啊,是的,混合模式EXE不是位置独立的,因此不能像纯MSIL EXE那样加载到Visual Studio的地址空间。谢谢,但代码存在于同一项目中,我不想单独编译然后导入它。@Rariolu;没关系,它仍然需要加载到VisualStudio中,以便设计者放置它。并且只能将32位DLL加载到Visual Studio中。64位不能,对于C++/CLI,除非使用/clr:pure,否则没有任何CPU,正如Hans在评论中指出的,EXE也不能。