C++ Visual Studio - textBox oraz comboBox

0

Witam.
Jestem początkującym, tworzę jakieś podstawowe aplikacje w Visual Studio C++ z użyciem forms.

Posiadam textBox, w który wpisuję ilość złotych, comboBox w którym wybieram Euro/Funty/Dolary, oraz label.
Po wyborze itemu w comboBoxie oraz wpisaniu wartości w textBox wyświetla mi się na labelu przewalutowana wartość.

  1. Chciałbym aby program wyświetlał MessageBox::Show("Wprowadź wartość","Uwaga",MessageBoxButtons::OK) jeżeli do textBoxu nie jest wpisana żadna wartość. Najlepiej, aby program to robił w momencie, gdy ktoś wybiera któryś z itemów w comboBoxie. Coś na zasadzie

"if (textbox jest pusty albo wartość jest równa zero)
{messagebox}
else
{wybiera się item oraz wykonują się instrukcje}

Próbowałem różnych sposobów szukając po internecie, nie działało...

  1. Jak zablokować [szary/wyłączony] comboBoxa, jeżeli żadna wartość nie jest wpisana do textBoxa?
#pragma once

namespace Project9 {

	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 MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Button^  button1;

	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::GroupBox^  groupBox1;
	private: System::Windows::Forms::RadioButton^  radioButton4;
	private: System::Windows::Forms::RadioButton^  radioButton3;
	private: System::Windows::Forms::RadioButton^  radioButton2;
	private: System::Windows::Forms::RadioButton^  radioButton1;

