Exemplo n.º 1
0
        public static T Add <T>(this Control parent, WinFormItem winFormItem) where T : Control
        {
            Type t = typeof(T);

            winFormItem.NullTest(nameof(winFormItem));
            winFormItem.PresentationType = t;
            return((T)AddInternal(parent, winFormItem));
        }
Exemplo n.º 2
0
        public static T Add <T>(this Control parent, string name) where T : Control
        {
            name.NullTest(nameof(name));
            WinFormItem wfi = new WinFormItem(name)
            {
                PresentationType = typeof(T)
            };

            return((T)AddInternal(parent, wfi));
        }
Exemplo n.º 3
0
        public static TKey Choose <TKey, TText>(IEnumerable <KeyValuePair <TKey, TText> > items, int selected)
        {
            WinFormItem selector = new WinFormItem("RadioGropPanel")
            {
                PresentationType = typeof(TableLayoutPanel),
                ControlTrigger   = (c, e) =>
                {
                    e.Dock = DockStyle.Fill;
                }
            };
            int i = 0;

            foreach (var item in items)
            {
                selector.Add(new WinFormItem($"P{i}")
                {
                    PresentationType = typeof(RadioButton),
                    ControlTrigger   = (cdi, ctrl) =>
                    {
                        RadioButton rb             = (ctrl as RadioButton);
                        rb.UseVisualStyleBackColor = true;
                        rb.Text        = item.Value.ToString();
                        rb.Tag         = item.Key;
                        rb.Dock        = DockStyle.Top;
                        rb.MouseClick += (c, e) => cdi
                                         .LinkedControl
                                         .FindForm()
                                         .DialogResult = System.Windows.Forms.DialogResult.OK;
                        rb.MouseMove += (c, e) => rb.Cursor = Cursors.Hand;
                        rb.TabIndex   = i;
                    }
                });
                i++;
            }

            using (WinForm cd = new BaseViewForm(selector))
            {
                cd.GetControl <RadioButton>($"P{selected}").Checked = true;
                Button btn = cd.GetControl <Button>("OK");
                cd.AcceptButton = btn;

                cd.StartPosition = FormStartPosition.CenterParent;
                if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    cd.Save();
                    return((TKey)cd.Items
                           .Select(kvp => kvp.Value.LinkedControl as RadioButton)
                           .FirstOrDefault(rb => rb != null && rb.Checked).Tag);
                }
                else
                {
                    return(default);
Exemplo n.º 4
0
        public static T Add <T>(this Control parent) where T : Control
        {
            WinForm wf = parent.GetOwner();

            if (wf == null)
            {
                throw new InvalidOperationException($"Control extensoin method Add<T> may be called only for WinForm child control");
            }
            WinFormItem wfi = new WinFormItem(wf.NewName(typeof(T)))
            {
                PresentationType = typeof(T)
            };

            return((T)AddInternal(parent, wfi));
        }
Exemplo n.º 5
0
        public BaseViewForm(WinFormItem browser)
        {
            Width  = 680;
            Height = (int)(Width / vbo.GoldenRatio);
            this.Add <Panel>(WinFormConfig.SinglePageForm);
            TableLayoutPanel tp = Items["FormRowsPanel"].LinkedControl as TableLayoutPanel;

            tp.Add(browser);
            browser.LinkedControl.Dock = DockStyle.Fill;
            var btp = GetItem("MainPanel")
                      .LinkedControl
                      .Add <TableLayoutPanel>(WinFormConfig.StdButtons());

            btp.Height = ButtonHeight + 20;
        }
Exemplo n.º 6
0
        public DBGridParam(JMXSchema schema, DBGrid grid)
        {
            _grid = grid;
            int count = schema.Parameters.Count;
            List <WinFormItem> listItems = new List <WinFormItem>(count);

            foreach (var p in schema.Parameters)
            {
                WinFormItem item = new WinFormItem(p.ParamName)
                {
                    Caption          = p.Name,
                    DataType         = MdbTypeMap.GetType(p.DataType),
                    Width            = p.DisplayWidth,
                    DataSize         = p.Width,
                    Format           = p.Format,
                    SuperForm        = p.SuperForm,
                    SuperMethod      = p.SuperMethod,
                    ConstName        = p.ConstName,
                    PresentationType = Type.GetType(p.PresentationType),
                    Value            = p.DefaultValue
                };
                if (p.ListData.Count > 0)
                {
                    for (int i = 0; i < p.ListData.Count; i++)
                    {
                        item.Add(new WinFormItem(p.ListItems[i])
                        {
                            Caption  = p.ListItems[i],
                            DataType = item.DataType,
                            Value    = p.ListData[i]
                        });
                    }
                    item.Mask = "lock";
                }
                //!!! Добавить добавление констант в расширениях (class UserConstants.GetConst(), AddConst()
                switch (item.ConstName.ToLower())
                {
                case "bs_datecurrent":
                    item.Value = vbo.Date();
                    break;

                case "bs_datestart":
                    item.Value = rth.DateStart;
                    break;

                case "bs_datefinish":
                    item.Value = rth.DateFinish;
                    break;

                case "bs_username":
                    item.Value = PathHelper.UserName;
                    break;

                default:
                    if (item.ConstName.Left(1) == "=")
                    {
                        //!!! item.Value = Evaluator.Eval(item.ConstName.Substring(1), schema);
                    }
                    else
                    {
                        //!!!
                        //string localSetting = schema[path + "ConstName"];
                        //if (localSetting.IndexOf(vbo.chrSep) > -1)
                        //	localSetting = dbs.GetSetting(localSetting);
                        //else
                        //	localSetting = dbs.GetSetting(setPath + localSetting);
                        //if (string.IsNullOrEmpty(localSetting))
                        //	localSetting = dbs.GetSetting(schema["ObjectName"] + vbo.chrSep + "Setup" + vbo.chrSep +
                        //		schema[path + "ConstName"]);
                        //if (!string.IsNullOrEmpty(localSetting))
                        //	item.Value = localSetting;
                    }
                    break;
                }
                if (item.OriginalValue == null && _grid.ParentRow != null && !string.IsNullOrEmpty(p.FieldName))
                {
                    try
                    {
                        item.Value = _grid.ParentRow[p.FieldName];
                    }
                    finally
                    {
                        if (vbo.IsEmpty(item.Value))
                        {
                            item.Value = null;
                        }
                    }
                }
                if ((item.Visible = (vbo.IsEmpty(item.Value))) && _paramCash.ContainsKey(item.Name))
                {
                    item.Value = _paramCash[item.Name];
                }
                listItems.Add(item);
            }
            _items = listItems.ToArray();
        }
Exemplo n.º 7
0
        public static object Add(this Control parent, WinFormItem winFormItem)
        {
            if (!winFormItem.Caption.IsEmpty())
            {
                Label l;
                if (winFormItem.CellAddress == WinFormItem.CellAddressDefault)
                {
                    l = parent.Add <Label>(new WinFormItem("Label_" + winFormItem.Name)
                    {
                        CellAddress = new Pair <int>(winFormItem.CellAddress.X, winFormItem.CellAddress.Y)
                    });
                    winFormItem.CellAddress = new Pair <int>(winFormItem.CellAddress.X + 1, winFormItem.CellAddress.Y);
                }
                else
                {
                    l = parent.Add <Label>(new WinFormItem("Label_" + winFormItem.Name));
                }
                l.Text = winFormItem.Caption;
            }
            if (winFormItem.PresentationType == null)
            {
                if (winFormItem.DataType == typeof(bool))
                {
                    winFormItem.PresentationType = typeof(CheckBox);
                    winFormItem.Clear();
                    winFormItem.Add(new WinFormItem("Да")
                    {
                        Value = true
                    });
                    winFormItem.Add(new WinFormItem("Нет")
                    {
                        Value = false
                    });
                    winFormItem.ControlTrigger = (i, c) =>
                    {
                        CheckBox chkBox = c as CheckBox;
                        chkBox.Text            = (bool)i.Value ? "Да" : "Нет";
                        chkBox.Checked         = (bool)i.Value;
                        chkBox.CheckedChanged += (ctrl, e) =>
                        {
                            CheckBox cb = (CheckBox)ctrl;
                            chkBox.Text = cb.Checked ? "Да" : "Нет";
                        };
                    };
                }
                else if (winFormItem.DataType == typeof(DateTime))
                {
                    winFormItem.PresentationType = typeof(DateEdit);
                }
                else if (!winFormItem.SuperForm.IsEmpty())
                {
                    winFormItem.PresentationType = typeof(TextBuX);
                }
            }
            if ((winFormItem.PresentationType == null || winFormItem.PresentationType.IsSubclassOf(typeof(ListBox))) &&
                winFormItem.Count() > 0 && winFormItem.SuperForm.IsEmpty() && IsSimpleType(winFormItem.DataType))
            {
                winFormItem.PresentationType = typeof(ComboBox);
                winFormItem.ControlTrigger   = (item, c) =>
                {
                    ComboBox cmb = c as ComboBox;
                    if (item.Mask.ToLower() == "lock")
                    {
                        cmb.DropDownStyle = ComboBoxStyle.DropDownList;
                    }
                    if (!string.IsNullOrEmpty(item.Format))
                    {
                        cmb.FormatString = item.Format;
                    }
                    cmb.Items.AddRange(item.ToArray());
                    cmb.DataSource = item.Select(wfi => wfi.Name).ToList();
                    cmb.DataBindings.Add("Text", item, "Value");

                    Type t = winFormItem.DataType;
                    if (t.IsNumeric())
                    {
                        var itemWithData = winFormItem.FirstOrDefault(d => d.Value.Equals(winFormItem.Value));
                        if (itemWithData != null)
                        {
                            cmb.Text = itemWithData.Caption;
                        }
                    }
                };
            }
            else if ((winFormItem.PresentationType == typeof(RadioGroup) && winFormItem.Count() > 0 &&
                      winFormItem.SuperForm.IsEmpty() && IsSimpleType(winFormItem.DataType)))
            {
                winFormItem.ControlTrigger = (i, c) =>
                {
                    RadioGroup rg = c as RadioGroup;
                    rg.Items.AddRange(i.Select(item => item.Name).ToArray());
                    if (rg.Items.Count > 2)
                    {
                        rg.Height = Math.Max(rg.Height, rg.Items.Count * rg.Font.Height * 2);
                    }
                    else
                    {
                        rg.Height = rg.Font.Height * 5;
                    }

                    if (winFormItem.Value != null)
                    {
                        rg.Text = winFormItem.Value.ToString();
                    }
                };
            }
            else if (winFormItem.PresentationType == null)
            {
                winFormItem.PresentationType = typeof(TextBox);
            }
            return(AddInternal(parent, winFormItem));
        }
Exemplo n.º 8
0
        static object AddInternal(Control parent, WinFormItem winFormItem)
        {
            parent.SuspendLayout();
            WinForm wf = parent.GetOwner();

            wf.OnItemAdd(winFormItem);
            if (wf == null)
            {
                throw new InvalidOperationException($"Control extensoin method Add<T> may be called only for WinForm child control");
            }
            Type t        = winFormItem.PresentationType;
            var  instance = (Control)Activator.CreateInstance(t);

            instance.Tag = new WinFormRef()
            {
                WinForm = wf, WinFormItem = winFormItem
            };
            instance.Name = $"{t.Name}_{winFormItem.Name}";
            if (instance is Splitter)
            {
                instance.Dock = DockStyle.Right;
            }
            else
            {
                instance.Dock = DockStyle.Fill;
            }


            wf.Items.Add(winFormItem.Name, winFormItem);
            if (winFormItem.Value != null)
            {
                instance.Text = winFormItem.Value.ToString();
            }
            winFormItem.LinkedControl = instance;
            var cellAddress = winFormItem.CellAddress;

            TextBuX tbux = instance as TextBuX;

            void dataChanged(object c, EventArgs e)
            {
                (c as Control)?.GetOwner().OnDataChanged(new DataChangedEventArgs(c as Control));
            }

            if (instance is TextBoxBase || tbux != null)
            {
                instance.TextChanged += new EventHandler(dataChanged);
                instance.GotFocus    += TextBox_GotFocus;
                instance.KeyDown     += Enter_KeyDown;
                if (tbux != null)
                {
                    tbux.Click            += CmdTB_Click;
                    tbux.TextBox.GotFocus += TextBox_GotFocus;
                    if (winFormItem.DataType.IsNumeric(NumericTypesScope.All))
                    {
                        tbux.TextAlign = HorizontalAlignment.Right;
                        tbux.KeyPress += new KeyPressEventHandler(Num_KeyPress);
                    }
                    else if (winFormItem.DataType == typeof(string) && winFormItem.SuperForm == WinForm.StrViewFormList)
                    {
                        //tbux.TextBox.Enabled = false;
                        tbux.TextBox.Multiline = true;
                        tbux.TextBox.WordWrap  = false;
                        tbux.TextBox.MaxLength = winFormItem.DataSize;
                    }
                }
                else if (instance is TextBox && winFormItem.DataType.IsNumeric(NumericTypesScope.All))
                {
                    TextBox tb = (instance as TextBox);
                    tb.TextAlign = HorizontalAlignment.Right;
                    tb.KeyPress += new KeyPressEventHandler(Num_KeyPress);
                    if (!winFormItem.Format.IsEmpty())
                    {
                        tb.Text        = string.Format("{0:" + winFormItem.Format + "}", winFormItem.Value);
                        tb.Validating += (sender, e) =>
                        {
                            TextBox tbo = (sender as TextBox);
                            if (tbo == null || !tbo.GetSTDAction("Validating"))
                            {
                                return;
                            }
                            else
                            {
                                WinFormItem wfi = tbo.GetItem();
                                tbo.Text = string.Format("{0:" + wfi.Format + "}", tbo.Text.ToObjectOf(wfi.DataType));
                            }
                        };
                    }
                }
            }
            else if (typeof(ListControl).IsInstanceOfType(instance) || instance is CheckBox || instance is TabControl || instance is DateTimePicker)
            {
                instance.TextChanged += new EventHandler(dataChanged);
                instance.KeyDown     += Enter_KeyDown;
                ListControl lc = (instance as ListControl);
                if (lc != null)
                {
                    lc.SelectedValueChanged += new EventHandler(dataChanged);
                }
            }
            else if (instance is ICSharpCode.TextEditor.TextEditorControlBase)
            {
                instance.TextChanged += new EventHandler(dataChanged);
            }
            else if (instance is TableLayoutPanel && parent is TableLayoutPanel)
            {
                TableLayoutPanel tp      = instance as TableLayoutPanel;
                TableLayoutPanel tparent = parent as TableLayoutPanel;

                tparent.RowCount++;
                tp.CellBorderStyle = WinForm.CellBorderStyle;
                tp.Dock            = DockStyle.Fill;
                tp.AutoSize        = true;
                if (winFormItem.CellsSize != null)
                {
                    for (int i = 0; i < winFormItem.CellsSize.X; i++)
                    {
                        tp.ColumnCount++;
                        tp.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                    }
                    for (int i = 0; i < winFormItem.CellsSize.Y; i++)
                    {
                        tp.RowCount++;
                        tp.RowStyles.Add(new RowStyle(SizeType.AutoSize, WinForm.CellsRowHieght));
                    }
                }
            }

            if (cellAddress == WinFormItem.CellAddressDefault)
            {
                parent.Controls.Add(instance);
            }
            else
            {
                (parent as TableLayoutPanel)?.Controls.Add(instance, cellAddress.X, cellAddress.Y);
            }
            winFormItem.ControlTrigger?.Invoke(winFormItem, instance);
            foreach (WinFormItem item in winFormItem)
            {
                if (item.PresentationType != null && typeof(Control).IsAssignableFrom(item.PresentationType))
                {
                    AddInternal(instance, item);
                }
            }
            if (winFormItem.ReadOnly)
            {
                instance.Enabled = false;
            }
            parent.ResumeLayout();
            wf.OnItemAdded(winFormItem);
            return(instance);
        }
Exemplo n.º 9
0
 public void OnItemAdded(WinFormItem item)
 {
     ItemAdded?.Invoke(this, new ItemAddEventArgs(item));
 }
Exemplo n.º 10
0
 public ItemAddEventArgs(WinFormItem item)
 {
     Item = item;
 }