331 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			331 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.ComponentModel;
 | 
						|
using System.Drawing;
 | 
						|
using System.Windows.Forms;
 | 
						|
using System.Windows.Forms.Design;
 | 
						|
 | 
						|
namespace DevComponents.Charts.Design
 | 
						|
{
 | 
						|
    [ToolboxItem(false)]
 | 
						|
    public partial class SeriesPointValueDropDown : UserControl
 | 
						|
    {
 | 
						|
        #region Private variables
 | 
						|
 | 
						|
        private object _Value;
 | 
						|
        private bool _EscapePressed;
 | 
						|
 | 
						|
        private IWindowsFormsEditorService _EditorService;
 | 
						|
        private ITypeDescriptorContext _Context;
 | 
						|
 | 
						|
        private LabeledTextBoxX[] _TextBoxes;
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        public SeriesPointValueDropDown()
 | 
						|
        {
 | 
						|
            InitializeComponent();
 | 
						|
        }
 | 
						|
 | 
						|
        public SeriesPointValueDropDown(object value,
 | 
						|
            IWindowsFormsEditorService editorService, ITypeDescriptorContext context)
 | 
						|
        {
 | 
						|
            if (value == null)
 | 
						|
                value = new object[0];
 | 
						|
 | 
						|
            _Value = value;
 | 
						|
 | 
						|
            _EditorService = editorService;
 | 
						|
            _Context = context;
 | 
						|
 | 
						|
            BackColor = SystemColors.Control;
 | 
						|
 | 
						|
            InitializeComponent();
 | 
						|
 | 
						|
            LayoutMultipleEntry();
 | 
						|
 | 
						|
            Disposed += SeriesPointValueDropDown_Disposed;
 | 
						|
        }
 | 
						|
 | 
						|
        #region LayoutMultipleEntry
 | 
						|
 | 
						|
        private void LayoutMultipleEntry()
 | 
						|
        {
 | 
						|
            if (_Value is object[])
 | 
						|
            {
 | 
						|
                object[] values = (object[])_Value;
 | 
						|
 | 
						|
                _TextBoxes = GetTextBoxArray(values);
 | 
						|
 | 
						|
                PositionBtns();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #region GetTextBoxArray
 | 
						|
 | 
						|
        private LabeledTextBoxX[] GetTextBoxArray(object[] values)
 | 
						|
        {
 | 
						|
            LabeledTextBoxX[] tba = new LabeledTextBoxX[Math.Max(values.Length + 1, 3)];
 | 
						|
 | 
						|
            for (int i = 0; i < tba.Length; i++)
 | 
						|
            {
 | 
						|
                object value = (i < values.Length) ? values[i] : null;
 | 
						|
 | 
						|
                tba[i] = GetValueTextBox(value, i);
 | 
						|
            }
 | 
						|
 | 
						|
            return (tba);
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region GetValueTextBox
 | 
						|
 | 
						|
        private LabeledTextBoxX GetValueTextBox(object value, int i)
 | 
						|
        {
 | 
						|
            LabeledTextBoxX ltb = new LabeledTextBoxX();
 | 
						|
 | 
						|
            ltb.EscapeKeyPressed += ltb_EscapeKeyPressed;
 | 
						|
 | 
						|
            ltb.lbIndex.Text = i.ToString();
 | 
						|
            ltb.tbValue.Tag = (value is string) ? "\"" + value + "\"" : value;
 | 
						|
            ltb.tbValue.Text = FormatToStringValue(value);
 | 
						|
 | 
						|
            ltb.Location = new Point(0, i * (ltb.Height + 2) + 2);
 | 
						|
            ltb.Width = Width + 6;
 | 
						|
 | 
						|
            ltb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
 | 
						|
 | 
						|
            Controls.Add(ltb);
 | 
						|
 | 
						|
            return (ltb);
 | 
						|
        }
 | 
						|
 | 
						|
        #region FormatToStringValue
 | 
						|
 | 
						|
        private string FormatToStringValue(object value)
 | 
						|
        {
 | 
						|
            if (value == null)
 | 
						|
                return ("");
 | 
						|
 | 
						|
            if (value is string)
 | 
						|
                return ("\"" + value + "\"");
 | 
						|
 | 
						|
            if (value is int)
 | 
						|
                return (value.ToString());
 | 
						|
 | 
						|
            if (value is DateTime)
 | 
						|
                return (value.ToString());
 | 
						|
 | 
						|
            return (String.Format("{0:0.0###############}", value));
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region ltb_EscapeKeyPressed
 | 
						|
 | 
						|
        void ltb_EscapeKeyPressed(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            btnCancel.PerformClick();
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region PositionBtns
 | 
						|
 | 
						|
        private void PositionBtns()
 | 
						|
        {
 | 
						|
            int y = 2;
 | 
						|
 | 
						|
            foreach (LabeledTextBoxX ltb in _TextBoxes)
 | 
						|
                y += (ltb.Height + 2);
 | 
						|
 | 
						|
            Height = y + btnOk.Height;
 | 
						|
 | 
						|
            int x = Width - btnCancel.Width;
 | 
						|
 | 
						|
            btnCancel.Location = new Point(x - 3 , y);
 | 
						|
            btnOk.Location = new Point(x - 3 - btnOk.Width, y);
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region Public properties
 | 
						|
 | 
						|
        #region Value
 | 
						|
 | 
						|
        public object Value
 | 
						|
        {
 | 
						|
            get
 | 
						|
            {
 | 
						|
                if (EscapePressed == true)
 | 
						|
                    return (_Value);
 | 
						|
 | 
						|
                return (GetTextBoxValues());
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #region GetTextBoxValues
 | 
						|
 | 
						|
        private object[] GetTextBoxValues()
 | 
						|
        {
 | 
						|
            object[] values = new object[_TextBoxes.Length];
 | 
						|
 | 
						|
            int n = 0;
 | 
						|
 | 
						|
            for (int i = 0; i < _TextBoxes.Length; i++)
 | 
						|
            {
 | 
						|
                values[i] = GetTextBoxValue(i);
 | 
						|
 | 
						|
                if (values[i] != null)
 | 
						|
                    n++;
 | 
						|
            }
 | 
						|
 | 
						|
            if (n > 0)
 | 
						|
            {
 | 
						|
                object[] rvalues = new object[n];
 | 
						|
 | 
						|
                n = 0;
 | 
						|
 | 
						|
                for (int i = 0; i < values.Length; i++)
 | 
						|
                {
 | 
						|
                    if (values[i] != null)
 | 
						|
                        rvalues[n++] = values[i];
 | 
						|
                }
 | 
						|
 | 
						|
                return (rvalues);
 | 
						|
            }
 | 
						|
 | 
						|
            return (null);
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region GetTextBoxValue
 | 
						|
 | 
						|
        private object GetTextBoxValue(int n)
 | 
						|
        {
 | 
						|
            LabeledTextBoxX ltb = _TextBoxes[n];
 | 
						|
 | 
						|
            if (string.IsNullOrEmpty(ltb.tbValue.Text) == true)
 | 
						|
                return (null);
 | 
						|
 | 
						|
            if (ltb.tbValue.Tag != null)
 | 
						|
            {
 | 
						|
                if (ltb.tbValue.Text.Equals(ltb.tbValue.Tag.ToString()) == true)
 | 
						|
                    return (ltb.tbValue.Tag);
 | 
						|
            }
 | 
						|
 | 
						|
            return (GetConvertedValue(ltb.tbValue.Text));
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region GetConvertedValue
 | 
						|
 | 
						|
        private object GetConvertedValue(string text)
 | 
						|
        {
 | 
						|
            string s = text.Trim();
 | 
						|
            
 | 
						|
            if (s.StartsWith("\"") && s.EndsWith("\""))
 | 
						|
                return (s.Substring(1, s.Length - 2));
 | 
						|
 | 
						|
            if (s.Contains(".") == false)
 | 
						|
            {
 | 
						|
                int iResult;
 | 
						|
 | 
						|
                if (int.TryParse(text, out iResult) == true)
 | 
						|
                    return (iResult);
 | 
						|
            }
 | 
						|
 | 
						|
            double dblResult;
 | 
						|
 | 
						|
            if (double.TryParse(text, out dblResult) == true)
 | 
						|
                return (dblResult);
 | 
						|
 | 
						|
            DateTime dtResult;
 | 
						|
 | 
						|
            if (DateTime.TryParse(text, out dtResult) == true)
 | 
						|
                return (dtResult);
 | 
						|
 | 
						|
            return (text);
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region EditorService
 | 
						|
 | 
						|
        public IWindowsFormsEditorService EditorService
 | 
						|
        {
 | 
						|
            get { return (_EditorService); }
 | 
						|
            set { _EditorService = value; }
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region EscapePressed
 | 
						|
 | 
						|
        public bool EscapePressed
 | 
						|
        {
 | 
						|
            get { return (_EscapePressed); }
 | 
						|
            set { _EscapePressed = value; }
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region MyPreviewKeyDown
 | 
						|
 | 
						|
        private void MyPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
 | 
						|
        {
 | 
						|
            if (e.KeyCode == Keys.Escape)
 | 
						|
                _EscapePressed = true;
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region btnOk_Click
 | 
						|
 | 
						|
        private void btnOk_Click(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            _EditorService.CloseDropDown();
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region btnCancel_Click
 | 
						|
 | 
						|
        private void btnCancel_Click(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            _EscapePressed = true;
 | 
						|
 | 
						|
            _EditorService.CloseDropDown();
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region SeriesPointValueDropDown_Disposed
 | 
						|
 | 
						|
        void SeriesPointValueDropDown_Disposed(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            for (int i = 0; i < _TextBoxes.Length; i++)
 | 
						|
            {
 | 
						|
                LabeledTextBoxX ltb = _TextBoxes[i];
 | 
						|
 | 
						|
                ltb.EscapeKeyPressed -= ltb_EscapeKeyPressed;
 | 
						|
                ltb.Dispose();
 | 
						|
 | 
						|
                _TextBoxes[i] = null;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
    }
 | 
						|
}
 |