C++ Button Enable\Disable Issue

Started by
4 comments, last by PyroBlizzard 18 years, 10 months ago
ok, im about to pull my hair out over this. I have an event handler, supposed to check if a textbox is empty or not, and enable a button if its not empty. Well, it will enable the button, but it wont disable it. Here is the function
private: System::Void tAddressBox_TextChanged(System::Object *  sender, System::EventArgs *  e)
		 {
			 if (!(tAddressBox->Text == ""))
			 {
				 bPingServer->Enabled = true;
				 return;
			 }
			 bPingServer->Enabled = false;
		 }
Now, I know that the code should be right, unless im missing something incredibly obvious. I have even done breakpoints in the debug, and the debugger shows plain as day
Quote:tAddressBox->Text "" String*
So, im lost. I dont know what im doing wrong. Some help, please.
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?
Advertisement
So I take it that it never even enters the if statement? Or does it enter but the enable just doesn't work?

Also:

It looks like you're using C#, which I've never used. But I know with C/C++ you have to use a function like strcmp (string compare) when comparing the contents of a string. Otherwise, you'd be comparing "" to the address of that string. But that assumes it's a simple char*, so who knows. Just tossing it out there.

Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
Yeah, unless tAddressBox->Text is a string object (std::string), you need to use strcmp() or equivelant to compare the strings.
Quote:Original post by matthughson
So I take it that it never even enters the if statement? Or does it enter but the enable just doesn't work?

Also:

It looks like you're using C#, which I've never used. But I know with C/C++ you have to use a function like strcmp (string compare) when comparing the contents of a string. Otherwise, you'd be comparing "" to the address of that string. But that assumes it's a simple char*, so who knows.


Bolded for emphisis. == works fine if you're using std::string.

I've not used C#/.NET either, but check if there's an "empty()" member of text or similar. It's possible that [empty()] and [== ""] are not quite the same.
Actually, PyroBlizzard is using Managed C++ and the string he's using is the System::String class.

The comparison should be written:

if( !tAddressBox->Text->Equals("") )
{
...
}

EDIT:

Or, as MaulingMonkey suggested, you could just use the string's empty attribute

if( !tAddressBox->Text->Empty )
{
...
}
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
wow, once again, JWalsh saves the day. Thank you all for your help, I really appreciate it. It was in fact a thing of needing an ->Equals section.
As I lay down, staring up at the stars in all their glory, I wonder.....WHERE THE F*** IS MY ROOF!?!?!?

This topic is closed to new replies.

Advertisement