将 C# 语句体 lambda 转换为 VB

发布于 2024-08-07 14:21:08 字数 1945 浏览 3 评论 0原文

VS8 中的 VB 似乎不支持/转换带有语句主体的 lambda 表达式。我在 C# 应用程序中使用它们,但现在必须将其转换为 VB。

我正在动态创建一大堆控件,并且我希望能够动态地为它们提供事件处理程序。这样我就可以从数据库构建动态用户界面。在下面的代码中,我将创建一个表单和一个复选框,使复选框控制表单的可见性,向表单添加一些方法处理程序,然后将新创建的复选框添加到预先存在的表单/面板/等中。例如,表单的处理程序会影响复选框:

  // Inside some loop creating a lot of these forms and matching checkboxes
      Form frmTemp = frmTestPanels[i];  // New form in array
      CheckBox chkSelectPanel;          // New checkbox that selects this panel
      chkSelectPanel = new CheckBox();
      chkSelectPanel.Text = SomeName;   // Give checkbox a label
      chkSelectPanel.Click += (ss, ee) =>  // When clicked
      {
          label1.Text = SomeName;       // Update a label
          if (chkSelectPanel.Checked)   // Show or hide the form
          {
              frmTemp.Show();
          }
          else
          {
              frmTemp.Hide();
          }
      };

      frmTemp.VisibleChanged += (ss, ee) =>  // When form visibility changes
      {
          chkSelectPanel.Checked = frmTemp.Visible;  // Reflect change to checkbox
          ConfigurationFileChanged = true;   // Update config file later
      };

      frmTemp.FormClosing += (ss, ee) =>     // When the form closes
      {   // We're only pretending to close the form - it'll sit around until needed
          chkSelectPanel.Checked = false;    // Update the checkbox
          frmTemp.Hide();                    // Hide the form
          ee.Cancel = true;                  // Cancel the close
      };

      flpSelectGroup.Controls.Add(chkSelectPanel); // Add checkbox to flow layout panel
  // End of loop creating a bunch of these matching forms/checkboxes

当然,我收到转换错误:

VB 不支持带有语句体的匿名方法/lambda 表达式

我真的很喜欢动态创建所有内容的能力,然后让对象自行处理 - 我不需要添加任何特殊函数来确定给出的形式关闭事件,以便它可以搜索正确的复选框并更新该复选框 - It Just Works (TM)。

不幸的是它需要转换为VB。

将 lambda/匿名语句体转换为可在 VB 中运行的内容的最佳方法是什么,特别是当需要创建其中许多语句体时?

It appears that VB in VS8 doesn't support/convert lambda expressions with a statement body. I'm using them in my C# application, but now must convert it to VB.

I'm creating a whole bunch of controls dynamically, and I want to be able to give them event handlers on the fly. This is so I can build a dynamic user interface from a database. In the following code I'll create a form and a checkbox, make the checkbox control the form's visibility, add a few method handlers to the form, and then add the newly created checkbox to a pre-existing form/panel/etc. Handlers for the form, for instance, affect the checkbox:

  // Inside some loop creating a lot of these forms and matching checkboxes
      Form frmTemp = frmTestPanels[i];  // New form in array
      CheckBox chkSelectPanel;          // New checkbox that selects this panel
      chkSelectPanel = new CheckBox();
      chkSelectPanel.Text = SomeName;   // Give checkbox a label
      chkSelectPanel.Click += (ss, ee) =>  // When clicked
      {
          label1.Text = SomeName;       // Update a label
          if (chkSelectPanel.Checked)   // Show or hide the form
          {
              frmTemp.Show();
          }
          else
          {
              frmTemp.Hide();
          }
      };

      frmTemp.VisibleChanged += (ss, ee) =>  // When form visibility changes
      {
          chkSelectPanel.Checked = frmTemp.Visible;  // Reflect change to checkbox
          ConfigurationFileChanged = true;   // Update config file later
      };

      frmTemp.FormClosing += (ss, ee) =>     // When the form closes
      {   // We're only pretending to close the form - it'll sit around until needed
          chkSelectPanel.Checked = false;    // Update the checkbox
          frmTemp.Hide();                    // Hide the form
          ee.Cancel = true;                  // Cancel the close
      };

      flpSelectGroup.Controls.Add(chkSelectPanel); // Add checkbox to flow layout panel
  // End of loop creating a bunch of these matching forms/checkboxes

Of course, I'm getting the conversion error:

VB does not support anonymous methods/lambda expressions with a statement body

I really liked the ability to create everything on the fly, and then let the objects handle themselves - I don't need to add any special functions that figure out which form is giving the close event so it can search for the right checkbox and update the checkbox - It Just Works (TM).

Unfortunately it needs to be converted to VB.

What is the best way to convert lambda/anonymous statement bodies into something that will work in VB, especially when one needs to create many of them?

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

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

发布评论

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

评论(2

哥,最终变帅啦 2024-08-14 14:21:08

等待.NET 4的最新版本,它将支持VB中的此类功能。看不到其他选择。

丑陋的替代方案是:

  1. 这可行,但您可以在函数中使用单个语句。

    AddHandler Me.Click, Function(o, e) MessageBox.Show("text")
    
  2. 创建一些常规的 Sub Foo

    Public Sub Foo(ByVal o 作为对象,ByVal e 作为 EventArgs)
         MessageBox.Show("文本")
    结束子
    

    并使用AddHandler将其绑定到事件

    AddHandler Me.Click,AddressOf Foo
    

Wait for the nearest release of .NET 4, it will support things like this in VB. Don't see other alternative.

Ugly alternatives are:

  1. This work, but you can use a single statement in a function.

    AddHandler Me.Click, Function(o, e) MessageBox.Show("text")
    
  2. Create some regular Sub Foo

    Public Sub Foo(ByVal o As Object, ByVal e As EventArgs)
         MessageBox.Show("text")
    End Sub
    

    and use AddHandler to bind it to an event

    AddHandler Me.Click, AddressOf Foo
    
℉絮湮 2024-08-14 14:21:08

您能否创建一个新类,在构造函数中接受 Form 并将 chkSelectPanel 作为字段,从而允许您使用实例方法作为事件处理程序?

Could you make a new class that accepts the Form in the constructor and has chkSelectPanel as a field, allowing you to use instance methods as your event handlers?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文