1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
0/5,

Delegates

Summary of the tutorial

A delegate is a way to encapsulate a reference to a method inside a object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. This means you can kind of treat a method like a variable by using delegates.

We'll explore this by creating a simple gump that has 2 options an Okay, and a Cancel button, a string for a title and a string for a message. We'll use a delegate void for processing the option (in the form of a bool) that the user selects.

Gump Design

We'll start by creating the basics of the gump.
The stuff that's always used will be taken care of first, for this we want a graphical user interface to ask the player a question and get their concent for something.
  1. title
    1. background
    2. label
  2. message
    1. background
    2. html
  3. user response
    1. okay button
    2. cancel button
We will also set up the response, but have it do nothing at this point in time, to collect the okay or cancel response from the user.
Code (C#):
  1.  
  2. using Server.Network;
  3.  
  4. namespace Server.Gumps
  5. {
  6.     public class Confirmation : Gump
  7.     {
  8.         public Confirmation(string title, string htmlmessage) : base(200, 200)
  9.         {
  10.             Closable = false;
  11.             Dragable = false;
  12.  
  13.             AddPage(0);
  14.             AddBackground(45, 0, 250, 35, 9200); // title background
  15.             AddLabel(55, 5, 0, title); //title
  16.             AddBackground(0, 30, 350, 150, 2600); //message background
  17.             AddHtml(45, 45, 260, 80, htmlmessage, false, true); //message
  18.             AddButton(55, 135, 247, 248, 1, GumpButtonType.Reply, 0); //Okay Button
  19.             AddButton(225, 135, 241, 242, 0, GumpButtonType.Reply, 0); //Cancel button
  20.         }
  21.  
  22.         public override void OnResponse(NetState sender, RelayInfo info)
  23.         {
  24.             Mobile from = sender.Mobile;
  25.  
  26.             switch (info.ButtonID)
  27.             {
  28.                 case 0: //Cancel clicked
  29.                 {
  30.                     break;
  31.                 }
  32.                 case 1: //Okay clicked
  33.                 {
  34.                     break;
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
  40.