using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;


public class classMessageBox : Form
{
    string reply;
    int intButtonLeft;
    public System.Windows.Forms.Button[] btns = new System.Windows.Forms.Button[0];
    public System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();


    public classMessageBox(string strTitle, string strText, params string[] strButtons)
    {
        Text = strTitle;
        Controls.Add(lbl);
        lbl.Text = strText;
        lbl.Top = 5; lbl.Left = 5;
        lbl.AutoSize = true;


        foreach (string strButton in strButtons)
            addButton(strButton);

        resizeForm();
    }

    void addButton(string strThisButton)
    {
        System.Windows.Forms.Button btnNew = new System.Windows.Forms.Button();
        Controls.Add(btnNew);
        btnNew.Visible = true;
        btnNew.AutoSize = true; btnNew.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        btnNew.Text = strThisButton.Trim();

        btnNew.Click += new EventHandler(btn_Click);
        btnNew.FontChanged += new EventHandler(btnNew_FontChanged);

        Array.Resize<System.Windows.Forms.Button>(ref btns, btns.Length + 1);
        btns[btns.Length - 1] = btnNew;
    }

    void btn_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.Button btnThis = (System.Windows.Forms.Button)sender;
        reply = btnThis.Text;
        Close();
    }

    void btnNew_FontChanged(object sender, EventArgs e)
    { initResize(); }
    void lbl_FontChanged(object sender, EventArgs e) { initResize(); }

    void initResize()
    {
        System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
        tmr.Interval = 5;
        tmr.Tick += new EventHandler(tmr_Tick);
        tmr.Enabled = true;
    }

    void tmr_Tick(object sender, EventArgs e)
    {
        System.Windows.Forms.Timer tmrMe = (System.Windows.Forms.Timer)sender;
        tmrMe.Enabled = false;
        resizeForm();
    }

    void resizeForm()
    {
        intButtonLeft = 5;
        foreach (System.Windows.Forms.Button btn in btns)
        {
            btn.Left = intButtonLeft;
            intButtonLeft += btn.Width + 5;
        }

        Width = btns[btns.Length - 1].Left + btns[btns.Length - 1].Width + 25;
        lbl.AutoSize = true;
        // resize label
        int intNumLines = (int)Math.Ceiling((double)lbl.Width / (double)Width) + 1;
        lbl.AutoSize = false;
        lbl.FontChanged += new EventHandler(lbl_FontChanged);
        lbl.Height = lbl.Font.Height * intNumLines;
        lbl.Width = Width - 20;

        int intTallestButtonHeight = 0;
        foreach (System.Windows.Forms.Button thisButton in btns)
        {
            thisButton.Top = lbl.Top + lbl.Height + 5;
            if (intTallestButtonHeight < thisButton.Height)
                intTallestButtonHeight = thisButton.Height;
        }

        Height = btns[0].Top + intTallestButtonHeight + 35;

        Top = (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2;
        Left = (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2;
    }

    public string Reply { get { return reply; } }
}
