C#.net中的动态图片框 [英] Dynamic Picture Box in C#.net

查看:151
本文介绍了C#.net中的动态图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要你的帮助.我创建了一个图片框,动态地创建了标签.单击图片框时.标签文本未更改.请告诉我如何在运行时更改标签文本和图片框图片.非常紧急
我给我代码...
简单:
动态属性:GroupBox,图片框,标签
当我按下"PicMImportopen"时,我想更改标签:在线,以显示PicMOnline

Hi,
I need your Help. I created a Picture Box ,Label in Dynamically.when i click Picture Box .Label text didn''t change.Please tell me how to change the Label text and Picture Box Picture at run time. very Urgent
I give my code...
Simple:
Dynamic Properties : GroupBox,Picture Box,Label
When I press a "PicMImportopen" I want to change the Label :Online ,To Display PicMOnline

void m_pictureBox_Click(object sender, EventArgs e)
        {
             PictureBox PicMImportClose = (PictureBox)sender;
             if (PicMImportClose.Name == "PicMImportopen")
            {
                 PicMImportClose.Visible = true;
                PicMImportClose.BringToFront();
                PicMImportClose.Image = new System.Drawing.Bitmap(Getpath + "\\picImportClose.Image.png");
                PicMImportClose.Width = 110;
                PicMImportClose.Height = 100;
                PicMImportClose.Left = 5;
                PicMImportClose.Top = 20;
               PicMImportClose.Name = "CLOSE";
             }
            else
            {
                 PicMImportClose.Visible = true;
                PicMImportClose.BringToFront();
                PicMImportClose.Image = new System.Drawing.Bitmap(Getpath + "\\picImportOpen.Image.png");
                PicMImportClose.Width = 110;
                PicMImportClose.Height = 100;
                PicMImportClose.Left = 5;
                PicMImportClose.Top = 20;
                PicMImportClose.Name = "PicMImportopen";
            }



          }


感谢进阶
问候,
Lakshmi Narayanan.S

[edit]已添加代码块,忽略HTML ..."选项已禁用-OriginalGriff [/edit]


Thanks in Advanced
Regards,
Lakshmi Narayanan.S

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]

推荐答案

我不知道您的代码有什么问题.从问题中很难理解您的确切问题.但是在您的代码中,我看不到任何用于更改标签文本的代码.一个小提示:在方法期间更改PictureBox的名称",然后使用此名称标识图片框.我建议您使用图片框的标记"属性来存储标志"(真/假),以了解应显示的图片.
无论如何,在运行时更改属性都可以.您的代码似乎可以正常工作.只需添加代码即可更改标签文本.
如何处理这种情况的示例:在新的Forms项目中复制/粘贴并将Program.cs替换为以下内容:

I don''t know whats wrong with your code. Difficult to understand your exact problem from your question. But in your code I don''t see any code to change the label text. One little tip: You change the PictureBox''s "Name" during the method, after that you use this name to identify the picturebox. I''d recommend you use the "Tag" property of the picturebox instead to store your "flag" (true/false) to know which picture should be shown.
Anyway changing the properties at runtime should work. Your code seems to work. Just add code to change the label text too.
An example how to handle this situation: Copy/Paste in a new Forms project and replace Program.cs with the following:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace PictureBoxWithLabel
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            // Create an array of sample image paths:
            // For example the pictures located in your systems Common Pictures folder.
            // If you don''t have any pictures there, change to any other directory containing image files.
            string strFolderWithPictures = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
            string[] astrImages = Directory.GetFiles(strFolderWithPictures, "*.jpg", SearchOption.AllDirectories);

            int iShownPicture = 0; 

            // Create a test form with 
            Form form = new Form();
            // a Label for some text
            Label label = new Label();
            label.Dock = DockStyle.Bottom;
            label.Text = "Click on the PictureBox!";
            // ... and a PictureBox for viewing an image
            PictureBox picturebox = new PictureBox();
            picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
            picturebox.BackColor = Color.Black;
            picturebox.Dock = DockStyle.Fill;
            // On PictureBox click (or any other event) we re-assign the image for the PictureBox and 
            // the text for the Label - or in other words: change them at runtime.
            picturebox.Click += delegate(object sender, EventArgs ea)
            {
                if (astrImages.Length > 0) // Any images in the sample image directory?
                {
                    PictureBox pbSender = sender as PictureBox;
                    pbSender.Image = new Bitmap(astrImages[iShownPicture]); // change the image
                    label.Text = astrImages[iShownPicture]; // change the text
                    // increment, or reset, shown picture index for next call of this handler
                    iShownPicture = astrImages.Length >= iShownPicture + 1 ? ++iShownPicture : 0;
                }
            };

            form.Controls.Add(label);
            form.Controls.Add(picturebox);

            Application.Run(form);
        }
    }
}


尝试用PictureBox实现任何动态行为都是有可能的,并且……完全滥用了此组件,该组件是为最简单的近100%静态情况设计的.与根本不使用它相比,当它提供零帮助且有很多麻烦时,您将进入该区域.

这是您必须执行的操作:
如何从旧图纸中清除面板 [ ^ ].从上面引用的我过去的解决方案中,您可以理解.如果不清楚,请问您后续问题并解释您要实现的目标.

我确定您根本不需要PictureBox .

—SA
Trying to implement any dynamic behavior with PictureBox is quite possible and is… a pure abuse of this component designed for the simplest nearly 100% static cases. You will go to the area when it provides zero help with a lot of hassles compared to… not using it at all.

Here is what you have to do:
How do I clear a panel from old drawing[^]. From my past solution referenced above you can get the idea. If this is not clear, ask you follow-up question and explain what you want to achieve.

I''m sure you don''t need PictureBox at all.

—SA


这篇关于C#.net中的动态图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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