Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用方法切换边框样式。C#_C#_.net_Methods_Properties - Fatal编程技术网

使用方法切换边框样式。C#

使用方法切换边框样式。C#,c#,.net,methods,properties,C#,.net,Methods,Properties,有人知道我如何编写“cardImage1.BorderStyle=BorderStyle.Fixed3D;”而不必显式声明“cardImage1” 我正在尝试将其放入一个方法中,这样我就不需要编写代码,在单击图片框时,为每个图片框(有52个)在两种边框样式之间切换 e、 g.对于当前的每个框,我需要在其点击事件中包含以下内容 if (cardImage1.BorderStyle == BorderStyle.None) { cardImag

有人知道我如何编写“cardImage1.BorderStyle=BorderStyle.Fixed3D;”而不必显式声明“cardImage1”

我正在尝试将其放入一个方法中,这样我就不需要编写代码,在单击图片框时,为每个图片框(有52个)在两种边框样式之间切换

e、 g.对于当前的每个框,我需要在其点击事件中包含以下内容

        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

的所有实例上使用相同的事件处理程序。假设这是在事件处理程序中,您应该将
发送方
参数强制转换为
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;
    }
}
class MyPictureBox : PictureBox
{
    protected void this_Click(object sender, EventArgs e)
    {
        if (this.BorderStyle == BorderStyle.None)
        {
            this.BorderStyle = BorderStyle.Fixed3D;
        }
        else
        {
            this.BorderStyle = BorderStyle.None;
        }
    }
}

然后,您可以在
PictureBox的所有实例上使用相同的事件处理程序,而不是为每个图片框创建一个处理程序,您可以编写一个方法来处理所有图片框的点击:

 protected void onClickHandler(object sender, EventArgs e)
 {
    if (((PictureBox)sender).BorderStyle == BorderStyle.None)
    {
        ((PictureBox)sender).BorderStyle = BorderStyle.Fixed3D;
    }
    else
    {
        ((PictureBox)sender).BorderStyle = BorderStyle.None;
    }

}
class MyPictureBox : PictureBox
{
    protected void this_Click(object sender, EventArgs e)
    {
        if (this.BorderStyle == BorderStyle.None)
        {
            this.BorderStyle = BorderStyle.Fixed3D;
        }
        else
        {
            this.BorderStyle = BorderStyle.None;
        }
    }
}
您还可以编写一个循环来遍历表单上的所有控件,并将事件处理程序附加到所有图片框中(如果您的情况可能的话)


当然,如果表单上有其他图片框,解决方案是将您感兴趣的52个图片框放在一个面板中,然后不迭代表单中的所有控件(this.controls),只迭代面板中的控件(thePanelControl.controls)

,而不是为每个图片框创建一个处理程序,您可以编写一个方法来处理所有图片框的点击:

 protected void onClickHandler(object sender, EventArgs e)
 {
    if (((PictureBox)sender).BorderStyle == BorderStyle.None)
    {
        ((PictureBox)sender).BorderStyle = BorderStyle.Fixed3D;
    }
    else
    {
        ((PictureBox)sender).BorderStyle = BorderStyle.None;
    }

}
您还可以编写一个循环来遍历表单上的所有控件,并将事件处理程序附加到所有图片框中(如果您的情况可能的话)


当然,如果表单上有其他图片框,解决方案是将您感兴趣的52个图片框放在一个面板中,然后不迭代表单中的所有控件(this.controls),只迭代面板中的控件(panelcontrol.controls)

创建一个新类型
MyPictureBox
并处理单击事件。将所有
PictureBox
替换为
MyPictureBox

class MyPictureBox : PictureBox
{
    protected void this_Click(object sender, EventArgs e)
    {
        if (this.BorderStyle == BorderStyle.None)
        {
            this.BorderStyle = BorderStyle.Fixed3D;
        }
        else
        {
            this.BorderStyle = BorderStyle.None;
        }
    }
}

创建一个新类型
MyPictureBox
并处理单击事件。将所有
PictureBox
替换为
MyPictureBox

class MyPictureBox : PictureBox
{
    protected void this_Click(object sender, EventArgs e)
    {
        if (this.BorderStyle == BorderStyle.None)
        {
            this.BorderStyle = BorderStyle.Fixed3D;
        }
        else
        {
            this.BorderStyle = BorderStyle.None;
        }
    }
}

非常感谢大家,这个特别的问题已经解决了,他背后的想法将在项目中被更多地使用。你不知道我昨晚为此绞尽脑汁有多久了!我很高兴我能帮上忙:)非常感谢大家,这个特殊的问题已经解决了,他背后的想法将在项目中被更多地使用。你不知道我昨晚为此绞尽脑汁有多久了!我很高兴我能帮上忙:)