Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 564
  • Last Modified:

How do I make my dialog button DO SOMETHING?

Hello EE,

I just created a new dialog-based MFC App in VS 2008 where MFC is used in a shared DLL.  I called the project "Example".

I've used dialog editor to add a "button1" onto the Example dialog.  Now, I would like to make the button do SOMETHING.

Say, if I CLICK on "button1" I would like the program to add 1+1, and present the result ("2") in a popup message window.  How do I do this?

Thanks!
0
shaolinfunk
Asked:
shaolinfunk
  • 14
  • 6
  • 2
2 Solutions
 
shaolinfunkAuthor Commented:
Just to clarify, inside the Header Files folder I see: Example.h, ExampleDlg.h.
Inside the Source Files folder, I see: Example.cpp, ExampleDlg.cpp.
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
refer this tutorial
http://depts.washington.edu/cmmr/biga/chapter_tutorials/VC_Guide/CreatingDialogAppWithMFC7.pdf
http://www.functionx.com/visualc/controls/dialogbox.htm


After creating a dialog using the wizard. Open the dialog resource file using the IDE. Drag and drop the button control. Double click on the button control will create a function on its own and you could write your code there for adding and popup a message box (AfxMessageBox()) to show the result.
0
 
sarabandeCommented:
you could add a text box below the button with the resource editor and it will get the id IDC_EDIT1. then right-click on your button and choose 'Add EventHandler...'.

choose the BN_CLICKED event and create a member function 'OnBnClickedButton1' by choosing 'Add and Edit..' .

you will be directed to the dialog cpp file and can add

   GetDlgItem(IDC_EDIT1)->SetWindowText(_T("1+1=2"));

what should do what you expect.

Sara
 
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
shaolinfunkAuthor Commented:
Ok I followed what you're saying and wrote this:

void CExampleDlg::OnBnClickedButton1()
{
      // TODO: Add your control notification handler code here
      AfxMessageBox("Hello World");

}

When compiling I get this:

1>Compiling...
1>ExampleDlg.cpp
1>c:\documents and settings\administrator\desktop\example\example\exampledlg.cpp(156) : error C2665: 'AfxMessageBox' : none of the 2 overloads could convert all the argument types
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afxwin.h(5372): could be 'int AfxMessageBox(LPCTSTR,UINT,UINT)'
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afxwin.h(5374): or       'int AfxMessageBox(UINT,UINT,UINT)'
1>        while trying to match the argument list '(const char [12])'
1>Build log was saved at "file://c:\Documents and Settings\Administrator\Desktop\Example\Example\Debug\BuildLog.htm"
1>Example - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What did I do wrong?
0
 
shaolinfunkAuthor Commented:
the above comment was for Deepu.  I will try sarabande's suggestion now and post back.
0
 
sarabandeCommented:
you probably have unicode char set and should use

 AfxMessageBox(_T("Hello World"));

the _T macro makes L"Hello World" if your project settings are to use wide characters.

Sara
0
 
shaolinfunkAuthor Commented:
Hi SaraBande,

Yes you are correct.  With your modification I got Deepu's idea to work.  I think you should both get equal points...is it alright if I split it 50%/50%?

I still want to try your idea as well as I am new and learning this stuff.
0
 
shaolinfunkAuthor Commented:
Hi SaraBande,

I am trying your idea..I added a static text box...then right clicked to add event handler...but there is no option to choose BN_CLICKED event...this is what I see (snapshot)...what do I do here?
sara.jpg
0
 
sarabandeCommented:
if using static text box you need to change the properties. the ID of the static text control should be changed to IDC_STAT1 (or any other unique name). the IDC_STATIC what is the current id is mapped to -1 and cannot used in the GetDlgItem function.

for defining the event handler you need to right-click on the button, not on the static textbox.

the code for the event handler would change to

void CExampleDlg::OnBnClickedButton1()
{
    GetDlgItem(IDC_STAT1)->SetWindowText(_T("1+1=2"));
}

Open in new window


you see the id used in GetDlgItem is the id of your static text control

Sara
0
 
shaolinfunkAuthor Commented:
Hi Sara,

I changed the ID of the static text box to IDC_STAT1.

then I have this code:

void CExampleDlg::OnBnClickedButton1()
{
      // TODO: Add your control notification handler code here
      //AfxMessageBox(_T("Hello World"));
      
      GetDlgItem(IDC_STAT1)->SetWindowText(_T("1+1=2"));

}

but got this error:

1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Linking...
1>ExampleDlg.obj : error LNK2001: unresolved external symbol "public: void __thiscall CExampleDlg::OnStnClickedProgramstatus(void)" (?OnStnClickedProgramstatus@CExampleDlg@@QAEXXZ)
1>C:\Documents and Settings\Administrator\Desktop\Example\Debug\Example.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\Administrator\Desktop\Example\Example\Debug\BuildLog.htm"
1>Example - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What did I do wrong?
0
 
shaolinfunkAuthor Commented:
NEVERMIND!  I found the problem and deleted the erroneous code.  It worked with the static text box.

When you original said text box did you mean you wanted me to try and add an "Edit Control" text box?
0
 
