Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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";
            }



          }

Thanks in Advanced
Regards,
Lakshmi Narayanan.S

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 18-Jul-11 2:49am
v2

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);
        }
    }
}
 
Share this answer
 
v2
Comments
naraayanan 19-Jul-11 0:44am    
Thanks for u reply.Please see my task
I am looking for Dynamic Properties in C#.net in Windows application.One flowLayoutpanel in the form.Dynamic properties are Group box ,Picture Boxes and Labels In that flowLayoutPanel.My question is when i click the One Picture Box ,Label text should change and another picture box picture should change .please help me How to do this task ?.
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
 
Share this answer
 
Comments
naraayanan 19-Jul-11 0:43am    
Thanks for u reply.Please see my Task.
I am looking for Dynamic Properties in C#.net in Windows application.One flowLayoutpanel in the form.Dynamic properties are Group box ,Picture Boxes and Labels In that flowLayoutPanel.My question is when i click the One Picture Box ,Label text should change and another picture box picture should change .please help me How to do this task ?.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900