Solved

How do I make my dialog button DO SOMETHING?

Posted on 2011-02-10
22
494 Views
Last Modified: 2013-12-14
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
Comment
Question by:shaolinfunk
  • 14
  • 6
  • 2
22 Comments
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34867657
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
 
LVL 11

Accepted Solution

by:
DeepuAbrahamK earned 250 total points
ID: 34868547
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
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 34868920
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34869693
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34869700
the above comment was for Deepu.  I will try sarabande's suggestion now and post back.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34869729
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34870124
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34870145
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
 
LVL 32

Expert Comment

by:sarabande
ID: 34870195
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34870249
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34870259
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Author Comment

by:shaolinfunk
ID: 34870272
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
 
LVL 32

Expert Comment

by:sarabande
ID: 34870426
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
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 34870612
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34871438
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
 
LVL 1

Author Closing Comment

by:shaolinfunk
ID: 34871442
THANK YOU FOR YOUR HELP!
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34874640
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34874920
Thanks, I found it and changed it.  Can you help me understand what is the difference between the two?
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34875635
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34875696
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34875703
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34875714
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now