Ejemplo n.º 1
0
        static void CmdTB_Click(object sender, EventArgs e)
        {
            TextBuX cmd = (sender as TextBuX);

            cmd.GetOwner().OnItemButtonClick(cmd.GetItem());
        }
Ejemplo n.º 2
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);
        }