获取ListBox C++的所选项目ID

Get Selected Item ID of ListBox C++

本文关键字:选项 项目 ID ListBox C++ 获取      更新时间:2023-10-16

我试图制作一个简单的程序,在ListBox中列出几个名称,当选择其中一个时,单击一个按钮应该会在几个TextBox中加载数据。。。我只需要列表框中所选项目的ID,因为数据在数组中,没有该ID我无法获得信息。这是我的代码:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             this->listBox1->Items->Clear();
             if (sCount != 0) { 
                for (int i = 1; i <= sCount; i++) { 
                    String^ entry = gcnew System::String(s[i].Show().c_str()); 
                    this->listBox1->Items->Add(entry); //Listing the items from the array: s
                } 
             }
         }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
         s[++sCount].InsertStudent("Name",270,50); //This is how i'm adding items in the array
     }
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
         int i = this->listBox1->SelectedItem; //Trying to get the ListBox Item ID
         String^ entry = gcnew System::String(s[i].Show().c_str()); //Getting the Item from the array
         this->textBox1->Text = entry; //Placing the array item into the textBox1
     }

附言:我想这样做,因为数组有类,我在一个ID中放了多个项,但在ListBox中只列出了其中一个。有人能帮忙吗?提前感谢:-)

我自己找到了答案:)

以下是我搜索的内容,如果有人来此帖子:

int i = this->listBox1->SelectedIndex;