Page 1 of 1
Help with Comparing Strings in Windows form app
Posted: Tue Mar 12, 2013 1:33 am
by Mr.Wilson
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
Re: Help with Comparing Strings in Windows form app
Posted: Wed Mar 13, 2013 12:05 am
by mandai
If you are using the CLR runtime for the Forms App then the code might look something like this:
Code: Select allprivate: 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");
}
}
Re: Help with Comparing Strings in Windows form app
Posted: Thu Mar 14, 2013 1:37 am
by Mr.Wilson
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.
Re: Help with Comparing Strings in Windows form app
Posted: Thu May 23, 2013 7:51 am
by Mr.Wilson
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
Re: Help with Comparing Strings in Windows form app
Posted: Thu May 23, 2013 10:48 pm
by mandai
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()
Re: Help with Comparing Strings in Windows form app
Posted: Fri May 24, 2013 6:35 am
by Mr.Wilson
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