• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Jak sprawdzić czy textBox jest pusty

Aruba Cloud - Virtual Private Server VPS
0 głosów
562 wizyt
pytanie zadane 11 maja 2020 w C i C++ przez laytenek Nowicjusz (210 p.)

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 google, nie działało...
 

2. Jak zablokować [ wyszarzyć/wyłączyć] comboBoxa, jeżeli żadna wartość nie jest wpisana do textBoxa? 


Z góry dziękuję za pomoc!

 

#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";
}
		
};
}

 

komentarz 11 maja 2020 przez DragonCoder Nałogowiec (36,500 p.)
1. Nie wiem jak w samym win forms jest to rozwiazane, ale w momencie proby wcisniecia buttona pobierasz wartosc textBoxa, zmieniasz na string i sprawdzasz czy jest rowny == "";. Jesli tak, to wyswietlasz messageBox.

2. To juz bsrdziej praca z sygnalami, sprawdz czy nie ma tam zadnej metody typu changed, ktora informuje Cie, czy cos sie zmienilo wewnatrz textBox. Czyli:

Na poczatku ustawiasz comboBox read/write, czy jaka metode ma tam windows, na false, w momencie ktorym textBox change jest true, to wlaczasz spowrotem comboBoxa. Oczywiscie sprawdzaslbym tu dlugosc podanego stringa, bo jesli wyniesie ona znowu 0, to znowu musisz go wylaczyc
komentarz 11 maja 2020 przez laytenek Nowicjusz (210 p.)

@DragonCoder,

Co do pierwszego problemu poradziłem sobie w ten sposób:
 

#pragma endregion
	private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
		
		if (textBox1->TextLength==0|| textBox1->Text=="0")
			
			{
				
				MessageBox::Show("Wprowadź wartość", "Uwaga",MessageBoxButtons::OK,MessageBoxIcon::Warning);
				label1->Text = "";
			}
			
			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)
			{

			}

Na pewno dało się to zrobić prościej niż 
if (textBox1->TextLength==0|| textBox1->Text=="0")
ale nie mam zielonego pojęcia jak :/

Co do drugiego problemu nadal pozostaje nierozwiązany.

1 odpowiedź

0 głosów
odpowiedź 11 maja 2020 przez OdsetekGlupoty Pasjonat (15,360 p.)

Witam,

1. Nie dało się zrobić prościej niż krótki if, który sprawdza czy pole jest puste.

2. Nie wiem jak to wygląda w C++ (ja programowałem w C#), ale na pewno podobnie. Najpierw zmieniasz właściwość Enabled dla comboBoxa na false. Następnie Szukasz eventu, który się wykonuje zawsze wtedy, gdy pole tekstowe jest zmienione. Później w kodzie tego eventu sprawdzasz, czy tekst tego pola jest pusty i jeśli nie, zmieniasz w kodzie Enabled dla comboBoxa na true, a jeśli tak, to zmieniasz Enabled na true.

 

Podobne pytania

0 głosów
1 odpowiedź 270 wizyt
pytanie zadane 27 stycznia 2020 w C# przez Kamilos1906 Nowicjusz (240 p.)
0 głosów
2 odpowiedzi 327 wizyt
0 głosów
1 odpowiedź 288 wizyt
pytanie zadane 17 kwietnia 2016 w C i C++ przez Dominik Gola Użytkownik (580 p.)

93,329 zapytań

142,323 odpowiedzi

322,400 komentarzy

62,662 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj

Wprowadzenie do ITsec, tom 1 Wprowadzenie do ITsec, tom 2

Można już zamawiać dwa tomy książek o ITsec pt. "Wprowadzenie do bezpieczeństwa IT" - mamy dla Was kod: pasja (użyjcie go w koszyku), dzięki któremu uzyskamy aż 15% zniżki! Dziękujemy ekipie Sekuraka za fajny rabat dla naszej Społeczności!

...