Ejemplo n.º 1
0
        public FormatPage()
        {
            InitializeComponent();

            #region Initialize Setting Panels

            numberNegativeStyleList.Items.Add(new NegativeStyleListItem(
                                                  NumberDataFormatter.NumberNegativeStyle.Default, (-1234.10f).ToString("###,###.00"), SystemColors.WindowText));
            numberNegativeStyleList.Items.Add(new NegativeStyleListItem(
                                                  NumberDataFormatter.NumberNegativeStyle.Red, (1234.10f).ToString("###,###.00"), Color.Red));
            numberNegativeStyleList.Items.Add(new NegativeStyleListItem(
                                                  NumberDataFormatter.NumberNegativeStyle.Brackets, (1234.10f).ToString("(###,###.00)"), SystemColors.WindowText));
            numberNegativeStyleList.Items.Add(new NegativeStyleListItem(
                                                  NumberDataFormatter.NumberNegativeStyle.RedBrackets, (1234.10f).ToString("(###,###.00)"), Color.Red));

            if (numberNegativeStyleList.Items.Count > 0)
            {
                numberNegativeStyleList.SelectedIndex = 0;
            }

            datetimeFormatList.SelectedIndexChanged += (s, e) =>
            {
                if (datetimeFormatList.SelectedItem != null)
                {
                    DatetimeFormatListItem item = (DatetimeFormatListItem)datetimeFormatList.SelectedItem;
                    txtDatetimeFormat.Text = item.Pattern;
                }
            };

            var currentCulture = Thread.CurrentThread.CurrentCulture;

            datetimeFormatList.Items.AddRange(new object[] {
                // culture patterns
                new DatetimeFormatListItem(true, currentCulture.DateTimeFormat.ShortDatePattern),
                new DatetimeFormatListItem(true, currentCulture.DateTimeFormat.LongDatePattern),
                new DatetimeFormatListItem(true, currentCulture.DateTimeFormat.ShortDatePattern
                                           + " " + currentCulture.DateTimeFormat.ShortTimePattern),
                new DatetimeFormatListItem(true, currentCulture.DateTimeFormat.LongDatePattern
                                           + " " + currentCulture.DateTimeFormat.LongTimePattern),
                new DatetimeFormatListItem(true, currentCulture.DateTimeFormat.ShortTimePattern),
                new DatetimeFormatListItem(true, currentCulture.DateTimeFormat.LongTimePattern),

                // predefine patterns
                new DatetimeFormatListItem(false, "yyyy/M/d"),
                new DatetimeFormatListItem(false, "yyyy/M/d hh:mm"),
                new DatetimeFormatListItem(false, "M/d"),
                new DatetimeFormatListItem(false, "hh:mm"),
            });

            datetimeLocationList.SelectedIndexChanged += (s, e) =>
            {
                var ci = (CultureInfo)datetimeLocationList.SelectedItem;

                ((DatetimeFormatListItem)(datetimeFormatList.Items[0])).Pattern = ci.DateTimeFormat.ShortDatePattern;
                ((DatetimeFormatListItem)(datetimeFormatList.Items[1])).Pattern = ci.DateTimeFormat.LongDatePattern;
                ((DatetimeFormatListItem)(datetimeFormatList.Items[2])).Pattern = ci.DateTimeFormat.ShortDatePattern + " " + ci.DateTimeFormat.ShortTimePattern;
                ((DatetimeFormatListItem)(datetimeFormatList.Items[3])).Pattern = ci.DateTimeFormat.LongDatePattern + " " + ci.DateTimeFormat.LongTimePattern;
                ((DatetimeFormatListItem)(datetimeFormatList.Items[4])).Pattern = ci.DateTimeFormat.ShortTimePattern;
                ((DatetimeFormatListItem)(datetimeFormatList.Items[5])).Pattern = ci.DateTimeFormat.LongTimePattern;

                DateTime    dt      = new DateTime(1980, 7, 13);
                CultureInfo culture = (CultureInfo)datetimeLocationList.SelectedItem;
                foreach (DatetimeFormatListItem item in datetimeFormatList.Items)
                {
                    item.Sample = dt.ToString(item.Pattern, culture);
                }

                typeof(ListBox).InvokeMember("RefreshItems",
                                             BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
                                             null, datetimeFormatList, new object[] { });

                object backup = datetimeFormatList.SelectedItem;
                if (backup != null)
                {
                    datetimeFormatList.SelectedItem = null;
                    datetimeFormatList.SelectedItem = backup;
                }
                else
                {
                    datetimeFormatList.SelectedIndex = 0;
                }
            };

            currencySymbolList.SelectedIndexChanged += (s, e) =>
            {
                var listItem = (CurrencySymbolListItem)currencySymbolList.SelectedItem;

                currencyNegativeStyleList.Items.Clear();

                currencyNegativeStyleList.Items.Add(new NegativeStyleListItem(
                                                        NumberDataFormatter.NumberNegativeStyle.Default, /*"-$1,234.10 ###,###.00"*/ (1234.10f).ToString("c", listItem.Culture), SystemColors.WindowText));
                currencyNegativeStyleList.Items.Add(new NegativeStyleListItem(
                                                        NumberDataFormatter.NumberNegativeStyle.Red, /*"$1,234.10"*/ (1234.10f).ToString("c", listItem.Culture), Color.Red));
                currencyNegativeStyleList.Items.Add(new NegativeStyleListItem(
                                                        NumberDataFormatter.NumberNegativeStyle.Brackets, /*"($1,234.10)"*/ "(" + (1234.10f).ToString("c", listItem.Culture) + ")", SystemColors.WindowText));
                currencyNegativeStyleList.Items.Add(new NegativeStyleListItem(
                                                        NumberDataFormatter.NumberNegativeStyle.RedBrackets, /*"($1,234.10)"*/ "(" + (1234.10f).ToString("c", listItem.Culture) + ")", Color.Red));

                if (currentFormatArgs != null)
                {
                    currencyFormatArgs.PrefixSymbol  = null;
                    currencyFormatArgs.PostfixSymbol = null;
                }

                if (currencyNegativeStyleList.Items.Count > 0)
                {
                    currencyNegativeStyleList.SelectedIndex = 0;
                }

                UpdateSample();
            };

            var cultures = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures).OrderBy(c => c.EnglishName);
            foreach (CultureInfo info in cultures)
            {
                datetimeLocationList.Items.Add(info);
                if (info.Equals(currentCulture))
                {
                    datetimeLocationList.SelectedItem = info;
                }

                try
                {
                    var item = new CurrencySymbolListItem(info);

                    currencySymbolList.Items.Add(item);

                    if (info.Equals(currentCulture))
                    {
                        currencySymbolList.SelectedItem = item;
                    }
                }
                catch { }
            }

            // add valid data formatter
            foreach (var key in Enum.GetValues(typeof(CellDataFormatFlag)))
            {
                IDataFormatter formatter;
                if (DataFormatterManager.Instance.DataFormatters.TryGetValue((CellDataFormatFlag)key, out formatter))
                {
                    formatList.Items.Add(key);
                }
            }

            numberFormatArgs = new NumberDataFormatter.NumberFormatArgs
            {
                DecimalPlaces = 2,
                NegativeStyle = NumberDataFormatter.NumberNegativeStyle.Default,
                UseSeparator  = true,
            };
            datetimeFormatArgs = new DateTimeDataFormatter.DateTimeFormatArgs
            {
                Format = currentCulture.DateTimeFormat.ShortDatePattern,
            };
            currencyFormatArgs = new CurrencyDataFormatter.CurrencyFormatArgs()
            {
                CultureEnglishName = currentCulture.EnglishName,
                DecimalPlaces      = (short)currentCulture.NumberFormat.CurrencyDecimalDigits,
                NegativeStyle      = NumberDataFormatter.NumberNegativeStyle.Default,
            };

            #endregion

            formatList.SelectedIndexChanged += (sender, e) =>
            {
                try
                {
                    currentFormat = (CellDataFormatFlag)Enum.Parse(typeof(CellDataFormatFlag), formatList.Text);
                }
                catch
                {
                    currentFormat = CellDataFormatFlag.General;
                }

                Panel newSettingPanel = null;

                switch (currentFormat)
                {
                case CellDataFormatFlag.Number:
                    newSettingPanel = numberPanel;
                    break;

                case CellDataFormatFlag.DateTime:
                    newSettingPanel = datetimePanel;
                    break;

                case CellDataFormatFlag.Currency:
                    newSettingPanel = currencyPanel;
                    break;

                case CellDataFormatFlag.Percent:
                    newSettingPanel = percentPanel;
                    break;
                }

                if (newSettingPanel != currentSettingPanel)
                {
                    if (newSettingPanel != null)
                    {
                        newSettingPanel.Location = new Point(grpSample.Left - 5, grpSample.Bottom + 10);
                        newSettingPanel.Show();
                    }

                    if (currentSettingPanel != null)
                    {
                        currentSettingPanel.Hide();
                    }

                    currentSettingPanel = newSettingPanel;
                }

                UpdateSample();
            };

            numberDecimalPlaces.ValueChanged               += (s, e) => UpdateSample();
            chkNumberUseSeparator.CheckedChanged           += (s, e) => UpdateSample();
            numberNegativeStyleList.SelectedIndexChanged   += (s, e) => UpdateSample();
            currencyDecimalPlaces.ValueChanged             += (s, e) => UpdateSample();
            currencyNegativeStyleList.SelectedIndexChanged += (s, e) => UpdateSample();
            percentDecimalPlaces.ValueChanged              += (s, e) => UpdateSample();

            formatList.DoubleClick += (s, e) => RaiseDone();
            numberNegativeStyleList.DoubleClick   += (s, e) => RaiseDone();
            datetimeFormatList.DoubleClick        += (s, e) => RaiseDone();
            currencyNegativeStyleList.DoubleClick += (s, e) => RaiseDone();

            txtDatetimeFormat.TextChanged += (s, e) => UpdateSample();
        }
