Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an GUI functioning code, in which I have two methods (for button1 and button2).
I want to access 'h' and 'w' variable values from button1 method to button2 method.
C#

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace First
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select an Image";
            dlg.Filter = "jpg files (*.jpg)|*.jpg";
            if (DialogResult.OK == dlg.ShowDialog())
            {
                this.pictureBox1.Image = new Bitmap(dlg.FileName);
                // pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                Bitmap img = new Bitmap(dlg.FileName);
                int w = img.Width;
                int h = img.Height;
                pictureBox1.Height = h;
                pictureBox1.Width = w;
                textBox1.Text = dlg.FileName;

                            }
         }

        private void button2_Click(object sender, EventArgs e)
        {
        MessageBox.Show("Height is- "+   h.ToString() +"      
        Width is- " +  w.ToString(),"Height & Width");


        }
    }
}


That is nothing but globalizing the variables. But I am unable to do that.Please help me in this context.
Thank you in advance.

What I have tried:

I have tried by declaring variables in starting of program in 'public int h,w;' way.
Posted
Updated 20-Aug-16 1:22am

1 solution

Yes you can, using this.pictureBox1 object, since you are assigning the values of h and w to the picture box in the button1 click event.

C#
private void button2_Click(object sender, EventArgs e)
       {
           int h = pictureBox1.Height;
           int w = pictureBox1.Width;
           MessageBox.Show("Height is- " + h + " Width is- " + w, "Height & Width");
       }
 
Share this answer
 
v2
Comments
Ralf Meier 21-Aug-16 6:27am    
Or perhaps better like this :
You first check if an image is assigned to PictureBox1 and then read the PictureBox1.Image.Width and PictureBox1.Image.Height
Karthik_Mahalingam 21-Aug-16 6:37am    
Yes, right
I hope the op will take care of it
vaay.jain 21-Aug-16 13:13pm    
But How to do that in general case ?
Karthik_Mahalingam 21-Aug-16 13:19pm    
Means?
vaay.jain 22-Aug-16 5:38am    
@Karthik .. I can use picturebox here for reference, that's okay but if I want to access some other variables like- selecting,reading and showing image using first button(method) and getting pixel information of image using second button(method).
It will definitely need to access some variables from one method to other.

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