C# listView 显示图片。双击图片在新的窗体中显示,关闭显示窗体
时间: 2024-02-21 18:58:36 浏览: 75
在C#中,关闭显示图片的窗体可以通过在新窗体中添加关闭按钮实现。具体实现步骤如下:
1. 在新窗体中添加PictureBox控件,并设置其Dock属性为Fill。
```csharp
PictureBox pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.Image = image;
this.Controls.Add(pictureBox);
```
2. 在新窗体中添加关闭按钮,实现关闭窗体的功能。
```csharp
Button closeButton = new Button();
closeButton.Text = "关闭";
closeButton.Dock = DockStyle.Bottom;
closeButton.Click += new EventHandler(closeButton_Click);
this.Controls.Add(closeButton);
```
在按钮的Click事件中,调用窗体的Close方法,关闭窗体。
```csharp
private void closeButton_Click(object sender, EventArgs e)
{
this.Close();
}
```
完整代码如下:
```csharp
private void listView_DoubleClick(object sender, EventArgs e)
{
if (listView.SelectedItems.Count > 0)
{
Image image = listView.SelectedItems[0].ImageList.Images[listView.SelectedItems[0].ImageIndex];
Form form = new Form();
PictureBox pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.Image = image;
form.Controls.Add(pictureBox);
Button closeButton = new Button();
closeButton.Text = "关闭";
closeButton.Dock = DockStyle.Bottom;
closeButton.Click += new EventHandler(closeButton_Click);
form.Controls.Add(closeButton);
form.ShowDialog();
}
}
private void closeButton_Click(object sender, EventArgs e)
{
this.Close();
}
```
这样,在C#中ListView控件中双击图片,在新的窗体中显示图片,并且可以通过点击关闭按钮关闭显示图片的窗体。
阅读全文