using System;
namespace FlashCards
{
[System.ComponentModel.ToolboxItem(true)]
public partial class SimpleMathWidget : Gtk.Bin
{
string answer = null;
public delegate void AnsweredHandler (object sender, System.EventArgs e);
public event AnsweredHandler Answered;
protected virtual void on_activated (object sender, System.EventArgs e)
{
//rethrow to widget event
Answered(this, e);
Console.WriteLine(this.Correct.ToString());
}
public SimpleMathWidget()
{
this.Build();
this.user_answer.Alignment = 1;
}
public string Operand1
{
get
{
return this.operand1.Text;
}
set
{
this.operand1.Text = value;
this.resize_operands();
}
}
public string Operand2
{
get
{
return this.operand2.Text;
}
set
{
this.operand2.Text = value;
this.resize_operands();
}
}
public string Operation
{
get
{
return this.operation.Text;
}
set
{
this.operation.Text = value;
}
}
public string Answer
{
get
{
return this.answer;
}
set
{
this.answer = value;
}
}
public bool Correct
{
get
{
return (this.user_answer.Text == this.answer);
}
}
private void resize_operands()
{
float a, b;
//Make sure both operands are numbers
string op1 = this.operand1.Text.Trim();
string op2 = this.operand2.Text.Trim();
if (float.TryParse(op1, out a) && float.TryParse(op2, out b))
{
//Check if either string has a decimal point
int idx_op1 = op1.IndexOf(".");
int idx_op2 = op2.IndexOf(".");
if (idx_op1 >= 0 || idx_op2 >= 0)
{
//Determine the length of anything to the right of the decimal
int lng_op1 = op1.Length - (idx_op1 >= 0 ? idx_op1 : op1.Length);
int lng_op2 = op2.Length - (idx_op2 >= 0 ? idx_op2 : op2.Length);
if (lng_op1 > lng_op2)
{
this.operand2.Text = (op2.IndexOf(".") >= 0 ? append_spaces(op2, lng_op1 - lng_op2) : append_spaces(op2 + ".", lng_op1 - lng_op2 - 1));
}
else
{
this.operand1.Text = (op1.IndexOf(".") >= 0 ? append_spaces(op1, lng_op2 - lng_op1) : append_spaces(op1 + ".", lng_op2 - lng_op1 - 1));
}
}
}
}
private string append_spaces(string str, int spaces)
{
for (int i = 0; i < spaces; i++)
{
str+= "0";
}
return str;
}
}
}