转换C#语句体的lambda到VB [英] Convert C# statement body lambda to VB
问题描述
看来,VB中VS8不支持/转换lambda表达式与语句体。我使用他们在我的C#应用程序,但现在必须将其转换为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.
我动态创建了一大堆的管制,我希望能够给他们在飞行中的事件处理程序。这是这样我就可以建立从数据库中动态用户界面。在下面的代码我将创建一个表单和一个复选框,使CheckBox控件形式的知名度,增加一些方法来处理的形式,然后将新创建的复选框添加到一个预先存在的形式/面板/等。表单处理程序,例如,影响复选框:
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
$ B的$ b
当然,我得到了转换错误,VB不支持的语句体匿名方法/ lambda表达式
Of course, I'm getting the conversion error, "VB does not support anonymous methods/lambda expressions with a statement body"
我真的很喜欢我并不需要补充的是找出哪些形式给予密切事件的任何特殊功能,因此它可以寻找正确的复选框,并更新复选框 - - 它以动态创建的一切,然后让对象自行处理能力刚工作TM。
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).
不幸的是,它需要被转换到VB。
Unfortunately it needs to be converted to VB.
- 什么是波长/匿名发言机构转变成的东西,将在VB的工作,特别是当一个人需要创造出许多他们的最好方法是什么?
推荐答案
等待.NET 4最近的版本中,将支持VB这样的事情。不要看其他的替代
Wait for the nearest release of .NET 4, it will support things like this in VB. Don't see other alternative.
丑陋的替代品是:
-
这个工作,但你可以在一个函数中使用一个单独的语句。
This work, but you can use a single statement in a function.
AddHandler Me.Click, Function(o, e) MessageBox.Show("text")
创建一些正规子美孚
Create some regular Sub Foo
Public Sub Foo(ByVal o As Object, ByVal e As EventArgs)
MessageBox.Show("text")
End Sub
和使用的AddHandler
将其绑定到事件
AddHandler Me.Click, AddressOf Foo
这篇关于转换C#语句体的lambda到VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!