Hi!
I have a problem. I cannot pass text chosen in combox to text field..
The problem is: how to do that if all menu items from combobox are read from cfg file.

ComboBox:

// 
			// systemBOX
			// 
			this->systemBOX->FormattingEnabled = true;
			resources->ApplyResources(this->systemBOX, L"systemBOX");
			this->systemBOX->Name = L"systemBOX";
			StreamReader^ plik= gcnew StreamReader("CONFIG\\systems.ini",System::Text::Encoding::Default);
		    while (linia=plik->ReadLine()) 
		    {
		    this->systemBOX->Items->Add(linia);
		    }
			this->systemBOX->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);

code for action after clicking the item in combobox
* systemBOX is a name of the combobox
* systemTEXT is a name of the textfield where I want to see option chosen from combobox

private: System::Void systemBOXComboBoxMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 systemTEXT->Text=((MenuItem^)sender)->Text;
		 }

Thx for help!

Dani AI

Generated

Short answer: the form never ran the handler you edited, and C++/CLI is case‑sensitive. The InitializeComponent hookup in your posted code was pointing at a different method name, so selecting an item did not call the method you changed. Also remember the managed string method is ToString() (capital T) and SelectedItem can be null, so guard against that.

The simplest, robust pattern is to write a handler that uses the sender parameter (so one handler can serve multiple ComboBoxes) and checks SelectedIndex/SelectedItem before reading text. This avoids depending on control names in the handler hookup and makes it obvious whether the event fired.

private: System::Void systemBOX_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
    System::Windows::Forms::ComboBox^ cb = safe_cast<System::Windows::Forms::ComboBox^>(sender);
    if (cb != nullptr && cb->SelectedIndex != -1 && cb->SelectedItem != nullptr)
        systemTEXT->Text = cb->SelectedItem->ToString()->Trim();
    else
        systemTEXT->Clear();
}

Troubleshooting checklist:

  • Verify InitializeComponent binds the event to the exact handler name you edited (method names are case sensitive). If unsure, delete the handler wiring and let the designer regenerate it by double‑clicking the ComboBox.
  • Use the debugger (breakpoint in the handler) to confirm it fires. If you prefer quick checks, show a short MessageBox or write to the debug output.
  • Prefer using the ComboBox Text property if you only care about the visible string; use SelectedItem when you need the underlying object.
  • If you load items from a file, trim whitespace when adding entries to avoid unexpected leading/trailing spaces.

This addresses the issues hit and follows the direction suggested, with a safer, maintainable handler pattern you can reuse.

Recommended Answers

All 14 Replies

Change your event to systemBOXComboBoxMenuItem_Click to systemBOXComboBoxMenuItem_SelectedIndexChanged

systemText->Text = systemBOXComboBox->SelectedItem->ToString()

I need to look it up, but it seems like ->SelectedText does not work

I recive 2 errors:

Error	3	error C2227: left of '->SelectedItem' must point to class/struct/union/generic type
Error	2	error C2065: 'systemBOXComboBoxMenu' : undeclared identifier

Sorry, it should be whatever the name of your combobox is, systemBoxComboBox instead of systemBoxComboBoxMenu (I copy/pasted too far)

so this part should look like that:

private: System::Void systemBOXcomboBox_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 systemTEXT->Text=systemBOXcomboBox->SelectedItem->toString();
}

