无法在 C++/CLI 中从 System::Object 转换为 System::Windows::Forms::MenuItem

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

我正在处理我的 C++/CLI 项目,因为我正在添加带有单击事件的子菜单项。我在获取菜单项的文本时遇到问题,因为当我尝试这样做时:

Debug::WriteLine("SmtpEmail............" + sender);

我将其作为返回输出:

System.Windows.Forms.MenuItem, Items.Count: 0, Text: hello

所以我必须从字符串转换为系统菜单项。

当我尝试这个时:

MenuItem^ selectedItem = (MenuItem)sender;
Debug::WriteLine("SmtpEmail............" + selectedItem.Text->ToString());

我收到错误:

'type cast' : cannot convert from 'System::Object ^' to 'System::Windows::Forms::MenuItem'

错误在这一行跳跃:

MenuItem^ selectedItem = (MenuItem)sender;

它说我无法从 'System::Object ^' 转换为 'System::Windows::Forms::MenuItem'。

完整代码如下:

   private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
         RegistryKey^ rkCurrentUser = Registry::CurrentUser;
         RegistryKey^ rkInternetAccountManager = rkCurrentUser->OpenSubKey("Software\\Microsoft\\Internet Account Manager\\Accounts");

         
         for each (String^ getkeysNames in rkInternetAccountManager->GetSubKeyNames()) {
             if (getkeysNames->Contains("0000000")) {
                 RegistryKey^ myKey = rkCurrentUser->OpenSubKey("Software\\Microsoft\\Internet Account Manager\\Accounts\\"+getkeysNames->ToString());
                     
                 String^ AccountName = myKey->GetValue("Account Name")->ToString();
                 String^ POP3Server = myKey->GetValue("POP3 Server", "")->ToString();
                 String^ IMAPServer = myKey->GetValue("IMAP Server", "")->ToString();
                 String^ HTTPMailServer = myKey->GetValue("HTTPMail Server", "")->ToString();
                 String^ SmtpDisplayName = myKey->GetValue("SMTP Display Name")->ToString();
                 String^ SmtpEmail = myKey->GetValue("SMTP Email Address")->ToString();


                 Debug::WriteLine("getkeys............" + getkeysNames->ToString());
                 Debug::WriteLine("AccountName............" + AccountName);
                 Debug::WriteLine("SmtpDisplayName............" + SmtpDisplayName);
                 Debug::WriteLine("SmtpEmail............" + SmtpEmail);

                 if (SmtpEmail->Length > 0) {
                     menuItem121->MenuItems->Add(SmtpEmail)->Click += gcnew EventHandler(this, &Form1::MenuItem_Click);
                 }
                 
                 

                 //Close the Registry.
                 myKey->Close();
             }
         }
    }

    private: void MenuItem_Click(Object^ sender, EventArgs^ e) {
         
         MenuItem^ selectedItem = (MenuItem)sender;

    
         // Access the clicked item here..
         //String text = menuitem->Text; // I guess it's called text(?)
         Debug::WriteLine("SmtpEmail............" + selectedItem.Text->ToString());
         //Debug::WriteLine("SmtpEmail............" + sender);


    }

您能否向我展示一个示例,如何从系统对象\字符串转换为菜单项,以便我可以获得菜单项的文本?

visual-c++ c++-cli menuitem
1个回答
0
投票

C++/CLI 中的引用类型与对象句柄运算符 (^) 一起使用。

因此这一行:

MenuItem^ selectedItem = (MenuItem)sender;

应改为:

//--------------------------------V--------
MenuItem^ selectedItem = (MenuItem^)sender;

请注意,这在 C# 中相当于:

MenuItem selectedItem = (MenuItem)sender;

所以你的错误行是 C# 和 C++/CLI 语法的混合。

© www.soinside.com 2019 - 2024. All rights reserved.