代码之家  ›  专栏  ›  技术社区  ›  Adam Davis

将C#语句体lambda转换为VB

  •  2
  • Adam Davis  · 技术社区  · 16 年前

      // 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
    

    当然,我遇到了转换错误:

    我真的很喜欢动态创建所有内容,然后让对象自己处理的能力——我不需要添加任何特殊函数来找出哪个表单发出关闭事件,这样它就可以搜索正确的复选框并更新复选框——它只是有效的(TM)。

    2 回复  |  直到 3 年前
        1
  •  4
  •   Kamarey    16 年前

    等待最近的释放。NET 4,它将在VB中支持这样的东西。看不到其他替代方案。

    1. AddHandler Me.Click, Function(o, e) MessageBox.Show("text")
      
    2. Public Sub Foo(ByVal o As Object, ByVal e As EventArgs)
           MessageBox.Show("text")
      End Sub
      

      和使用 AddHandler

      AddHandler Me.Click, AddressOf Foo
      
        2
  •  1
  •   dahlbyk    16 年前

    你能创建一个新的类来接受 Form 在构造函数中,并且具有 chkSelectPanel 作为一个字段,允许您将实例方法用作事件处理程序吗?