All pastes #410350 Raw Edit

Macca

public text v1 · immutable
#410350 ·published 2007-03-26 10:08 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;

namespace Bersirc.UI.Controls
{
    public class HistoryTextBox : TextBox
    {
        private List<string> m_History;
        private int m_HistoryIndex;

        public string[] History
        {
            get { return m_History.ToArray(); }
        }

        public HistoryTextBox()
        {
            m_History = new List<string>();
            m_HistoryIndex = 0;
            this.KeyPress += new KeyPressEventHandler(KeyPressHandler);
            this.KeyDown += new KeyEventHandler(KeyDownHandler);
        }

        private void KeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                if (m_HistoryIndex > 0)
                {
                    m_HistoryIndex--;
                    Text = m_History[m_HistoryIndex];
                }

                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (m_HistoryIndex < m_History.Count)
                {
                    m_HistoryIndex++;
                    Text = m_History[m_HistoryIndex - 1];
                }
                else
                {
                    Text = "";
                }

                e.Handled = true;
            }
        }

        private void KeyPressHandler(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                if (Text != "")
                {
                    m_History.Add(Text);
                    m_HistoryIndex = m_History.Count;
                    e.Handled = true;
                }
            }
        }
    }
}