shaolinfunkAuthor Commented:
I guess my question is really:  Which kind of "text box" did you want me to add when you said I should choose a BN_CLICKED event for the event handler?
 
0
 
sarabandeCommented:
yes, in my first post i assumed you to use a edit text box cause the id IDC_EDIT1 would have been valid without change.

for a first test both approaches are ok.

you now can try to add a member variable for the IDC_STAT1 or IDC_EDIT1.

right-click on the control and choose Add Variable .... name the member m_stat1 (or m_edit1) and choose category 'Value'. then in button handler comment the GetDlgItem(... and do instead

   m_stat1 = _T("1+1=2");
   UpdateData(FALSE);

the UpdateData stores values from class members to screen when argument is FALSE and gets data from screen with TRUE. so if you change to a edit text box you can get data from screen.

Sara

0
 
Deepu AbrahamR & D Engineering ManagerCommented:
You could do this way as well:
// Declare variable
CString strMessage;
int iValue = 1 + 1;
// Format
strMessage.Format( _T("The value is %d"),  iValue);
// Then display.
AfxMessageBox(strMessage);
0
 
shaolinfunkAuthor Commented:
Sara and Deepu.  THANK YOU VERY MUCH!

These basic examples you gave really help to illustrate the interaction between the variables and dialogs.  THANK YOU.

I will split points between you both equally.

Sara...you mentioned that _T is for wide characters.  Is there a setting I can change so that I no longer have to use "_T"?
0
 
shaolinfunkAuthor Commented:
THANK YOU FOR YOUR HELP!
0
 
sarabandeCommented:
you can go to the project properties and choose 'Multibyte Character Set" instead of "UNICODE".

you'll find it in the Configuration Properties in the General page at property Character Set.

Sara
0
 
shaolinfunkAuthor Commented:
Thanks, I found it and changed it.  Can you help me understand what is the difference between the two?
0
 
sarabandeCommented:
the multibyte charset means single char waht is ANSI code from 0  to 255 while ms unicode - normally called utf-16 - is page 0 of 32-bit unicode and has 0 to 65535 codes where the 0 .. 255 maps to ansi.

but for 65535 what is 0xffff in hex you need two byte character set and the character type used for taht is wchar_t what is a 16-bit integer. if you have ansi text like "HELLO" you have 6 char where a char is 8-bit integer and the last char is 0 (not visible) what means the terminating zero character. in utf-16 the same literal must be L"HELLO" what means it is an array of 6 wchar_t where the last wchar_t is also 0 but is 2-bytes as all the w_char t. if you look on L"HELLO" in bytes you have

    'H''\0' 'E''\0' 'L''\0' 'L''\0'  '\0''\0'

where '\0' is byte value 0.  each pair builds a wchar_t character or wide character.

of course the wide characters have much more symbols than ansi and specially in asia they used characters beyond ansi where the second byte is not '\0'.

in order to support those countries which have more characters microsoft decided to offer both ANSI and UNICODE in mfc and allow to switch between those. so they have for all functions which take a string as a character array or pointer to character two versions, one takes single char and the second takes wide char.

for example there is

     CWnd::SetWindowTextA(const char * text)

and

     CWnd::SetWindowTextW(const wchar_t * text)

as member functions of baseclass CWnd which you have used above. microsoft uses typedefs LPCSTR for const char * and LPCWSTR (long pointer to const wide string) for const wchar_t * but these are equivalent.

same is for AfxMessageBoxA and AfxMessageBoxW and about 100 other functions and also class CString has CStringT mapping which either uses char arrays internally or wchar_t array.

to switch between both microsoft uses the macro UNICODE what is set or not set according to project properties. then in window and mfc headers you have code like

#ifdef UNICODE
# define  AfxMessageBox AfxMessageBoxW
#else
# define  AfxMessageBox AfxMessageBoxA
#endif

what means depending on the settings you have the one or the other. for literals you have to use the _T or TEXT macro which is defined like

#ifdef UNICODE
# define _T(s) L ##s
#else
# define _T(s) s
#endif

what makes L"HELLO" from _T("HELLO").

if you want to be able to switch from UNICODE to ANSI and back you consequently have to use the TCHAR, LPTSTR, LPCTSTR, .. and more types which would be translated to char, char*, const char* or wchar_t, wchar_t*, const wchar_t* depending on UNICODE macro.

i don't like the concept and have not seen a real life project where it was actually practiced consequently. so if you don't want to sell your code to asia you might change the default setting and forget about that for almost all your code.

Sara



 

0
 
shaolinfunkAuthor Commented:
Wow Sarah!  Thank you SO MUCH for that detailed explanation.  I really appreciate your taking the time to explain the difference to a beginner like me.
0
 
shaolinfunkAuthor Commented:
You might be able to help me with my other question.  I clearly am starting out from the beginning and have lots of basic questions:

http://www.experts-exchange.com/Programming/Editors_IDEs/C_CPP_CS/Visual_CPP/Q_26815661.html
0
 
shaolinfunkAuthor Commented:
Yeah I have no intention of even selling my code period, let alone to asia...so I really think I don't need the wide characters.. I will stick with Multibyte Characters.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 14
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now