trying to make a simple GUI for a game editor

Started by
2 comments, last by Nibbles 15 years, 5 months ago
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
Advertisement
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
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.
"Artificial Intelligence: the art of making computers that behave like the ones in movies."www.CodeFortress.com
bah, i shouldn't have looked here just yet... I *almost* had it figured. u guys just tipped the scale for me though. thank you.

This topic is closed to new replies.

Advertisement