在 C++ 中扩展类(System.Windows.Forms.TextBox)

问题描述 投票:0回答:2

在 C# 中,我经常喜欢创建一个名为“IntTextBox”的自定义类,它只允许有效整数存在于“Text”属性中。

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++ 中模仿这个并且没有明显的错误,但是当我尝试将它添加到我的表单时,Visual Studio 说“无法加载项目'IntTextBox'。它将从工具箱中删除。这是我的代码到目前为止已经尝试过。

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;
            }
        }
};
winforms c++-cli derived-class
2个回答
1
投票

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

如果C++/CLI DLL是与Visual Studio不同的架构,设计者无法加载它

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


0
投票
#pragma once
using namespace System;
using namespace System::Windows::Forms;
namespace wfext {
    public ref class IntTextBox : TextBox {
        String^ origin = "0";
        //A string to return to if the user-inputted text is not an integer.
    public: IntTextBox(){
        this->Text = "0";
        TextChanged += gcnew EventHandler(this, &IntTextBox::IntTextBox_TextChanged);
    }
    private: void IntTextBox_TextChanged(Object^ sender, EventArgs^ e){
        int temp;
        if (int::TryParse(this->Text, temp)){   //If the value of "Text" can be converted into an integer.
            origin = this->Text;
            //"Save" the changes to the "origin" variable.
        }
        else{
            this->Text = origin;
            //Return to the previous text value to remove invalidity.
        }
    }
    };
}
© www.soinside.com 2019 - 2024. All rights reserved.