如何查找货币中使用点作为组分隔符的部分

问题描述 投票:0回答:1

如何使用正则表达式查找使用点作为组分隔符的货币部分(查看 DatagridView 中的单价列)?

当我在搜索文本框中输入数字 100 时,单价栏中的数字 1.00(红框中的数字)未被选中。我希望选择数字 1.00(黄色)。

以上结果来自以下代码:

private async Task AddProduct_DataGridView_CellPaintingAsync(object sender, DataGridViewCellPaintingEventArgs e)
{
    try
    {
        if (e.RowIndex < 0 || e.ColumnIndex < 0)
        {
            return;
        }
        // draw empty background
        e.PaintBackground(e.ClipBounds, true);

        // draw default content
        e.PaintContent(e.ClipBounds);

        // search String
        String searchString = AddProduct_SearchTextBox.Text;
        if (String.IsNullOrEmpty(searchString))
        {
            return;
        }

        // cell String
        String? cellValue = e.FormattedValue.ToString();
        if (String.IsNullOrEmpty(cellValue))
        {
            return;
        }

        // unit price
        if (e.ColumnIndex == 2)
        {
           
            //???
        }

        // search searchString in cellValue
        MatchCollection? searchPositions = Regex.Matches(cellValue, searchString);
        if (searchPositions is null)
        {
            return;
        }
        if (searchPositions.Count == 0)
        {
            return;
        }

        // construct a StringFormat object.
        using (StringFormat cellFormat = new StringFormat())
        {
            // set the ranges on the StringFormat object.
            cellFormat.SetMeasurableCharacterRanges(searchPositions.OfType<Match>().Select(x => new CharacterRange(x.Index, x.Length)).ToArray());

            // construct a new RectangleF.
            RectangleF rectF = new RectangleF(e.CellBounds.X + 0.8f, e.CellBounds.Y + 4.2f, e.CellBounds.Width + 0.0f, e.CellBounds.Height + 0.0f);
            using (Font cellFont = new Font("Microsoft Sans Serif", 10.5f, FontStyle.Regular))
            {
                // get the Region to highlight by calling the MeasureCharacterRanges method.
                Region[] regions = e.Graphics.MeasureCharacterRanges(cellValue, cellFont, rectF, cellFormat);
                if (regions.Length == 0)
                {
                    return;
                }

                // draw empty background
                e.PaintBackground(e.ClipBounds, true);
                foreach (Region? region in regions)
                {
                    if (region is not null)
                    {
                        using (var lightBrush = new SolidBrush(Color.FromArgb(0xFF, Color.Yellow)))
                        {
                            // fill in the region using a semi-transparent color.
                            e.Graphics.FillRegion(lightBrush, region);
                        }
                    }
                }

                // draw default content
                e.PaintContent(e.ClipBounds);
            }
        }
        // draw custom graphic content
        e.Handled = true;

        await Task.Yield();
    }
    catch (Exception ex)
    {
        Log.Exception(ex);
    }
}

谢谢。

c# winforms
1个回答
0
投票

我用这段代码解决了这个问题:

// unit price
if (e.ColumnIndex == 2)
{
    CultureInfo culture = Common.GetCultureInfo();
    String currencySymbol = culture.NumberFormat.CurrencySymbol;
    String currencyGroupSeparator = culture.NumberFormat.CurrencyGroupSeparator;

    String cellValueWithoutCurrencySymbolAndGroupSeparator = "";
    if (!searchString.Contains(currencySymbol) && !searchString.Contains(currencyGroupSeparator))
    {
        cellValueWithoutCurrencySymbolAndGroupSeparator = Common.RemoveNonDigit(cellValue);
    }
    if (String.IsNullOrEmpty(cellValueWithoutCurrencySymbolAndGroupSeparator))
    {
        return;
    }
    int location = -1;
    string realSelected = "";
    for (int i = 0; i < cellValueWithoutCurrencySymbolAndGroupSeparator.Length; i++)
    {
        if (String.CompareOrdinal(cellValueWithoutCurrencySymbolAndGroupSeparator, i, searchString, 0, searchString.Length) == 0)
        {
            location = currencySymbol.Length + i;
            realSelected = cellValue.Substring(location, searchString.Length);
            break;
        }
    }
    if (location >= 0 && !String.IsNullOrEmpty(realSelected))
    {
        if (realSelected.Contains(currencyGroupSeparator))
        {
            searchString = cellValue.Substring(location, searchString.Length + 1);
        }
        else
        {
            searchString = cellValue.Substring(location, searchString.Length);
        }
    }
}

结果:

© www.soinside.com 2019 - 2024. All rights reserved.