Skip to main content
 首页 » 编程设计

c#之使用方法切换 BorderStyle。 C#

2024年02月24日1352php

有谁知道我怎么写“cardImage1.BorderStyle = BorderStyle.Fixed3D;”无需明确声明“cardImage1”?

我试图将它放入一个方法中,这样我就不需要编写代码来在单击图片框时在两个边框样式之间切换,对于每个单独的图片框(有 52 个!)

例如对于当前的每个框,我需要在其 _click 事件中包含以下内容。

        if (cardImage1.BorderStyle == BorderStyle.None) 
        { 
            cardImage1.BorderStyle = BorderStyle.Fixed3D; 
        } 
        else 
        { 
            cardImage1.BorderStyle = BorderStyle.None; 
        } 

请您参考如下方法:

假设这是在事件处理程序中,您应该将 sender 参数转换为 PictureBox 并直接对其进行操作。

private void pictureBox_Click(object sender, EventArgs args) 
{ 
    var image = (PictureBox)sender; 
    if (image.BorderStyle == BorderStyle.None) 
    { 
        image.BorderStyle = BorderStyle.Fixed3D; 
    } 
    else 
    { 
        image.BorderStyle = BorderStyle.None; 
    } 
} 

然后您将在 PictureBox 的所有实例上使用相同的事件处理程序。