C++ Combobox selection and values

I need a way to set value of a int depending on the selection of the combobox. This is in C++ Windows forms.


Like i will have

int KO_END;

and if they select Pop from combo box then KO_END = 1;
if soda is selected then KO_END = 2;

I have 50 selections for my combobox.

i tried using
map<string, int> comboMap;
comboMap["bow"] = 120;
comboMap["arrow"] = 63;

KO_END = comboMap[comboBox1->SelectedItem];

but i cant see how to use the map in windwos forms MFC
LVL 2
Gibson_JunkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You can hanlde the SelectedIndexChanged event of the ComboBox and inside this function, set your variable. For the example below, the variable is defined locally, which I'm sure you don't want. Also, I am setting the variable to the value of the index, but you can perform logic to set your values accordingly.
private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e)
{
	int KO_END = this->comboBox1->SelectedIndex;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gibson_JunkAuthor Commented:
I will have a look at it. But not quiet sure about the setting values thing. I knew how to make KO_END be the value of the selection i just need to understand how to make the value of the selection a certain number for each option
0
AndyAinscowFreelance programmer / ConsultantCommented:
if you have the following: pop , soda, bow, arrow

Do they get sorted in the combo and appear as
arrow
bow
pop
soda

Then what value do you want?
is it arrow = 1, bow = 2 ...
or the original pop = 1, soda = 2...
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Gibson_JunkAuthor Commented:
If i have a list in my combo box.

like this

63
Arrow
Auto
Bow
Armor

i want
63 = 63
Arrow = 120
Auto = 170
Bow = 154

there is no value order. But all of the items in my list box have a certain value i need them to be
0
käµfm³d 👽Commented:
This sounds like you want to maintain a Dictionary of keys and values. You can load your dictionary when the app starts, then use the keys from the dictionary to load the combo box. Once the box is loaded, hanlde the SelectedIndexChanged event (or one of the other SelectedXXX events of ComboBox). Inside this handler, you can index the dictionary using the text for the selected item--since the text was generated using the keys from the dictionary.

I am including an example below. I took the liberty of adding the code from your previous question. The new version of RetrieveInfo() returns a Dictionary instead of a String.

I cannot test this code at present because I am behind a FW that will not allow me to your test address.
#pragma once


namespace TestForm {

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

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//

			this->list = RetrieveInfo(GetPageSource("http://kingofknight.com/category.php?type=70"));

			for each (String^ key in this->list->Keys)
			{
				this->comboBox1->Items->Add(key);
			}
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;
		System::Windows::Forms::ComboBox^  comboBox1;
		int KO_END;

		// Internal map of keys to values
		System::Collections::Generic::Dictionary<String^, int> ^list;

		String^ GetPageSource(String^ URL2)
		{
			System::Net::WebClient^ client;

			client = gcnew System::Net::WebClient();
			String^ source = client->DownloadString(URL2);

			return source;
		}

		System::Collections::Generic::Dictionary<String^, int>^ RetrieveInfo(String^ pageSource)
		{
			System::Collections::Generic::Dictionary<String^, int>^ returnedData;
			System::Text::RegularExpressions::Regex^ reg;
			System::Text::RegularExpressions::MatchCollection^ matches;

			returnedData = gcnew System::Collections::Generic::Dictionary<String^, int>();

			// Create a Regex object with a special pattern to look for a particularly formed string
			reg = gcnew System::Text::RegularExpressions::Regex("(?i)ajax:/tips\\.php\\?id=(\\d+)[^>]+>([^<]+)</span>");
			// Execute the regex against the input string
			matches = reg->Matches(pageSource);

			for each (System::Text::RegularExpressions::Match^ match in matches)
			{
				// Concatenate results into one string separated by newlines
				//  You do not have to do this, this was just how I returned them. The point is that your data will be found
				//  in each Match object in the MatchCollection returned from reg->Matches(); within each Match object, two
				//  capture groups should be present--group 1 represents the id and group 2 represents the text
				try
				{
					returnedData->Add(match->Groups[1]->Value, System::Convert::ToInt32(match->Groups[2]->Value));
				}
				catch (Exception^ e)
				{
					continue;
				}
			}

			return returnedData;

			//////////////////////////////////////////////////////////////////////////////////////////////////////////////
			// Explanation of pattern:
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////
			// (?i)                   - Make the search case-insensitive
			// ajax:/tips\\.php\\?id= - Find the literal string "ajax:/tips.php?id="
			// (\\d+)                 - Find one-or-more digits and capture them in a group--group #1
			// [^>]+>                 - Find one-or-more characters which are not the greater-than, then find a greater-than
			// ([^<]+)                - Find one-or-more characters which are not the less-than and capture them in a group--group #2
			// </span>                - Find the literal string "</span>"
		}


#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)
		{
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->SuspendLayout();
			// 
			// comboBox1
			// 
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Location = System::Drawing::Point(159, 12);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(121, 21);
			this->comboBox1->TabIndex = 0;
			this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(292, 266);
			this->Controls->Add(this->comboBox1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);

		}
#pragma endregion

	private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e)
			 {
				 // Index the internal list using the text from the selected item in the combo box
				 this->KO_END = this->list[this->comboBox1->SelectedText];
			 }
	};
}

Open in new window

0
Gibson_JunkAuthor Commented:
Here, This link http://kingofknight.com/category.php?type=  has a combo or dropdown box. When you click the links in it you see the new url there is a number at the end like

http://kingofknight.com/category.php?type=70

i try to replicate this in my combobox. So when they click on lets say arrow the value is 120 like in that combobox
0
Gibson_JunkAuthor Commented:
I tried this.

std::map<std::string, int>* comboMap;
      comboMap["63"] = 63;
      comboMap["Arrow"] = 120;

and get these errors


Error	1	error C2057: expected constant expression	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	20
Error	2	error C2466: cannot allocate an array of constant size 0	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	20
Error	3	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	20
Error	4	error C2372: 'ItemGrabber::comboMap' : redefinition; different types of indirection	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	20
Error	5	error C2440: 'initializing' : cannot convert from 'int' to 'int []'	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	20
Error	6	error C2057: expected constant expression	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	21
Error	7	error C2466: cannot allocate an array of constant size 0	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	21
Error	8	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	21
Error	9	error C2372: 'ItemGrabber::comboMap' : redefinition; different types of indirection	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	21
Error	10	error C2440: 'initializing' : cannot convert from 'int' to 'int []'	c:\users\zachary\documents\visual studio 2010\projects\item grabber\item grabber\Form1.h	21

Open in new window

0
Gibson_JunkAuthor Commented:
Ok IDK why i didn't think of this from the getgo but i solved. Just made a class and override the string. Thanks for the help.
0
Gibson_JunkAuthor Commented:
I did take not of this Dictionary thing. Thanks for all your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.