I still have some errors :(

Error	4	error C2227: left of '->toString' must point to class/struct/union/generic type
Error	3	error C2227: left of '->SelectedItem' must point to class/struct/union/generic type
Error	2	error C2065: 'systemBOXcomboBox' : undeclared identifier

What is your ComboBox called? If that's not the right name, change it to whatever it is named on your form. In fact, probably should erase that event handler code and let the IDE regenerate it for you. Double click on the combobox on the form and the proper handler will be placed in the code automatically. Then, use the SelectedItem of that combobox and it's ToString() method.

Just a suggestion, but if you don't need .NET 4.0 stuff, you can get away with using VC++ 2008 Express Edition, which has much better intellisense for C++/CLI than 2010 does (shows you their priorities).

Thx for your patience...c++/cli for me is really pain in the ass...But I think I wont avoid programming in it, because I dunno how to add pure c++/console code to vc++ project (linker errors).

I have replaced everything as you said and i recive only one error now:

Error	2	error C2039: 'toString' : is not a member of 'System::Object'

Btw..Thx for your patience mate!...WIll try to install VC express too.

whole code is:

#pragma once


namespace Templater {

	using namespace std;
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::IO;


	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	protected: 
	private: System::Windows::Forms::ToolStripMenuItem^  typeToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  availabilityToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  unavailabilityToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  degradationToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  aboutToolStripMenuItem;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::ComboBox^  systemBOX;
	private: System::Windows::Forms::TextBox^  DescrPL;


	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  descrENG;

	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::DateTimePicker^  dateTimePicker1;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Button^  button1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;
	private: System::Windows::Forms::TextBox^  systemTEXT;


	private: System::Windows::Forms::ComboBox^  affiliateBOX;

	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::TextBox^  affiliateTEXT;


		// zmienne globalne
		System::String^ linia;
		// zmienne

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary> 
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->typeToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->availabilityToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->unavailabilityToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->degradationToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->systemBOX = (gcnew System::Windows::Forms::ComboBox());
			this->DescrPL = (gcnew System::Windows::Forms::TextBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->descrENG = (gcnew System::Windows::Forms::TextBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->dateTimePicker1 = (gcnew System::Windows::Forms::DateTimePicker());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->systemTEXT = (gcnew System::Windows::Forms::TextBox());
			this->affiliateBOX = (gcnew System::Windows::Forms::ComboBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->affiliateTEXT = (gcnew System::Windows::Forms::TextBox());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->typeToolStripMenuItem, 
				this->aboutToolStripMenuItem});
			resources->ApplyResources(this->menuStrip1, L"menuStrip1");
			this->menuStrip1->Name = L"menuStrip1";
			// 
			// typeToolStripMenuItem
			// 
			this->typeToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->availabilityToolStripMenuItem, 
				this->unavailabilityToolStripMenuItem, this->degradationToolStripMenuItem});
			this->typeToolStripMenuItem->Name = L"typeToolStripMenuItem";
			resources->ApplyResources(this->typeToolStripMenuItem, L"typeToolStripMenuItem");
			// 
			// availabilityToolStripMenuItem
			// 
			this->availabilityToolStripMenuItem->Name = L"availabilityToolStripMenuItem";
			resources->ApplyResources(this->availabilityToolStripMenuItem, L"availabilityToolStripMenuItem");
			this->availabilityToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::availabilityToolStripMenuItem_Click);
			// 
			// unavailabilityToolStripMenuItem
			// 
			this->unavailabilityToolStripMenuItem->Name = L"unavailabilityToolStripMenuItem";
			resources->ApplyResources(this->unavailabilityToolStripMenuItem, L"unavailabilityToolStripMenuItem");
			this->unavailabilityToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::unavailabilityToolStripMenuItem_Click);
			// 
			// degradationToolStripMenuItem
			// 
			this->degradationToolStripMenuItem->Name = L"degradationToolStripMenuItem";
			resources->ApplyResources(this->degradationToolStripMenuItem, L"degradationToolStripMenuItem");
			this->degradationToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::degradationToolStripMenuItem_Click);
			// 
			// aboutToolStripMenuItem
			// 
			this->aboutToolStripMenuItem->Name = L"aboutToolStripMenuItem";
			resources->ApplyResources(this->aboutToolStripMenuItem, L"aboutToolStripMenuItem");
			this->aboutToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::aboutToolStripMenuItem_Click);
			// 
			// label1
			// 
			resources->ApplyResources(this->label1, L"label1");
			this->label1->Name = L"label1";
			this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
			// 
			// systemBOX
			// 
			this->systemBOX->FormattingEnabled = true;
			resources->ApplyResources(this->systemBOX, L"systemBOX");
			this->systemBOX->Name = L"systemBOX";
			StreamReader^ plik= gcnew StreamReader("CONFIG\\systems.ini",System::Text::Encoding::Default);
		    while (linia=plik->ReadLine()) 
		    {
		    this->systemBOX->Items->Add(linia);
		    }
			this->systemBOX->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);
			// 
			// DescrPL
			// 
			resources->ApplyResources(this->DescrPL, L"DescrPL");
			this->DescrPL->Name = L"DescrPL";
			// 
			// label2
			// 
			resources->ApplyResources(this->label2, L"label2");
			this->label2->Name = L"label2";
			// 
			// descrENG
			// 
			resources->ApplyResources(this->descrENG, L"descrENG");
			this->descrENG->Name = L"descrENG";
			// 
			// label3
			// 
			resources->ApplyResources(this->label3, L"label3");
			this->label3->Name = L"label3";
			// 
			// dateTimePicker1
			// 
			this->dateTimePicker1->Format = System::Windows::Forms::DateTimePickerFormat::Short;
			resources->ApplyResources(this->dateTimePicker1, L"dateTimePicker1");
			this->dateTimePicker1->Name = L"dateTimePicker1";
			// 
			// label4
			// 
			resources->ApplyResources(this->label4, L"label4");
			this->label4->Name = L"label4";
			// 
			// button1
			// 
			resources->ApplyResources(this->button1, L"button1");
			this->button1->Name = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			// 
			// systemTEXT
			// 
			resources->ApplyResources(this->systemTEXT, L"systemTEXT");
			this->systemTEXT->Name = L"systemTEXT";
			// 
			// affiliateBOX
			// 
			this->affiliateBOX->FormattingEnabled = true;
			resources->ApplyResources(this->affiliateBOX, L"affiliateBOX");
			this->affiliateBOX->Name = L"affiliateBOX";
			StreamReader^ plik1= gcnew StreamReader("CONFIG\\affiliates.ini",System::Text::Encoding::Default);
		    while (linia=plik1->ReadLine()) 
		    {
		    this->affiliateBOX->Items->Add(linia);
		    }
			this->affiliateBOX->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);
			// 
			// label5
			// 
			resources->ApplyResources(this->label5, L"label5");
			this->label5->Name = L"label5";
			// 
			// affiliateTEXT
			// 
			resources->ApplyResources(this->affiliateTEXT, L"affiliateTEXT");
			this->affiliateTEXT->Name = L"affiliateTEXT";
			// 
			// Form1
			// 
			resources->ApplyResources(this, L"$this");
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->Controls->Add(this->affiliateTEXT);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->affiliateBOX);
			this->Controls->Add(this->systemTEXT);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->dateTimePicker1);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->descrENG);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->DescrPL);
			this->Controls->Add(this->systemBOX);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->menuStrip1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
			this->MainMenuStrip = this->menuStrip1;
			this->Name = L"Form1";
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void availabilityToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->Text="Notifier 1.0 - Availability";
		 }