	private: System::ComponentModel::IContainer^  components;
	protected:

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#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->label1 = (gcnew System::Windows::Forms::Label());
			this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
			this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton3 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton4 = (gcnew System::Windows::Forms::RadioButton());
			this->groupBox1->SuspendLayout();
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->Location = System::Drawing::Point(52, 74);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(174, 16);
			this->label1->TabIndex = 0;
			this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
			// 
			// comboBox1
			// 
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^  >(3) { L"Euro", L"Dolar", L"Funt" });
			this->comboBox1->Location = System::Drawing::Point(135, 36);
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(100, 21);
			this->comboBox1->TabIndex = 1;
			this->comboBox1->Text = L"Wybierz walutę";
			this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &MyForm::comboBox1_SelectedIndexChanged);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(29, 36);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 20);
			this->textBox1->TabIndex = 2;
			this->textBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(111, 323);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 3;
			this->button1->Text = L"Close";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(26, 9);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(85, 13);
			this->label2->TabIndex = 5;
			this->label2->Text = L"Przelicznik walut";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(12, 130);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(171, 13);
			this->label3->TabIndex = 6;
			this->label3->Text = L"Jaki jest symbol chemiczny cynku\?";
			// 
			// groupBox1
			// 
			this->groupBox1->Controls->Add(this->radioButton4);
			this->groupBox1->Controls->Add(this->radioButton3);
			this->groupBox1->Controls->Add(this->radioButton2);
			this->groupBox1->Controls->Add(this->radioButton1);
			this->groupBox1->Location = System::Drawing::Point(12, 162);
			this->groupBox1->Name = L"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(105, 127);
			this->groupBox1->TabIndex = 7;
			this->groupBox1->TabStop = false;
			// 
			// radioButton1
			// 
			this->radioButton1->AutoSize = true;
			this->radioButton1->Location = System::Drawing::Point(6, 19);
			this->radioButton1->Name = L"radioButton1";
			this->radioButton1->Size = System::Drawing::Size(38, 17);
			this->radioButton1->TabIndex = 0;
			this->radioButton1->TabStop = true;
			this->radioButton1->Text = L"Zn";
			this->radioButton1->UseVisualStyleBackColor = true;
			this->radioButton1->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton1_CheckedChanged);
			// 
			// radioButton2
			// 
			this->radioButton2->AutoSize = true;
			this->radioButton2->Location = System::Drawing::Point(6, 42);
			this->radioButton2->Name = L"radioButton2";
			this->radioButton2->Size = System::Drawing::Size(38, 17);
			this->radioButton2->TabIndex = 1;
			this->radioButton2->TabStop = true;
			this->radioButton2->Text = L"Cu";
			this->radioButton2->UseVisualStyleBackColor = true;
			this->radioButton2->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton2_CheckedChanged);
			// 
			// radioButton3
			// 
			this->radioButton3->AutoSize = true;
			this->radioButton3->Location = System::Drawing::Point(6, 65);
			this->radioButton3->Name = L"radioButton3";
			this->radioButton3->Size = System::Drawing::Size(35, 17);
			this->radioButton3->TabIndex = 2;
			this->radioButton3->TabStop = true;
			this->radioButton3->Text = L"Cr";
			this->radioButton3->UseVisualStyleBackColor = true;
			this->radioButton3->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton3_CheckedChanged);
			// 
			// radioButton4
			// 
			this->radioButton4->AutoSize = true;
			this->radioButton4->Location = System::Drawing::Point(6, 88);
			this->radioButton4->Name = L"radioButton4";
			this->radioButton4->Size = System::Drawing::Size(39, 17);
			this->radioButton4->TabIndex = 3;
			this->radioButton4->TabStop = true;
			this->radioButton4->Text = L"Na";
			this->radioButton4->UseVisualStyleBackColor = true;
			this->radioButton4->CheckedChanged += gcnew System::EventHandler(this, &MyForm::radioButton4_CheckedChanged);
			// 
			// MyForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(312, 358);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->comboBox1);
			this->Controls->Add(this->label1);
			this->Name = L"MyForm";
			this->Text = L"5,7";
			this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
			this->groupBox1->ResumeLayout(false);
			this->groupBox1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
		
	
			
			float a;
			try
			{
				
				a = Single::Parse(textBox1->Text);
				switch (comboBox1->SelectedIndex)
				{
				case 0:
					label1->Text = (a*4.5).ToString();
					break;
				case 1:
					label1->Text = (a*4.2).ToString();
					break;
				case 2:
					label1->Text = (a*5.2).ToString();
					break;
				}
			}
			catch (FormatException^ e)
			{

			}
		

	}
	private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		
		float a;
		try
		{
			
			
				a = Single::Parse(textBox1->Text);
				switch (comboBox1->SelectedIndex)
				{
				case 0:
					label1->Text = (a*4.5).ToString();
					break;
				case 1:
					label1->Text = (a*4.2).ToString();
					break;
				case 2:
					label1->Text = (a*5.2).ToString();
					break;
				}
			
		}
		catch (FormatException^ e)
		{

		}

		
	}
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
		if
			(MessageBox::Show("Are you sure you want to close this application?", "Close", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes)
		{
			Application::Exit();
		}
	
	}

	private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) {
	}
	private: System::Void radioButton1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
		label3->Text = "1";
	}
private: System::Void radioButton2_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
	label3->Text = "0";
}
private: System::Void radioButton3_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
	label3->Text = "0";
}
private: System::Void radioButton4_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
	label3->Text = "0";
}
		
};
}

Z góry dziękuję za pomoc!

1
  1. skoro i tak już masz try-kacza to wyświetlaj MessageBoxa w tym catch (FormatException^ e).
  2. w TextChanged textboxa ustawiasz odpowiednio comboBox->Enabled.
0
Azarien napisał(a):
  1. skoro i tak już masz try-kacza to wyświetlaj MessageBoxa w tym catch (FormatException^ e).
  2. w TextChanged textboxa ustawiasz odpowiednio comboBox->Enabled.

Z pierwszym poradziłem sobie trochę inaczej, mianowicie:

	if (textBox1->TextLength==0|| textBox1->Text=="0")
			
			{
				
				MessageBox::Show("Wprowadź wartość", "Uwaga",MessageBoxButtons::OK,MessageBoxIcon::Warning);
				label1->Text = "";
			}
		else
		{INSTRUKCJA, KTÓRA MA SIE WYKONAC}

