只有最后动态创建的图片框才可访问 [英] only last dynamically created picturebox is accessible

查看:64
本文介绍了只有最后动态创建的图片框才可访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 WinForm 应用程序,其中我有 TabControl1 并且在运行时创建 TabPage PictureBox



按钮点击,我制作新标签页和 PictureBox 并将它们添加到 TabControl1



现在,我可以更改最后创建的 PictureBox 的图像。

当我尝试更改任何<$ c的图像时$ c> PictureBox ,除了最后一个,我无法做到这一点



这是代码示例。



button1 创建新的 TabPage PictureBox

  private   void  button1_Click( object  sender,EventArgs e)
{
TabPage tpgallery = new TabPage();
tpgallery.Name = tpgallery;
tpgallery.Text = 图库;

tabControl1.TabPages.Add(tpgallery);

picturebox1 = new PictureBox();

picturebox1.Name = picturebox1name;
picturebox1.Image = WindowsFormsApplication7.Properties.Resources.logo1;

tpgallery.Controls.Add(picturebox1);
}





button2 更改 PictureBox 。

  private   void  button2_Click( object  sender,EventArgs e)
{
picturebox1.Image = WindowsFormsApplication7.Properties.Resources.logo2;
}









更正了格式和语法问题。

[/ edit]

解决方案

pictureBox1 保存对最后创建的引用,即问题。

 私人  void  button2_Click(  object  sender,EventArgs e)
{
PictureBox pb = tabControl1.SelectedTab.Controls.OfType< picturebox>()。FirstOrDefault(p = < span class =code-keyword>> p.Name == picturebox1name);

if (pb!= null
pb.Image = WindowsFormsApplication7.Properties.Resources.logo2;
}



在此处找到 - 只有最后动态创建的图片框才可访问 [ ^ ]







添加链接文字以反映文章标题。

更正格式和语法问题。

[/编辑]


I am creating a WinForm application, in which I have a TabControl1 and at run time created TabPage and PictureBox.

On a Button Click, I make new tab page and PictureBox and add them to the TabControl1.

Now, I can change image of only last created PictureBox.
and when I try to change the image of any PictureBox, except the last one, I am not able to do that

This is the code example.

button1 creates new TabPage and PictureBox.

private void button1_Click(object sender, EventArgs e)
{
    TabPage tpgallery = new TabPage();           
    tpgallery.Name = "tpgallery";
    tpgallery.Text = "  Gallery  ";            
    
    tabControl1.TabPages.Add(tpgallery);           
    
    picturebox1 = new PictureBox();           
    
    picturebox1.Name = "picturebox1name";           
    picturebox1.Image = WindowsFormsApplication7.Properties.Resources.logo1;
    
    tpgallery.Controls.Add(picturebox1);          
}



button2 changes image of PictureBox.

private void button2_Click(object sender, EventArgs e)
{
    picturebox1.Image = WindowsFormsApplication7.Properties.Resources.logo2;
}




[Edit member="Tadit"]
Corrected formatting and grammatical issues.
[/Edit]

解决方案

pictureBox1 holds the reference to the last created one, that's the problem.

private void button2_Click(object sender, EventArgs e)
{
    PictureBox pb = tabControl1.SelectedTab.Controls.OfType<picturebox>().FirstOrDefault(p => p.Name == "picturebox1name");

    if (pb != null)
        pb.Image = WindowsFormsApplication7.Properties.Resources.logo2; 
}


Found Here - only last dynamically created picturebox is accessible[^]


[Edit member="Tadit"]
Link text added to reflect the article title.
Corrected formatting and grammatical issues.
[/Edit]


这篇关于只有最后动态创建的图片框才可访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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