Пример #1
0
        private void CreateControls()
        {
            using (Graphics gr = this.CreateGraphics())
            {
                // Measure texts
                int labelwidth = 0;
                foreach (Param p in FReport.Params)
                {
                    if (p.UserVisible)
                    {
                        int nwidth = (int)Math.Round(gr.MeasureString(p.Description, this.Font).Width);
                        if (nwidth > labelwidth)
                        {
                            labelwidth = nwidth;
                        }
                        if (labelwidth > MaxLabelWidth)
                        {
                            labelwidth = MaxLabelWidth;
                        }
                    }
                }
                bool      focused      = false;
                const int TOP_GAP      = 3;
                const int MIDDLE_GAP   = 3;
                const int BOTTOM_GAP   = 10;
                const int BUTTON_WIDTH = 30;
                const int LEFT_GAP     = 3;
                const int GAP_LABEL    = 10;
                const int BUTTON_GAP   = 2;
                const int RIGHT_GAP    = BUTTON_WIDTH + 30 + BUTTON_GAP;
                int       posy         = TOP_GAP;
                int       lastheight   = 0;
                foreach (Param p in FReport.Params)
                {
                    if (p.UserVisible)
                    {
                        ControlInfo cinfo = new ControlInfo();
                        cinfo.param          = p;
                        cinfo.label          = new Label();
                        cinfo.label.AutoSize = false;
                        cinfo.label.Text     = p.Description;
                        cinfo.label.Width    = labelwidth + LEFT_GAP;
                        cinfo.label.Left     = 0;
                        cinfo.label.Top      = posy;
                        cinfo.label.Parent   = this;
                        switch (p.ParamType)
                        {
                        case ParamType.Bool:
                            cinfo.control = new CheckBox();
                            ((CheckBox)cinfo.control).Checked = (bool)p.LastValue;
                            break;

                        case ParamType.String:
                        case ParamType.Subst:
                        case ParamType.SubstExpre:
                        case ParamType.ExpreA:
                        case ParamType.ExpreB:
                            cinfo.control = new TextBox();
                            ((TextBox)cinfo.control).Text = (string)p.LastValue;
                            break;

                        case ParamType.Date:
                            cinfo.control = new DateTimePicker();
                            ((DateTimePicker)cinfo.control).Value  = (DateTime)p.LastValue;
                            ((DateTimePicker)cinfo.control).Format = DateTimePickerFormat.Short;
                            break;

                        case ParamType.Time:
                            cinfo.control = new DateTimePicker();
                            ((DateTimePicker)cinfo.control).Value        = (DateTime)p.LastValue;
                            ((DateTimePicker)cinfo.control).Format       = DateTimePickerFormat.Custom;
                            ((DateTimePicker)cinfo.control).ShowUpDown   = true;
                            ((DateTimePicker)cinfo.control).CustomFormat = "hh:mm:ss";
                            break;

                        case ParamType.DateTime:
                            cinfo.control = new DateTimePicker();
                            ((DateTimePicker)cinfo.control).Value  = (DateTime)p.LastValue;
                            ((DateTimePicker)cinfo.control).Format = DateTimePickerFormat.Short;
                            cinfo.control2 = new DateTimePicker();
                            ((DateTimePicker)cinfo.control2).Value        = (DateTime)p.LastValue;
                            ((DateTimePicker)cinfo.control2).Format       = DateTimePickerFormat.Custom;
                            ((DateTimePicker)cinfo.control2).ShowUpDown   = true;
                            ((DateTimePicker)cinfo.control2).CustomFormat = "hh:mm:ss";
                            break;

                        case ParamType.Integer:
                            cinfo.control = new NumericUpDown();
                            ((NumericUpDown)cinfo.control).Minimum = System.Int32.MinValue;
                            ((NumericUpDown)cinfo.control).Maximum = System.Int32.MaxValue;
                            ((NumericUpDown)cinfo.control).Value   = (int)p.LastValue;
                            break;

                        case ParamType.Currency:
                            cinfo.control = new NumericUpDown();
                            ((NumericUpDown)cinfo.control).Minimum = System.Decimal.MinValue;
                            ((NumericUpDown)cinfo.control).Maximum = System.Decimal.MaxValue;
                            ((NumericUpDown)cinfo.control).Value   = (decimal)p.LastValue;
                            break;

                        case ParamType.Double:
                            cinfo.control = new TextBox();
                            ((TextBox)cinfo.control).Text = ((double)p.LastValue).ToString();
                            break;

                        case ParamType.List:
                        case ParamType.SubsExpreList:
                            cinfo.control = new ComboBox();
                            ((ComboBox)cinfo.control).DropDownStyle = ComboBoxStyle.DropDownList;
                            foreach (string sitem in p.Items)
                            {
                                ((ComboBox)cinfo.control).Items.Add(sitem);
                            }
                            ((ComboBox)cinfo.control).SelectedIndex = p.Values.IndexOf((string)p.Value);
                            if (((ComboBox)cinfo.control).SelectedIndex == -1)
                            {
                                if (p.Value.IsInteger())
                                {
                                    int idxcombo = p.LastValue;
                                    if (p.Value <= ((ComboBox)cinfo.control).Items.Count)
                                    {
                                        ((ComboBox)cinfo.control).SelectedIndex = idxcombo;
                                    }
                                }
                            }
                            break;

                        case ParamType.Multiple:
                            p.UpdateLookupValues();
                            CheckedListBox checkcontrol = new CheckedListBox();
                            cinfo.control = checkcontrol;
                            // Add menu to select/deselect all
                            ContextMenu nmenu     = new ContextMenu();
                            MenuItem    firstmenu = nmenu.MenuItems.Add(p.Alias);
                            firstmenu.Visible = false;
                            MenuItem mselectall = nmenu.MenuItems.Add(Translator.TranslateStr(1445));
                            MenuItem mclear     = nmenu.MenuItems.Add(Translator.TranslateStr(1446));
                            this.CheckListBoxes.Add(p.Alias, cinfo.control);
//								mselectall.Tag = cinfo.control;
//                                mclear.Tag = cinfo.control;
                            mselectall.Click         += new EventHandler(contextmenuclickselectall);
                            mclear.Click             += new EventHandler(contextmenuclickclear);
                            cinfo.control.ContextMenu = nmenu;
                            foreach (string sitem in p.Items)
                            {
                                ((CheckedListBox)cinfo.control).Items.Add(sitem);
                            }
                            foreach (string sitem in  p.Selected)
                            {
//                                    int index = System.Convert.ToInt32(sitem);
//                                    if (index<((CheckedListBox)cinfo.control).Items.Count)
//                                    {
//                                        ((CheckedListBox)cinfo.control).SetItemChecked(index,true);
//                                    }
                                // Selection by value instead of by index
                                int index = p.Values.IndexOf(sitem);
                                if (index >= 0)
                                {
                                    checkcontrol.SetItemChecked(index, true);
                                }
                            }
                            // Adjust Height up to 10 items
                            if (checkcontrol.Items.Count < 10)
                            {
                                checkcontrol.Height = checkcontrol.ItemHeight * (checkcontrol.Items.Count + 1);
                            }
                            else
                            {
                                checkcontrol.Height = checkcontrol.ItemHeight * (11);
                            }
                            break;
                        }
                        cinfo.control.Left    = labelwidth + LEFT_GAP + GAP_LABEL;
                        cinfo.control.Enabled = !p.IsReadOnly;
                        if (cinfo.control2 != null)
                        {
                            cinfo.control2.Enabled = !p.IsReadOnly;
                            cinfo.control.Width    = ControlWidth / 2;
                            cinfo.control2.Top     = posy;
                            cinfo.control2.Parent  = this;
                            cinfo.control2.Width   = (ControlWidth / 2) - LEFT_GAP;
                            cinfo.control2.Left    = labelwidth + LEFT_GAP + GAP_LABEL + (ControlWidth / 2) + LEFT_GAP;
                        }
                        else
                        {
                            cinfo.control.Width = ControlWidth;
                        }
                        cinfo.control.Parent  = this;
                        cinfo.control.Top     = posy;
                        cinfo.label.Height    = cinfo.control.Height;
                        cinfo.label.TextAlign = ContentAlignment.MiddleRight;
                        cinfo.control.Tag     = cinfo;
                        cinfo.button          = new Button();
                        cinfo.button.Tag      = cinfo;
                        cinfo.button.Click   += eventclick;
                        cinfo.button.Text     = "...";
                        cinfo.button.Width    = BUTTON_WIDTH;
                        cinfo.button.Height   = cinfo.control.Height;
                        cinfo.button.Left     = cinfo.control.Left + cinfo.control.Width + BUTTON_GAP;
                        if (cinfo.control2 != null)
                        {
                            cinfo.button.Left = cinfo.button.Left + cinfo.control2.Width + MIDDLE_GAP;
                        }
                        cinfo.button.Top     = posy;
                        cinfo.button.Visible = (p.SearchDataset.Length > 0);
                        cinfo.button.Parent  = this;
                        if (cinfo.control.Enabled)
                        {
                            if (!focused)
                            {
                                cinfo.control.Focus();
                            }
                        }

                        cinfo.PosY = posy;
                        lastheight = cinfo.control.Height;
                        posy       = posy + cinfo.control.Height + MIDDLE_GAP;

                        ControlList.Add(cinfo);
                    }
                }
                this.Width  = labelwidth + LEFT_GAP + GAP_LABEL + ControlWidth + LEFT_GAP + RIGHT_GAP;
                this.Height = posy + lastheight + MIDDLE_GAP + BOTTOM_GAP;
            }
        }