private: System::Void unavailabilityToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->Text="Notifier 1.0 - Unvailability";
		 }
private: System::Void degradationToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->Text="Notifier 1.0 - Degradation";
		 }
private: System::Void aboutToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 MessageBox::Show("Notifier 1.0 by Kolendo, Michal","About",MessageBoxButtons::OK,MessageBoxIcon::Information);
		 }
private: System::Void systemBOX_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			 systemTEXT->Text=systemBOX->SelectedItem->toString();
			 
		 }
};
}

toString

It's ToString() (case counts). The intellisense in 2008 is nice, as you can just type your object's name and then the -> and it drops down a little menu to select from. There's some in 2010, but I don't think it works for everything.

Hmm...Now there is no Errors at all...
program is compiled...but it doesnt work...
I see menu items in systemBOX...when I choose something, it isnt copied to systemTEXT field :(

private: System::Void systemBOX_SelectedIndexChanged (System::Object^  sender, System::EventArgs^  e) {
			 systemTEXT->Text=systemBOX->SelectedItem->ToString();
		 }

Within that same event handler, put in

MessageBox::Show(systemBOX->SelectedItem->ToString());

and see if the messagebox pops up with the string, to make sure the event is firing.

It doesnt show anything.

So that event handler isn't firing at all. Go through the code and check all of the event handler hookups this->systemBOX->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged); as this particular one is hooked up wrong.

What is comboBox1? You want systemBOX's SelectedIndexChanged event to be associated with systemBOX_SelectedIndexChanged (the event handler you have in your last post) not comboBox1_SelectedIndexChanged.

I'm happy to help you, but you've got to keep track of your own code, and if you don't understand the mechanics of these things you'll have to do some reading on them, or at least rely on the IDE to put them in the right spot for you.

You were asking about sites before, the functionx.com site is decent, but it's definitely the most comprehensive out there ()

thx for your help..
I am thinking about switching to C#....
as i said before..program wont be complicated...I already did all things in console version of c++...but I need windows gui for it...
C# is way more popular and I see plenty of boards with examples...and it looks a lot of easier..than VC++..
is it good idea to switch in your opinion?
thx for your help and sorry for being soo nooobish ;)

It's my understanding that it's a perfectly viable option to use a native C++ DLL with an interface designed in C#. I'd say go for it. It's not a problem that you're noobish.

btw...everything works now :) thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.