简体   繁体   English

以下 VB.NET 代码的 C# 等效项是什么?

[英]What's the C# equivalent of the following VB.NET code?

I have this code written in VB.NET that I need to convert to C# but I'm running in some problems.我有这段用 VB.NET 编写的代码,我需要将其转换为 C#,但我遇到了一些问题。

This is the code in VB:这是VB中的代码:

For Each c in Me.Controls
    If TypeOf (c) Is PictureBox Then
        CType(c,PictureBox).Image = icon;
        AddHandler c.Click, AddressOf PictureBox10_Click
    End If

So basically I'm trying to make it check for PictureBoxes and give them an icon and to make those PictureBoxes have the same function that PictureBox10 Click event has.所以基本上我试图让它检查PictureBoxes并给它们一个图标并使这些PictureBoxes具有与PictureBox10 Click事件相同的功能。

Here is the code I've written in C#:这是我用 C# 编写的代码:

foreach (Control c in this.Controls)
{
    if (c.GetType() == typeof(System.Windows.Forms.PictureBox))
    {
        ((PictureBox)c).Image = Properties.Resorces.available;
        c.Click += new System.EventHandler(this.pictureBox10_Click);
    }
}

It works till the Image part but I can't get the EventHandler working.它一直工作到图像部分,但我无法让EventHandler工作。

Also here is what the PictureBox10 Click Event does:这也是 PictureBox10 单击事件的作用:

private void pictureBox10_Click(object sender, EventArgs e) 
{
    if (pictureBox10.Image == Properties.Resorces.available)
        pictureBox10.Image = Properties.Resorces.selected;
    else if (pictureBox10.Image == Properties.Resorces.selected)
        pictureBox10.Image = Properties.Resorces.available;
}

Any help is very much appreciated.很感谢任何形式的帮助。

The code to select a picturebox can be simplified using the System.Linq extension method, OfType , which selects only the controls specified in the OfType argument, and returns them as that type.以选择一个PictureBox的代码可以使用被简化System.Linq扩展方法, OfType ,它选择在指定唯一的控件OfType参数,并返回它们作为该类型。 Also, we can assign a common event hanlder to all these controls:此外,我们可以为所有这些控件分配一个公共事件处理程序:

foreach (PictureBox pb in Controls.OfType<PictureBox>())
{
    pb.Image = Properties.Resorces.available;
    pb.Click += PictureBox_Click;  // Defined below
}

Then in the event handler, we cast sender to a PictureBox so that we have a strongly-typed object, which allows us to then set the Image property:然后在事件处理程序中,我们将sender PictureBox转换为PictureBox这样我们就有了一个强类型对象,这允许我们设置Image属性:

private void PictureBox_Click(object sender, EventArgs e)
{
    var thisPictureBox = sender as PictureBox;

    // May not be necessary, but it's a good practice to ensure that 'sender' was actually
    // a PictureBox and not some other object by checking if 'thisPictureBox' is 'null'
    if (thisPictureBox == null) return;

    if (thisPictureBox.Image == Properties.Resorces.available)
    {
        thisPictureBox.Image = Properties.Resorces.selected;
    {
    else if (thisPictureBox.Image == Properties.Resorces.selected)
    {
        thisPictureBox.Image = Properties.Resorces.available;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM