Share Pastebin
Guest
Public paste!

Rethrowing Events

By: a guest | Mar 21st, 2010 | Syntax: C# | Size: 2.50 KB | Hits: 111 | Expires: Never
Copy text to clipboard
  1. using System;
  2.  
  3. namespace FlashCards
  4. {
  5.        
  6.         [System.ComponentModel.ToolboxItem(true)]
  7.         public partial class SimpleMathWidget : Gtk.Bin
  8.         {
  9.                 string answer = null;
  10.                
  11.                 public delegate void AnsweredHandler (object sender, System.EventArgs e);
  12.                 public event AnsweredHandler Answered;
  13.                
  14.                 protected virtual void on_activated (object sender, System.EventArgs e)
  15.                 {
  16.                         //rethrow to widget event
  17.                         Answered(this, e);
  18.                         Console.WriteLine(this.Correct.ToString());
  19.                 }
  20.                
  21.                 public SimpleMathWidget()
  22.                 {
  23.                        
  24.                         this.Build();
  25.                         this.user_answer.Alignment = 1;
  26.                 }
  27.                
  28.                 public string Operand1
  29.                 {
  30.                         get
  31.                         {
  32.                                 return this.operand1.Text;
  33.                         }
  34.                        
  35.                         set
  36.                         {
  37.                                 this.operand1.Text = value;
  38.                                 this.resize_operands();
  39.                         }
  40.                                
  41.                 }
  42.                
  43.                 public string Operand2
  44.                 {
  45.                         get
  46.                         {
  47.                                 return this.operand2.Text;
  48.                         }
  49.                        
  50.                         set
  51.                         {
  52.                                 this.operand2.Text = value;
  53.                                 this.resize_operands();
  54.                         }
  55.                 }
  56.                
  57.                 public string Operation
  58.                 {
  59.                         get
  60.                         {
  61.                                 return this.operation.Text;
  62.                         }
  63.                        
  64.                         set
  65.                         {
  66.                                 this.operation.Text = value;
  67.                         }
  68.                                
  69.                 }
  70.                
  71.                 public string Answer
  72.                 {
  73.                         get
  74.                         {
  75.                                 return this.answer;
  76.                         }
  77.                        
  78.                         set
  79.                         {
  80.                                 this.answer = value;
  81.                         }
  82.                                
  83.                 }
  84.                
  85.                 public bool Correct
  86.                 {
  87.                         get
  88.                         {
  89.                                 return (this.user_answer.Text == this.answer);
  90.                         }
  91.                 }
  92.                
  93.                 private void resize_operands()
  94.                 {
  95.                         float a, b;
  96.                        
  97.                         //Make sure both operands are numbers
  98.                         string op1 = this.operand1.Text.Trim();
  99.                         string op2 = this.operand2.Text.Trim();
  100.                         if (float.TryParse(op1, out a) && float.TryParse(op2, out b))
  101.                         {
  102.                                 //Check if either string has a decimal point
  103.                                 int idx_op1 = op1.IndexOf(".");
  104.                                 int idx_op2 = op2.IndexOf(".");
  105.                                 if (idx_op1 >= 0 || idx_op2 >= 0)
  106.                                 {
  107.                                        
  108.                                         //Determine the length of anything to the right of the decimal
  109.                                         int lng_op1 = op1.Length - (idx_op1 >= 0 ? idx_op1 : op1.Length);
  110.                                         int lng_op2 = op2.Length - (idx_op2 >= 0 ? idx_op2 : op2.Length);
  111.                                        
  112.                                         if (lng_op1 > lng_op2)
  113.                                         {
  114.                                                 this.operand2.Text = (op2.IndexOf(".") >= 0 ? append_spaces(op2, lng_op1 - lng_op2) : append_spaces(op2 + ".", lng_op1 - lng_op2 - 1));
  115.                                         }
  116.                                         else
  117.                                         {
  118.                                                 this.operand1.Text = (op1.IndexOf(".") >= 0 ? append_spaces(op1, lng_op2 - lng_op1) : append_spaces(op1 + ".", lng_op2 - lng_op1 - 1));
  119.                                         }
  120.                                 }
  121.                         }
  122.                 }
  123.                
  124.                 private string append_spaces(string str, int spaces)
  125.                 {
  126.                         for (int i = 0; i < spaces; i++)
  127.                         {
  128.                                 str+= "0";
  129.                         }
  130.                        
  131.                         return str;
  132.                 }
  133.         }
  134. }