Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
how to pass variable value in event handler
Posted on: 15 May 2013
Hi

I am beginner in C#.
In for loop , I am creating panel control with Image inside (calling 10 images),When I click on image , it should display value of that Image (Image number) using pop up message box. Please suggest code for the same

public Form1()
        {
            InitializeComponent();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

            for (int count = 1; count < 11; count++)
            {
                 this.panel1 = new System.Windows.Forms.Panel();
               
                // panel1
                this.panel1.BackgroundImage = Image.FromFile(Application.StartupPath + "\\activecell.jpg");
                this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
                this.panel1.Location = new System.Drawing.Point(50, 50);
                this.panel1.Name = "panel1" + count;
                this.panel1.Size = new System.Drawing.Size(70, 35);
                this.panel1.Click += new System.EventHandler(this.panel1_Click);
                                
                this.flowLayoutPanel1.Controls.Add(this.panel1);
                              
            }

        }
 private void panel1_Click(object sender, EventArgs e)
        {
          
        }

as I can not pass count variable to panel1_Click variable and i want to display count value on message box.

please reply

regards
Viva

AuthorReply
Vulpes
  • 0
  • 0
accepted
Re: how to pass variable value in event handler
Posted on: 16 May 2013   Accepted Answer
If you want to pass any variable at all to the handler then you can set the panel's Tag property which is of type object and so an accept a value of any type:

        public Form1()
        {
            InitializeComponent();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

            for (int count = 1; count < 11; count++)
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.panel1.Name = "Image" + count.ToString();
                this.panel1.Tag = "whatever";
                // panel1
                this.panel1.BackgroundImage = Image.FromFile(Application.StartupPath + "\\activecell.jpg");
                this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
                this.panel1.Location = new System.Drawing.Point(50, 50);
                this.panel1.Name = "panel1" + count;
                this.panel1.Size = new System.Drawing.Size(70, 35);
                this.panel1.Click += new System.EventHandler(this.panel1_Click);
                                 
                this.flowLayoutPanel1.Controls.Add(this.panel1);
                               
            }

        }

        private void panel1_Click(object sender, EventArgs e)
        {
           Panel p = (Panel)sender;
           string number = p.Name.Substring(5);
           string tag  = p.Tag.ToString();
           MessageBox.Show("Image " + number + " clicked with tag " + tag);
        }

Re: how to pass variable value in event handler
Posted on: 15 May 2013  
Try this:

        public Form1()
        {
            InitializeComponent();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

            for (int count = 1; count < 11; count++)
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.panel1.Name = "Image" + count.ToString();
                // panel1
                this.panel1.BackgroundImage = Image.FromFile(Application.StartupPath + "\\activecell.jpg");
                this.panel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
                this.panel1.Location = new System.Drawing.Point(50, 50);
                this.panel1.Name = "panel1" + count;
                this.panel1.Size = new System.Drawing.Size(70, 35);
                this.panel1.Click += new System.EventHandler(this.panel1_Click);
                                 
                this.flowLayoutPanel1.Controls.Add(this.panel1);
                               
            }

        }

        private void panel1_Click(object sender, EventArgs e)
        {
           Panel p = (Panel)sender;
           string number = p.Name.Substring(5);
           MessageBox.Show("Image " + number + " clicked");
        }

viva
  • 0
  • 0
Re: how to pass variable value in event handler
Posted on: 16 May 2013  
Hi Thanks for the reply. It is working.

But I want to pass any variable value declare in public Form1() to that event.How to do that?

suppose there is other for loop inside running and want to pass that value.how to pass integer??


Viva

viva
  • 0
  • 0
Re: how to pass variable value in event handler
Posted on: 16 May 2013  
by doing this i am getting "count" as string.But i want the value of that count??

viva
  • 0
  • 0
Re: how to pass variable value in event handler
Posted on: 16 May 2013  
ok I got it by doing

this.panel1.Tag = count;

thank u very much

viva
  • 0
  • 0
Re: how to pass variable value in event handler
Posted on: 16 May 2013  
but now in the same event, I want to compare to display different messages.

 Panel p = (Panel)sender;
            string number = p.Name.Substring(5);
            string tag = p.Tag.ToString();
            if (tag<10)
            MessageBox.Show("Image " + number + " is active");
            else

                MessageBox.Show("Image " + number + " is disable");


but for red mark code I am getting error as


operator can not be applied to operands of type string to int

Re: how to pass variable value in event handler
Posted on: 16 May 2013  
If you know that the Tag property will contain an integer, then you can cast it to one before doing any arithmetic or arithmetic comparisons:

Panel p = (Panel)sender;
string number = p.Name.Substring(5);
int tag = (int)p.Tag;
if (tag < 10)
   MessageBox.Show("Image " + number + " is active");
else
   MessageBox.Show("Image " + number + " is disabled");


viva
  • 0
  • 0
Re: how to pass variable value in event handler
Posted on: 16 May 2013  
Thank u very much for your quick reply.

It is working

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.