C++/CLI How to add Text and Value to Combobox ?

Σωτηρης Σωτηρης 21 Reputation points
2022-05-31T11:44:09.617+00:00

Hello
how to add Text and Value to Combobox ?
here my code:


//ComboboxItem.h
namespace I2CProg {
 using namespace System;

 public ref class ComboboxItem
 {
 String^ _text = "";
 Object^ _value;

   public: property String^ Text
   {
      String^ get()
      {
         return _text;
      }
      void set(String^ value)
      {
         _text = value;
      }
   }
   public:  property Object^ Value
   {
     Object^ get()
     {
        return _value;
     }
     void set(Object^ value)
     {
       _value = value;
     }
   }
   public:  String^ ToString() override
   {
    return Text;
   }
 };
}

//Main.cpp
#include "MainForm.h"
#include "ComboboxItem.h"
using namespace System;
using namespace System::Windows::Forms;

void MainForm::LoadChipSize()
{
    cbx_ChipSize->Items->Clear();

    cbx_ChipSize->Items->Add(gcnew ComboboxItem{ Text = "Chip Size", Value = 0 });  //<-- Error C2065 'Value': undeclared identifier and Error C3673 'I2CProg::ComboboxItem': class does not have a copy - constructor

}

the name of combobox is cbx_ChipSize
and here code in MainForm.h

//
private: System::Windows::Forms::ComboBox^ cbx_ChipSize;
//
this->cbx_ChipSize = (gcnew System::Windows::Forms::ComboBox());
// cbx_ChipSize
//
this->cbx_ChipSize->DisplayMember = L"Text";
this->cbx_ChipSize->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
this->cbx_ChipSize->FormattingEnabled = true;
this->cbx_ChipSize->Location = System::Drawing::Point(66, 22);
this->cbx_ChipSize->Name = L"cbx_ChipSize";
this->cbx_ChipSize->Size = System::Drawing::Size(77, 21);
this->cbx_ChipSize->TabIndex = 1;
this->cbx_ChipSize->ValueMember = L"Value";
// 

any help pls ty!!!

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,526 questions
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2022-05-31T12:56:49.817+00:00

    Try something like this:

    public ref class ComboboxItem
    {
    public:
       ComboboxItem( String^ text, Object^ value )
       {
          _text = text;
          _value = value;
       }
       . . . .
    };
    
    . . . .
    
    cbx_ChipSize->Items->Add( gcnew ComboboxItem( "Chip Size", 0) );
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful