通过按钮单击在动态创建的图片框中显示图片 [英] show picture at dynamically created picture box by button click

查看:81
本文介绍了通过按钮单击在动态创建的图片框中显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经动态创建了一个标签控件,我在其中添加了一个标签页,在标签页中我添加了一个图片框



如何才能显示图片这个图片框

点击按钮



我得到的错误是



当前情境中不存在名称'picturebox1'



i知道将图片显示到图片框但是我在这里如何设置图像为动态创建的图片框

I have dynamically created a tab control in which i add a tab page and in the tab page i add a picture box

how can i show a image on this picture box
at Click of a button

the error i get is

The name 'picturebox1' does not exist in the current context

i know to show picture on to picturebox but here i m stucked at how can i set image for a dynamically created picture box

推荐答案

您必须先为您的表单成员:

You have to make for your form a member first:
private PictureBox picturebox1;





在表单构造函数中,您可以设置picturebox1对象的属性。在按钮单击事件之后,您可以毫无问题地使用PictureBox对象。



顺便说一句,您可以使用foreach迭代控件。也许你可以找到你动态创建的控件如下:



In the form constructor you can set the properties of the picturebox1 object. After this in the button click event you can use your PictureBox object without problem.

By the way you can iterate through the controls with foreach. Maybe you can find your dynamically created control as the follows:

foreach (var i in this.Controls)
{
    if (i.GetType() == typeof(PictureBox))
    {
        Image p = i as Image;

        p.Image = //your code
    }
}





我希望我能帮助你!



I hope I could help you!


这是设计师

命名空间WindowsFormsApplication6

{

partial class Form1

{

private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)

{

if(disposing&&(components!= null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows窗体设计器生成的代码



private void InitializeComponent()

{

this.buttoncreatepicturebox = new System.Windows.Forms.Button();

this.buttonshowimage = new System.Windows.Forms.Button();

this.SuspendLayout();



// buttoncreatepicturebox



// buttonshowimage



//表格1



}

#endregion

私人系统em.Windows.Forms.Button buttoncreatepicturebox;

private System.Windows.Forms.Button buttonshowimage;

private System.Windows.Forms.PictureBox picturebox1; //我手动写了这个

//我们必须首先让我们的表格成员

}

}

$ b





使用系统;

使用System.Collections .Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Windows.Forms;



名称空间WindowsFormsApplication6

{

公共部分类Form1:表格

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender,EventArgs e)

{

picturebox1 = new PictureBox();



Controls.Add(picturebox1);



picturebox1.Name =" pictur ebox1" ;;

picturebox1.Location = new Point(0,0);

picturebox1.Size = new Size(100,100);

picturebox1.SizeMode = PictureBoxSizeMode.CenterImage;

picturebox1.BackColor = Color.AliceBlue;

}

private void button2_Click(object sender,EventArgs e)

{picturebox1.Image = indowsFormsApplication6.Properties.Resources.monotone_close_exit_delete;

}

}

}

希望这有助于像我这样的新手!
this is the designer
namespace WindowsFormsApplication6
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code

private void InitializeComponent()
{
this.buttoncreatepicturebox = new System.Windows.Forms.Button();
this.buttonshowimage = new System.Windows.Forms.Button();
this.SuspendLayout();

// buttoncreatepicturebox

// buttonshowimage

// Form1

}
#endregion
private System.Windows.Forms.Button buttoncreatepicturebox;
private System.Windows.Forms.Button buttonshowimage;
private System.Windows.Forms.PictureBox picturebox1; // i wrote this manually
//We have to make for our form a member first
}
}


and

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
picturebox1 = new PictureBox();

Controls.Add(picturebox1);

picturebox1.Name = "picturebox1";
picturebox1.Location = new Point(0, 0);
picturebox1.Size = new Size(100, 100);
picturebox1.SizeMode = PictureBoxSizeMode.CenterImage;
picturebox1.BackColor = Color.AliceBlue;
}
private void button2_Click(object sender, EventArgs e)
{ picturebox1.Image=indowsFormsApplication6.Properties.Resources.monotone_close_exit_delete;
}
}
}
hope this helps some newbie like me!


这篇关于通过按钮单击在动态创建的图片框中显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