Sign in to follow this  

trying to make a simple GUI for a game editor

This topic is 3311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm working on a game editor for my XNA project. And i'm trying to throw together a simple GUI for controls like buttons, checkboxes, listboxes. the simple stuff. Anyways, awhile back (before i took a long break from programming) I had some success with a C++ gui for opengl. My problem now is that with C#, I can't do the control events the same way i did before. It's probably because i dont have a firm grasp on event handlers and delegates and whatnot. I used to have a pointer to a function in my control class that you could assign any function to.
gui.addbutton(x, y, myFunction);

void myfunction()
{
  // excecute this when button is clicked
}
now i'm sure there are fancy ways to do this but frankly i just would like get a system like this working first. I assume delegates are needed however i dont know how to apply them to this scenario. Unless you think some sort of custom event handler would be way better... but that also confuses me. thank you, Scott

Share this post


Link to post
Share on other sites
First you need to add a listener to the button click event.

button1.Click += new EventHandler(button1_Click);




+= to an event adds it to the listener list, -= removes it from the list.

button1_Click is the method that will be called when the click event happens.


void button1_Click(object sender, EventArgs e)
{
// Do your stuff here.
}




That is all you need to do. Add the listener, add the method. You are done.

theTroll

Share this post


Link to post
Share on other sites
Use a functor perhaps?

Just define a class that does the behavior you want to associate with the button and pass the class to the button. When your button does the associated action (i.e. "pressed"), just run the "Execute" method on the functor.

Using a functor is a form of delegation but not necessarily the best. This is just one of many ways to do it.

What about event-handling confuses you? Understanding how an event-handling system would work is surely to give you a lot of insight into the problem.

Share this post


Link to post
Share on other sites

This topic is 3311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this