Help with Comparing Strings in Windows form app

Post your questions regarding programming in C++ in here.
6 posts Page 1 of 1
Contributors
User avatar
Mr.Wilson
New Member
New Member
Posts: 24
Joined: Sat Apr 23, 2011 3:42 pm

I am trying to compare two strings in Windows Form App and I am having lots of difficulty.
Step 1
I want the program to read in a line of text into a label.
Step 2
Program should focus on textbox and wait for user to input
Step 3
Once form accept button (Enter in this case) has been pressed program should compare text in the label to text in the texbox.
and make a decision on results of comparison

I have found the syntax of form apps to be quite different than console/win32 apps and has thrown me.

I am using Visual Studio C++ 2010 express

Thank You in advance

Mr. Wilson

Also as I am new to Form Apps all the comments you can spare in the example will be much appreciated
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

If you are using the CLR runtime for the Forms App then the code might look something like this:
Code: Select all
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

				 String^ line = IO::File::ReadAllText("test.txt");//read a text file for the line
				 
				 label1->Text = line;

				 textBox1->Select();

			 }

private: System::Void btnAccept_Click(System::Object^  sender, System::EventArgs^  e) {

			 if (label1->Text == textBox1->Text)
			 {
				MessageBox::Show("matches");
			 }
			 else
			 {
				MessageBox::Show("does not match");
			 }

		 }
User avatar
Mr.Wilson
New Member
New Member
Posts: 24
Joined: Sat Apr 23, 2011 3:42 pm

Thank You for your suggestions. The code you suggested worked perfectly. I tried to rep you but site said I needed to spread points around first, so I'm guessing we've talked before. I can't rep you but thanks anyway.

I'll leave this topic open for a few days "just in case" But consider it solved.
User avatar
Mr.Wilson
New Member
New Member
Posts: 24
Joined: Sat Apr 23, 2011 3:42 pm

I am having some difficulty with C++ Windows Form Apps in Visual Studio Express 2010.
I am trying to compare two strings, one being read in into a label object and the second being keyed in buy the user.
When comparing these strings, a random space will appear at the end of the textbox causing the comparison to fail.
This random space does not occur every time but when it occurs, it is always on the end of the textbox string. I have double checked file being read in and double checked my double checking, those strings are correct. I have attempted the Trim() method without result, though not sure the syntax is correct on the Trim(). Any help getting rid of this space at the end of the textbox input would appreciated.
Code: Select all

private: System::Void btnCompare_Click(System::Object^  sender, System::EventArgs^  e) {
			// String^ Trim()
			 //String^ Trim(lblOutput->Text)
			// txtInput->Text = trim(" ");

		  if ((txtInput->Trim()) == lblOutput->Text)
          {
				txtInput->Text = (" ");
				lblOutput->Text = (" ");
			    fileNum += 1;
			    String^ line = IO::File::ReadAllText("Lesson" + fileNum + ".txt");//read a text file for the line
				lblOutput->Text = line;
			  // MessageBox::Show("matches");
          }

          else


          {
            MessageBox::Show("Does not match");
			txtInput->Text = (" ");
          }

		 }



Thank You in advance
Mr.Wilson
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

It looks like the above code will set txtInput->Text to contain a space character - it is not clear why you put brackets around the string (" ") .

It doesn't make much sense to use txtInput->Trim() as there is no such function. You are probably looking to use txtInput->Text->Trim()
User avatar
Mr.Wilson
New Member
New Member
Posts: 24
Joined: Sat Apr 23, 2011 3:42 pm

Yet again mandai, your advice is golden and works like a charm.


I changed the code from above to the following

Code: Select all

		  if ((txtInput->Text->Trim()) == lblOutput->Text)
          {
				txtInput->Text = (" ");
				lblOutput->Text = (" ");
			    fileNum += 1;
			    String^ line = IO::File::ReadAllText("Lesson" + fileNum + ".txt");//read a text file for the line
				lblOutput->Text = line;
			  // MessageBox::Show("matches");
          }

          else


          {
            MessageBox::Show("Does not match");
			txtInput->Text = ("");
          }

Again the site will not let me Rep you because you were the last person I rep ed
Thumbs up from me

Thank you,
Mr.Wilson
6 posts Page 1 of 1
Return to “General coding help”