将编辑文本的颜色更改为红色 [英] Change color of edited text to red

查看:276
本文介绍了将编辑文本的颜色更改为红色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五列gridview。我已经加载了它的数据。用户可以编辑列单元格中的文本。如果用户编辑文本,那些编辑的文本必须为红色。仅编辑

I have gridview with five columns. i have loaded the data on it. the user can edit the text within the column cell. if the user edited the text,those edited text must be red. Only edited

例如

stackoverflow = StackOverFlow(您会注意到我已经更改/编辑的帽子必须改变颜色,在这种情况下SO和F会改变颜色为红色)

stackoverflow = StackOverFlow (you will note that i have change/edited to caps. those edited caps must change color. in this case S O and F will change color to red)

这是我试过,但不工作

 private void Gridview_CellBeginEdit_1(object sender, DataGridViewCellCancelEventArgs e)
    {
        DataGridViewCell cell = Gridview_Output[e.ColumnIndex, e.RowIndex];
        if (cell.Tag != null && cell.Tag.ToString() != cell.Value.ToString())
            cell.Style.ForeColor = Color.Red;
        else
            DataGridViewCell cell = Gridview_Output[e.ColumnIndex, e.RowIndex];
            cell.Tag = cell.Value != null ? cell.Value : "";

    }


推荐答案

简单部分的代码示例:

它检查一行(4)中的一列(0),然后绘制在元组列表中准备的字符串。您可以轻松更改数据结构。

It checks for one column (0) in one row (4) and then draws the strings prepared in a list of tuples. You can change the data structure easily..

List<Tuple<SolidBrush, string>> diffStrings = new List<Tuple<SolidBrush, string>>();

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex != 4 || e.ColumnIndex != 0) { return; }  // only one test-cell
    float x = e.CellBounds.Left + 1;
    float y = e.CellBounds.Top + 1;

    e.PaintBackground(e.CellBounds,true);
    foreach (Tuple<SolidBrush, string> kv in diffStrings)
    {
        SizeF size = e.Graphics.MeasureString(kv.Item2, Font, 
                     e.CellBounds.Size, StringFormat.GenericTypographic);
        if (x + size.Width > e.CellBounds.Left + e.CellBounds.Width)  
           {x = e.CellBounds.Left + 1; y += size.Height; }
        e.Graphics.DrawString(kv.Item2, Font, kv.Item1,   x , y);
        x += size.Width;

    }
    e.Handled = true;
}

但是你从哪里得到数据?

But where will you get the data from??

以下是我创建测试数据的方式,它们只是用不同的颜色显示绘图:

Here is how I created the test data, which are only meant to show the drawing in different colors:

SolidBrush c1 = new SolidBrush(Color.Black);
SolidBrush c2 = new SolidBrush(Color.Red);

diffStrings.Add(new Tuple<SolidBrush, string>(c1, "1234"));
diffStrings.Add(new Tuple<SolidBrush, string>(c2, "M"));
diffStrings.Add(new Tuple<SolidBrush, string>(c1, "1234"));
diffStrings.Add(new Tuple<SolidBrush, string>(c2, "ÖÄÜ"));
diffStrings.Add(new Tuple<SolidBrush, string>(c1, "1234"));
diffStrings.Add(new Tuple<SolidBrush, string>(c2, "ÖÄÜ"));
diffStrings.Add(new Tuple<SolidBrush, string>(c1, "1234"));
..

你需要解决写一个Diff函数结构。

You will need to solve the problem of writing a Diff function that can fill the structure. It would help if you know restraints, like maybe that the length can't change..

你可以使用 Cells ' diff 使用旧值,如果将它们存储在 '标签。你必须处理例如。 CellBeginEdit 和/或 CellEndEdit 事件来管理这两个值的存储,但真正的挑战是获得diff

You can use the Cells' Values and diff them with their old values if you store these in the Cells' Tags. You would have to handle e.g. the CellBeginEdit and/or CellEndEdit event to manage the storage of the two values but the real challenge is to get the diffs, especially when there are inserted or deleted characters!

上面的示例将文本 TopLeft 对齐。包括所有 DataGridViewContentAlignment 选项会使代码复杂化,但只有有限的方式。同样适用于拆分字边界或管理嵌入的换行符。 - 编写 Diff 功能真的很难。请参阅这里是一个例子如何使用现成的一个..!

The example above aligns the text TopLeft. Including all DataGridViewContentAlignment options would complicate the code somewhat, but only in a limited way. The same goes for splitting after word boundaries or managing embeded line feeds.. - Writing a Diff function however is really tough. See here for an example of how to use a ready-made one..!

这篇关于将编辑文本的颜色更改为红色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