Ejemplo n.º 2
0
        public void LoadPage()
        {
            var sheet = this.grid.CurrentWorksheet;

            sheet.IterateCells(sheet.SelectionRange, (r, c, cell) =>
            {
                if (backupFormat == null)
                {
                    sampleCell = cell.Clone();
                    unvell.ReoGrid.Utility.CellUtility.CopyCellContent(sampleCell, cell);

                    if (cell != null)
                    {
                        originalData = cell.Data;
                    }

                    backupFormat = cell.DataFormat;
                    return(true);
                }
                else if (backupFormat == cell.DataFormat)
                {
                    return(true);
                }
                else
                {
                    backupFormat = null;
                    return(false);
                }
            });

            currentFormat = backupFormat;

            backupFormatArgs = null;

            if (currentFormat != null)
            {
                switch (currentFormat)
                {
                case CellDataFormatFlag.Number:
                    if (sampleCell.DataFormatArgs is NumberDataFormatter.NumberFormatArgs)
                    {
                        NumberDataFormatter.NumberFormatArgs nargs = (NumberDataFormatter.NumberFormatArgs)sampleCell.DataFormatArgs;
                        numberDecimalPlaces.Value     = nargs.DecimalPlaces;
                        chkNumberUseSeparator.Checked = nargs.UseSeparator;
                        foreach (NegativeStyleListItem item in numberNegativeStyleList.Items)
                        {
                            if (item.NegativeStyle == nargs.NegativeStyle)
                            {
                                numberNegativeStyleList.SelectedItem = item;
                                break;
                            }
                        }
                        backupFormatArgs = nargs;
                    }
                    break;

                case CellDataFormatFlag.DateTime:
                    DateTimeDataFormatter.DateTimeFormatArgs dargs = (DateTimeDataFormatter.DateTimeFormatArgs)sampleCell.DataFormatArgs;
                    txtDatetimeFormat.Text = dargs.Format;
                    int dfindex = -1;
                    for (int i = 0; i < datetimeFormatList.Items.Count; i++)
                    {
                        DatetimeFormatListItem item = (DatetimeFormatListItem)datetimeFormatList.Items[i];
                        if (item.Pattern.Equals(dargs.Format, StringComparison.CurrentCultureIgnoreCase))
                        {
                            dfindex = i;
                            break;
                        }
                    }
                    datetimeFormatList.SelectedIndex = dfindex;
                    backupFormatArgs = dargs;
                    break;

                case CellDataFormatFlag.Currency:
                    var cargs = (CurrencyDataFormatter.CurrencyFormatArgs)sampleCell.DataFormatArgs;

                    var cultureName = cargs.CultureEnglishName;

                    foreach (var currencyCultureItem in currencySymbolList.Items)
                    {
                        if (string.Compare(((CurrencySymbolListItem)currencyCultureItem).Culture.EnglishName, cultureName, true) == 0)
                        {
                            currencySymbolList.SelectedItem = currencyCultureItem;
                            break;
                        }
                    }

                    currencyDecimalPlaces.Value = cargs.DecimalPlaces;

                    int cnindex = (int)cargs.NegativeStyle;
                    if (cnindex >= 0 && cnindex < currencyNegativeStyleList.Items.Count)
                    {
                        currencyNegativeStyleList.SelectedIndex = cnindex;
                    }

                    foreach (NegativeStyleListItem item in currencyNegativeStyleList.Items)
                    {
                        if (item.NegativeStyle == cargs.NegativeStyle)
                        {
                            currencyNegativeStyleList.SelectedItem = item;
                            break;
                        }
                    }

                    backupFormatArgs = cargs;
                    break;

                case CellDataFormatFlag.Percent:
                    var pargs = (NumberDataFormatter.NumberFormatArgs)sampleCell.DataFormatArgs;
                    percentDecimalPlaces.Value = pargs.DecimalPlaces;
                    backupFormatArgs           = pargs;
                    break;
                }

                for (int i = 0; i < formatList.Items.Count; i++)
                {
                    var item = formatList.Items[i].ToString();

                    if (string.Equals(item, currentFormat.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        formatList.SelectedIndex = i;
                        break;
                    }
                }
            }
            else
            {
                formatList.SelectedIndex = 0;
            }

            backupFormat = currentFormat;
        }