示例#1
0
        void prepareEditor()
        {
            switch (objType)
            {
            case TypeCode.Boolean:
                editorKeyDown = (s, e) => {
                    if (e.KeyCode == Keys.Y)
                    {
                        editorBool = true;
                    }
                    else if (e.KeyCode == Keys.N)
                    {
                        editorBool = false;
                    }
                };
                break;

            case TypeCode.Char:
            case TypeCode.Byte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                objType.GetLimits(out var mi, out var ma);
                numMin = Convert.ToDecimal(mi);
                numMax = Convert.ToDecimal(ma);
                decimal num()
                {
                    decimal n = 0;
                    string  t = editor.Text;

                    if (!string.IsNullOrWhiteSpace(t))
                    {
                        n = (decimal)Convert.ChangeType(t, TypeCode.Decimal);
                    }
                    return(n);
                }

                editorNum     = num();
                editorKeyDown = (s, e) => {
                    decimal n2, n = 0;
                    string  result = null;
                    try {
                        n2        = n = num();
                        editorNum = n;
                        Keys k = e.KeyCode;
                        if (k == Keys.Up || k == Keys.Oemplus)
                        {
                            n += 1 * (e.Control ? 5 : 1);
                        }
                        else if (k == Keys.Down || k == Keys.OemMinus)
                        {
                            n -= 1 * (e.Control ? 5 : 1);
                        }
                        else if (k == Keys.PageUp)
                        {
                            n += 4 * (e.Control ? 5 : 1);
                        }
                        else if (k == Keys.PageDown)
                        {
                            n -= 4 * (e.Control ? 5 : 1);
                        }
                        else if (e.Control && k == Keys.Back)
                        {
                            keyDel = false;
                        }
                        else if (e.Control && k == Keys.Delete)
                        {
                            keyDel = true;
                        }
                        if (n < numMin)
                        {
                            n = numMin;
                        }
                        else if (n > numMax)
                        {
                            n = numMax;
                        }
                        if (e.IsInputKey = (n != n2))
                        {
                            result = n.ToString();    //Convert.ChangeType(n, objType).ToString();
                        }
                    } catch (OverflowException oe) {
                        var type = (Type)oe.TargetSite.GetType().GetProperty("ReturnType")
                                   .GetMethod.Invoke(oe.TargetSite, new object[0]);
                        var limit = type.GetField(n >= 0 ? "MaxValue" : "MinValue").GetValue(null);
                        result = limit.ToString();
                    } catch (Exception) {
                        result = Convert.ChangeType(editorNum, objType).ToString();
                    }
                    if (result != null)
                    {
                        editor.SuspendDrawing();
                        managedKey  = true;
                        editor.Text = result;
                        editor.ResumeDrawing();
                    }
                };
                editorLeave = (s, e) => {
                    object r;
                    bool   ok = Core.TryChangeType(editor.Text, objType, out r) ||
                                Core.TryChangeType(editorNum, objType, out r);
                    var si = editTarget.SubItems[editSubIndex];
                    editor.Text = (ok ? r : (si is LVSI_Ex x ? x.Value : si.Text)).ToString();
                };
                break;
            }

            switch (objType)
            {
            case TypeCode.Boolean:
                editorTransformer = (AutoTextBox from, StringBuilder text, ref int caret) => {
                    if (text.Length < 1)
                    {
                        return;
                    }
                    if (editorBool == true)
                    {
                        text.Clear();
                        text.Append(YES);
                    }
                    else if (editorBool == false)
                    {
                        text.Clear();
                        text.Append(NO);
                    }
                    caret = text.Length;
                };
                break;

            case TypeCode.Char:
                editorTransformer = (AutoTextBox from, StringBuilder text, ref int caret) => {
                    if (text.Length > 1)
                    {
                        text.Remove(0, text.Length - 1);
                    }
                    else
                    {
                        text.Append('\0');
                    }
                    caret = text.Length;
                };
                break;

            case TypeCode.Byte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                editorTransformer = (AutoTextBox from, StringBuilder text, ref int caret)
                                    => selectiveNumber(validUnsigned, from, text, ref caret);
                break;

            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
                editorTransformer = (AutoTextBox from, StringBuilder text, ref int caret)
                                    => selectiveNumber(validSigned, from, text, ref caret);
                break;

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                editorTransformer = (AutoTextBox from, StringBuilder text, ref int caret)
                                    => selectiveNumber(validFloating, from, text, ref caret);
                break;
            }
        }
示例#2
0
        public void StartEditing(ListViewItem item, int subIndex)
        {
            //Debug.WriteLine($@"start editing;");
            var e = new SubItemEditingEA(item, subIndex);

            SubItemEditing?.Invoke(this, e);
            var    sub = item.SubItems[subIndex];
            string value;
            bool   hide = false;

            if (sub is LVSI_Ex x)
            {
                var o = x.Value;
                objType = o?.GetType().GetInterfaces()
                          .Where(i => i.Name == "IConvertible").Count() >= 1
                        ? Convert.GetTypeCode(o)
                        : TypeCode.Object;
                if (o is bool b)
                {
                    value = b ? YES : NO;
                }
                else
                {
                    value = o?.ToString() ?? "";
                }
                hide = x.ShouldBeHidden;
            }
            else
            {
                value   = sub.Text;
                objType = TypeCode.String;
            }
            item.EnsureVisible();

            Rectangle subRect = GetSubItemBounds(item, subIndex);

            if (subRect.X < 0)
            {
                subRect.Width += subRect.X;
                subRect.X      = 0;
            }
            if (subRect.X + subRect.Width > Width)
            {
                subRect.Width = Width - subRect.Left;
            }
            subRect.Offset(Left, Top);

            oldForm = FindForm();
            oldForm?.Controls.Add(editor);
            Point origin = Point.Empty;
            Point pPos   = Parent.PointToScreen(origin);
            Point ePPos  = oldForm.PointToScreen(origin);
            var   align  = Columns[subIndex].TextAlign;

            if (align == HorizontalAlignment.Left)
            {
                subRect.X     += 3;
                subRect.Width -= 3;
            }
            else if (align == HorizontalAlignment.Right)
            {
                subRect.X     += 1;
                subRect.Width -= 4;
            }
            subRect.Y      += 2;
            subRect.Height -= 3;
            subRect.Offset(pPos.X - ePPos.X, pPos.Y - ePPos.Y);
            if (subIndex == 0 && item.ImageList != null)
            {
                int iconRes = 20;
                subRect.X     += iconRes - 1;
                subRect.Width -= iconRes;
            }
            SelectedItems.Clear();
            item.Selected = true;
            editTarget    = item;
            editSubIndex  = subIndex;

            editor.TextAlign    = align;
            editor.PasswordChar = hide ? '\u25CF' : '\0';
            editor.Bounds       = subRect;
            editor.Font         = item.Font;
            editorLeave         = null;
            editorKeyDown       = null;
            editorTransformer   = null;
            editor.Text         = value;
            prepareEditor();
            editor.Visible = true;
            editor.BringToFront();
            editor.Focus();
        }