Natomiast co do drugiego to nie udało mi się. Po kliknięciu w item z comboBoxa faktycznie wyszarza się i pozostaje wyszarzone na np. Euro, ale po wpisaniu w textBox jakiejś wartości nie chce się " odszarzyć". Nie do końca rozgryzłem jeszcze jak to ogarnąć.

if(textBox1->TextLenght==0)
{
comboBox1->Enabled=false;}
else
{}

Jest ktoś w stanie pomóc?

0

po wpisaniu w textBox jakiejś wartości nie chce się " odszarzyć". Nie do końca rozgryzłem jeszcze jak to ogarnąć.

czy masz w visualu jakąś funkcję która zwraca wartość bool "textBoxa" w którego coś wpisujesz ? Gdyby taka funkcja była, to mógłbyś zrobić prosty warunek np przy założeniu że funkcja nazywa się "isEmpty"

if(!textBox->isEmpty()){
   //kod aktywacji
}
else{
   //kod deaktywacji 
}
2

Opowiem dowcip.

W żydowskiej koszernej knajpie w Nowym Jorku ortodoksyjnych gości obsługują imigranci z Chin, i ku zdziwieniu, Chińczycy bardzo dobrze posługują się językiem jidisz.
Zainteresowany jeden z gości zaczepia właściciela, pyta o to, a ten odpowiada: Cśśśśś, oni myślą, że uczą się po angielsku.

Kol @cepluspluswariat, ty się nie uczysz C++, tylko koszmarnego (zresztą już martwego) C++/CLI, który jest innym językiem.
https://4programmers.net/Forum/C_i_.NET/196733-o_naduzywaniu_c++cli

0
AnyKtokolwiek napisał(a):

Opowiem dowcip.

W żydowskiej koszernej knajpie w Nowym Jorku ortodoksyjnych gości obsługują imigranci z Chin, i ku zdziwieniu, Chińczycy bardzo dobrze posługują się językiem jidisz.
Zainteresowany jeden z gości zaczepia właściciela, pyta o to, a ten odpowiada: Cśśśśś, oni myślą, że uczą się po angielsku.

Kol @cepluspluswariat, ty się nie uczysz C++, tylko koszmarnego (zresztą już martwego) C++/CLI, który jest innym językiem.
https://4programmers.net/Forum/C_i_.NET/196733-o_naduzywaniu_c++cli

Jest to forma zajęć z programowania jaką narzucił nam prowadzący, nie ja tworzę tok nauczania

0
MarekR22 napisał(a):

C++CLI nie jest przeznaczony do pisania aplikacji, ale do tworzenia mostów pomiędzy kodem C++ a .Net czyli C#. Ego C++CLI nie jest martwy, ale od samego początku i z definicji niszowy.

@MarekR22 w takim razie po co Microsoft zrobił designer Windows Forms dla C++/CLI, sugerując jego używanie do GUI?
W dodatku za czasów Visuali 2005, 2008 i 2010 był darmowe wersje Express dzielone językami (czyli osobno C++ Express, osobno C# Express i VB Express).
Do tego w VC++Express nie było MFC do tworzenia GUI, więc kto ściągnał tylko C++ (bo go ceszarpy nie interesowały) do klepania formatek widział tylko Windows Forms.

Więc chyba nie "od samego początku i z definicji niszowy", a przynajmniej nie tak im to wyszło.
Dopiero w VS 2012 usunięty został template nowego projektu WinForms dla C++/CLI. Sama funkcjonalność pozostała (nawet w 2019) ale projekt trzeba tworzyć na około (utworzyć konsolowy, dodać formę, usunąć konsolę, przestawić kilka rzeczy).

1 użytkowników online, w tym zalogowanych: 0, gości: 1