Thursday, July 9, 2009

GUI stuff in C++?

if i have 5 buttons that i created dynamically, and 5 strings. my question is, how can i create the eventhandler for each button to show each string? to make it clear, if i click button 1, it will display string 1. if i click button 2, it will display string 2, and so on. any ideas? i have been stuck in this situation for the past 3 days. thanks





ps: i need to do this in c++ and i'm using .net.

GUI stuff in C++?
What you will want to read up on is handlers. In VC++ .net you can specify an event handler and then associate it with an event. The link below shows you how this can be done with some example code. Scroll down to where it reads "Event Implementation".





This code can be found in the code generated by the IDE. Open it up and locate where it defines the buttons and you will see something like...





this-%26gt; button1-%26gt; Click += gcnew System:: EventHandler(this, %26amp;Form1:: button1_Click);





All 5 of your buttons will share the same handler function. In that function you will see there is an object passed into it (that is what 'this' represents followed by the function to call... in the example above it is the function button1_Click. Using the object passed, you can determine which button was pressed and then show the appropriate string.





I have thrown in a link to showing how to add events to controls. Hope that helps.





Good luck with the programming!
Reply:Just for future reference to anyone, what I wrote was generic, but the idea will indeed work. The idea is five buttons sharing the same handler, but based on the instance of the button will pull the appropriate string. No data is rewritten if you design the appropriate scoping. Report It



No comments:

Post a Comment