EASY 500 POINTS!!! How do I assign a function to an MFC app dialog button?

Hi,

I am using code from a previous thread to read text from a text file.  I want to make it so that when I push a button on my dialog-based MFC app (MFC standard project style, MFC in a shared DLL, uses Unicode libraries) it will execute the read-text function.  How do I do this?

Thanks.
LVL 1
shaolinfunkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi shaolinfunk,

simply add the button in the dialog's resource, right-click the button and select 'Add event handler'. After confirming the up-coming dialog the dialog's source should be opened where a new function was added with a name like 'OnBnClicked...', in there you can call the function to read the file.

Hope that helps,

ZOPPO
0
GogowitschCommented:
Which version of Visual Studio are you using?

VS already created a window for you. When you look at it, you should see a list of available controls (Button, Label, Textbox etc.) somewhere, depending on your VS version. Just add a button and double-click it, the source code with an automatically added event handler pops up. There, you type the name of the function you want to call.
0
shaolinfunkAuthor Commented:
hi zoppo,
i created a new button called Button1, and followed your directions.  when i build the solution i an error:

 error LNK2001: unresolved external symbol "public: void __thiscall CFirstAppDlg::OnBnClickedOk(void)" (?OnBnClickedOk@CFirstAppyDlg@@QAEXXZ)

is there something else i need to do? thanks.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ZoppoCommented:
Hm - did you rename the function somewhere or delete the 'OnBnClickedOk' or something else?

BTW, it seems you're new to MFC programming, so I would suggest you to read some tutorials, thus it might be you save time and points - here are two links which may help you:

http://www.codeproject.com/KB/dialog/index.aspx?#Dialog%20&%20Windows%20-%20Beginners
http://www.functionx.com/visualc/

ZOPPO
0
shaolinfunkAuthor Commented:
yes i deleted the onbnclickedok button from the dialog.  does not all the code go away with the dialog?  if not, what else do i need to do to erase those error references to the deleted OK button (that comes with the dialog-based app upon creation)?

yes, i'm new to MFC, hence the beginner questions.  thanks for the tutorials but i paid for a premium account so i could engage with a master like yourself to help speed along the process.
0
ZoppoCommented:
If you want to remove the OK button you have to remove all occurances of 'onbnclickedok' - there should have been three, one in the H file within the class declaration and two within the CPP file, one the function itself and another one between these two lines:

BEGIN_MESSAGE_MAP(...)
...
END_MESSAGE_MAP()

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shaolinfunkAuthor Commented:
Thanks Zoppo, you've been very helpful with your fast answers and links to tutorials.  I will look into those now.  Quick side question.  While I don't get an error I get 2 warnings related to the read-text function we discussed earlier.

Specifically, "warning C4244: 'argument' : conversion from 'ULONGLONG' to 'size_t', possible loss of data" for the code line: "char *p = (char *)malloc(mytxt.GetLength()); "

and "warning C4244: 'argument' : conversion from 'ULONGLONG' to 'UINT', possible loss of data" for the code line: "mytxt.Read(p, mytxt.GetLength());"

could i be modifying marco's code such that i no longer get those warnings?
0
ZoppoCommented:
Well, the problem here is that 'CFile::GetLenght()' returns a unsigned 64-bit integer value of type 'ULONGLONG' since files can be larger than 4 GB.

Now, neither 'malloc' nor 'CFile::Read' (and even CString) can handle values larger then 32 bit.


Now, you can either assume that you'll never load files larger than 4GB - in this case you can simply ignore these warnings or cast the value to 32 bit, i.e.:

> size_t nLen = (size_t)mytxt.GetLength();
> char *p = (char *)malloc(nLen);
> mytxt.Read(p, nLen);

Or you think it may be you will need to read files larger than 4 GB - then you have a problem since as told none of these functions support such an amount data, so reading such a file has to be implemented in a completeley different way.


BTW, another hint: IMO you shouldn't use 'malloc' since it an 'old' C method - in C++ you should use 'new' and 'delete', so i.e. replace this:

> char *p = (char *)malloc(nLen);
> ...
> free( p );

with

> char *p = new char[nLen];
> ...
> delete [] p;

ZOPPO
0
shaolinfunkAuthor Commented:
Ok, thx Zoppo.  I have another 500 point question if you're interested.  Given your expertise this is probably another easy one for you:  http://www.experts-exchange.com/Programming/Editors_IDEs/C_CPP_CS/Visual_CPP/Q_25478835.html
0
shaolinfunkAuthor Commented:
what is size_t mentioned in your 10:08AM post?  is it an unsigned int (if yes, why not just use unsigned int)?  do i have to define size_t earlier in the code?
0
ZoppoCommented:
'size_t' just is a typedef of 'unsigned int' and should be declared by default. The same is with UINT, so you in general can use what you want there, 'unsigned int',m 'size_t' or 'UINT'.

In some cases it's more senseful to use such typedefs since it might be that in future new VisualStudio versions may typedef it in a different way, i.e. for use with 64 bit binaries (I actually don't know if this is the case, but it might be).
0
ZoppoCommented:
Sorry, do you mean you get an error that 'size_t' isn't defined?
0
shaolinfunkAuthor Commented:
thx for the explanation zoppo.  no, i didn't get any errors with size_t, i was just curious about it.  thanks.  i put up another 500 point question if you're interested.  you've been super helpful so far, i appreciate it.

http://www.experts-exchange.com/Programming/Editors_IDEs/C_CPP_CS/Visual_CPP/Q_25478835.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.