I'm fairly new to visual C++ programming in a windows form and the code displayed below works fine. However my issue is as soon as the program is running it will display "failure" on label7 as soon as the button is clicked. I understand why it is doing this as "a" is not in the textbox at this time so it reads the else line and displays the message. When I enter "a" in the textbox everything works fine and label7 changes to the right message. Is there a way around this problem?
Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
label1->Text = "Question 1: test";
label2->Text = "A. test";
label3->Text = "B. test";
label4->Text = "C. test";
label5->Text = "D. test";
label7->Text = "You are currently on £0!";
String ^ test = textBox1->Text;
if (test == "a" || test == "A")
{
label7->Text = "Well done, you are currently on £500!";
}
else label7->Text = "Failure";
}
When I run the program and press the start button "Failure" under label7 is shown. It is only suppose to be shown if the user inputs anything other than "a" in the textbox. However if i input "a" it will change label7 to the correct text.
So my problem is it displaying failure when nothing has been entered.
if (System::String::IsNullOrEmpty(test))
{
label7->Text = "Whatever you want when there's nothing in the textbox";
}
elseif (test == "a" || test == "A")
{
label7->Text = "Well done, you are currently on £500!";
}
else
{
label7->Text = "Failure";
}