new to MFC, trying to change a label based on an if statement using a button

closed account (2EwbqMoL)
hey all, Im new to using visual studio and MFC, i've been trying to use simple functions I know to get a hang of things and make a simple Hello world program with a little twist to get acquainted...

not going to copy/paste all the files, but I'll do a quick overview of what Ive tried so far...

I got success changing it once by establishing my variable as a private: CString txt; then on initiation of the dialog box using txt = "This is a Test"; and then changing that with OnBnClickedOk(){ txt = "Hello World!!!": UpdateDate (FALSE);}

but I decided to try to switch it back and forth between This is a Test... and Hello World!!!

I tried using OnBnClickedOK(){
if (txt="This is a Test...){
txt = "Hello World!!!";
}
else if (txt = "Hello World!!!){
txt = "This is a Test...";
}

that didnt work at all, in any way

I also tried linking the txt variable to an integer established in the same header file I established the CString txt... then on initiation of the dialog I tried both establishing what the int (I called it state) did, I used an if statement there, and also a similar if statement above in the button click function to try and add or subtract to that number, or set it to 1 or 2 directly.

when I say link... I mean I tried to set what txt stored based on a number value of 1 or 2, trying to use that as a switch... which never worked...

for some reason, if I didnt set state to 1 in the initiation I got a little bit of a random response where it would set it to 1 or 2 at compile (once I got hello world without setting it to anything!!!)

but either way, it seems to be ignoring the button clicks when I do that... they SHOULD be adding to the state if it is 1 or subtracting if it is 2, or just setting it to 2 if its 1... etc...

but nothing happens, and the label variable does not get changed, or it does and its not updating (even though I used UpdateData(FALSE);... I tried TRUE too, that does nothing... ever...

is there a reason my if statements are not working in a Win32 MFC Application? or am I just declaring these functions in a wrongful, ineffecient way or something? You'd think Id be able to use an if statement within the button click function to try and establish different results of a button click...

Last edited on
closed account (2EwbqMoL)
I just tried the same project in C++ Builder... because I got both to experiment with (I find C++ Builder less advanced, but easier to use in general... got an old version from a friend to test)

but it seems to work if I establish the if statements all within the button function on borland and then switch the state AND the label with one if statement...

__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Label1->Caption = "This is a Test...";
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (state == 0) {
Label1->Caption = "This is a Test...";
state = 1;
}
else if (state == 1) {
Label1->Caption = "Hello World!!!";
state = 0;
}
}

I believe a similar arrangement will work in Visual C++ 2013 MFC (with obviously different functions) although I'll have to do a test run...

to be honest, I dont find much of a difference, other than C++ builder (I know embarcadero now, not borland, but I keep thinking that...) makes it WAYYYY easier to figure out and edit the variables involved with windows forms and edit them than MFC...

but I guess I could make use of either much more easily than windows API directly...
closed account (2EwbqMoL)
Indeed the same process DOES work in Visual Studio 2013...

makes sense seeing as the other function I defined it in at first is no longer running again, while the button function is called each time I click on it...

I do find Borland MUCHHH easier to use than MFC... But I've heard bad things. I've yet to check the compiled finished products to compare optimization...

but the version of C++ builder I have here isnt hte latest, and microsofts compiler is... so its a bit biased I suppose.

I'll probably end up using C++ builder for API applications more often though, as it seems more cut and dry how the variables are established, and even though the code is a bit screwy looking compared, it generates itself as long as I use their IDE...

although I guess, really, all things are about even except for having to right click/establish variables or write them in by hand and associate it with the related dialog box/button in visual studio... its just RIGHT THERE in C++ Builder, and much quicker, easier to access..

I find the code, while a little more awkward looking, is a bit cleaner as well, but I havent used either for long programs yet...
Don't map your button as a CString and rely on that Update business. Instead map it as a CButton. Then your button handlers will work.

As buttons are windows, you set the text with SetWindowText.
Last edited on

but I guess I could make use of either much more easily than windows API directly...


You've got that completely backwards. Using the Windows API directly is, in my not so humble opinion, much easier than using any of the Windows Class Frameworks such as MFC or Borland's various products. Just look at your situation as you've described it above. You are attempting to do something completely trivial that amounts to nothing more than a couple lines of code. But you're stuck. I wouldn't say these class frameworks are helping you much. I'm not trying to be mean or nasty. Its just that I hate to see folks struggling with convoluted code trying to do something trivial. If you would describe in more detail just what you are trying to do, I'd post an api app doing it.
Last edited on
Topic archived. No new replies allowed.