C++ Visual Studio ComboBox 获取选中值教程
在Visual Studio中直接实现获取ComboBox中的选中值的方法如下:
-
打开Visual Studio,创建一个Windows桌面应用程序。
-
在窗体设计器中添加一个ComboBox控件,命名为comboBox1。
-
双击窗体,进入窗体的代码视图。
-
在窗体的头文件中添加以下代码:
#include <string> //添加头文件
using namespace std; //使用命名空间
//声明变量
wstring selectedValue;
- 在窗体的源文件中添加以下代码:
//获取ComboBox中选中的值
selectedValue = comboBox1->SelectedItem->ToString();
- 在需要获取ComboBox中选中值的地方调用以上代码即可。
完整代码示例:
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Project1::MyForm form;
Application::Run(% form);
}
//获取ComboBox中选中的值
wstring selectedValue;
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
selectedValue = comboBox1->SelectedItem->ToString();
}
原文地址: https://www.cveoy.top/t/topic/jmtn 著作权归作者所有。请勿转载和采集!