Visual studio 2010 中的自动格式化 lambda 函数

发布于 2024-12-17 12:39:36 字数 1397 浏览 0 评论 0原文

如何设置 Visual Studio 2010,以便多行 lambda 函数不会因为左侧的所有空白区域而显得难看?

   dataView.CellFormatting += (s, e) =>
                                           {
                                               if ((e.ColumnIndex == 1)&&((dataView.SelectedCells.Count == 1)))
                                               {    
                                                   var scope = Scope.Instance;    
                                                   var row = dataView.Rows[e.RowIndex];
                                                   var variable = row.DataBoundItem as Variable;

                                                   if (scope.Variables.Contains(variable))
                                                   {
                                                       dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                                                           scope.GetGraph(variable).Color;
                                                   }

                                                   else
                                                   {
                                                       dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
                                                   }
                                               }
                                           };

How to set up Visual studio 2010 so that multi-line lambda-functions will not look ugly, with all that empty space in the left?

   dataView.CellFormatting += (s, e) =>
                                           {
                                               if ((e.ColumnIndex == 1)&&((dataView.SelectedCells.Count == 1)))
                                               {    
                                                   var scope = Scope.Instance;    
                                                   var row = dataView.Rows[e.RowIndex];
                                                   var variable = row.DataBoundItem as Variable;

                                                   if (scope.Variables.Contains(variable))
                                                   {
                                                       dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                                                           scope.GetGraph(variable).Color;
                                                   }

                                                   else
                                                   {
                                                       dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
                                                   }
                                               }
                                           };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷月断魂刀 2024-12-24 12:39:36

这取决于你认为丑陋的空白有多少,但你可以做的一件事是尽量减少它,那就是在等号后面按回车键。然后你就会得到这样的结果。 `

   {
        var raw_custs =
            (from customer in GetActive()
             where customer.Name.Contains(name)
             select customer).Take(numberToGet).ToList();

我通常在进行这样的更改后立即点击 CTRl-E CTRL-D 以使文档自动格式化(编辑->高级->格式文档)

(刚刚看到你修改后的帖子 - 当我将其放入 VS 时并在 += 后按回车键

dataView.CellFormatting +=
    (s, e) =>
    {
        if ((e.ColumnIndex == 1) && ((dataView.SelectedCells.Count == 1)))
        {
            var scope = Scope.Instance;
            var row = dataView.Rows[e.RowIndex];
            var variable = row.DataBoundItem as Variable;

            if (scope.Variables.Contains(variable))
            {
                dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                    scope.GetGraph(variable).Color;
            }

            else
            {
                dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
            }
        }

It depends on how much white space you consider ugly but one thing you can do to minimize it is hit a carriage return right after the equal. Then you end up with something like this. `

   {
        var raw_custs =
            (from customer in GetActive()
             where customer.Name.Contains(name)
             select customer).Take(numberToGet).ToList();

I usually hit CTRl-E CTRL-D right after making a change like this to get the document to auto-format (Edit->Advanced->Format Document)

(Just saw your amended post - when I put that in VS and hit return after the +=

dataView.CellFormatting +=
    (s, e) =>
    {
        if ((e.ColumnIndex == 1) && ((dataView.SelectedCells.Count == 1)))
        {
            var scope = Scope.Instance;
            var row = dataView.Rows[e.RowIndex];
            var variable = row.DataBoundItem as Variable;

            if (scope.Variables.Contains(variable))
            {
                dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                    scope.GetGraph(variable).Color;
            }

            else
            {
                dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
            }
        }
半透明的墙 2024-12-24 12:39:36

这很奇怪——缩进不应该那么远。

尝试将其剪切并粘贴到位,Visual Studio 应该会在粘贴时为您修复它。这就是我得到的:

dataView.CellFormatting += (s, e) =>
{
    if ((e.ColumnIndex == 1) && ((dataView.SelectedCells.Count == 1)))
    {
        var scope = Scope.Instance;
        var row = dataView.Rows[e.RowIndex];
        var variable = row.DataBoundItem as Variable;

        if (scope.Variables.Contains(variable))
        {
            dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                scope.GetGraph(variable).Color;
        }

        else
        {
            dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
        }
    }
};

Now that's odd — the indentation shouldn't go that far.

Try cutting and pasting it in place and Visual Studio should fix it for you on pasting. This is what I get:

dataView.CellFormatting += (s, e) =>
{
    if ((e.ColumnIndex == 1) && ((dataView.SelectedCells.Count == 1)))
    {
        var scope = Scope.Instance;
        var row = dataView.Rows[e.RowIndex];
        var variable = row.DataBoundItem as Variable;

        if (scope.Variables.Contains(variable))
        {
            dataView[e.ColumnIndex, e.RowIndex].Style.BackColor =
                scope.GetGraph(variable).Color;
        }

        else
        {
            dataView[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
        }
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文