Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I am stuck with my project a little bit and I need a little help. I am using Visual Studio 2017, for C#.

Basically my problem is that I cannot access an array in Form2 that is produced in the Form1. But the problem is a bit funny. I generate a matrix in Form1 that will be used as a bitmap in Form2. So I generate some matrix in Form1 based on some experimental data, and then I can pass this array and the number of its cols and rows to Form2 and then I can show it there as a bitmap picture.

I also have a small piece of code that shows the X and Y coordinates of the image if I click on it. This is very important for me, because based on these coordinates, I can look up values in different other arrays. I have one array that is making up the picture as a bitmap, but I assign many other data for each pixel and I want to store them in different arrays.

What is the easiest way to make an array and its values visible in Form2? I don't want to edit the arrays from Form2, I just want to be able to read them. So when I click on the picture, I can look up the values corresponding for the coordinates.

Thank you for the help and advice!

What I have tried:

In Form1, this part of the code works:
C#
Form2 newForm2 = new Form2(); //initialize a new form = Form2
               newForm2.ShowSLImg(ROWS, COLUMNS, array2D); //pass the values to the Form2's function
                newForm2.Show(this); //show the form2.

Then in Form2, I use a function:
C#
public void ShowSLImg(int _row, int _column, double[,] aarray2D)
        {...dosomething...}


So I am able to pass the array2D and ROWS, COLUMNS to the Form2 and I can work with it and based on the array2D values, I am able to show a bitmap image in the Form2.

In this part of the code, I can fetch the coordinates of the mouse position and also I can do something based on the color. But I would like to extend this function and be able to look up different array based on the x,y coordinates.

C#
public void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
           
            Color color = image2.GetPixel(e.X, e.Y); //Get the pixel color as RGB

            double Red = color.R; //Get only the RED color

            label1.Text = (Red / 255).ToString(); 

            int _x = Convert.ToInt32(e.X);
            int _y = Convert.ToInt32(e.Y);

            TextBox.Text += "X: "+_x.ToString() + " Y: " + _y.ToString() +Environment.NewLine;
 
        }
Posted
Updated 18-Feb-18 6:34am

1 solution

So you use the array2D parameter to build your bitmap, but you want to access it leter in
your Form2 code?
That's simple: just declare a private variable and have it reference the array parameter:
C#
private int rows = 0;
private int columns = 0;
private double[,] array2D = null;
public void ShowSLImg(int _row, int _column, double[,] aarray2D)
   {
   rows = _row;
   columns = _column;
   this.array2D = aarray2D;
   ...dosomething...
   }
Then you can access the raw data from any part of your Form2 code.
 
Share this answer
 
Comments
Bice90 18-Feb-18 13:02pm    
Dear OriginalGriff,

Thank you for your quick answer! I think my description was poor a little bit.

So I have this array2D that contains the pixels of the bitmap. I use it to show the picture only.

Then I would like to have several different arrays in the Form1, with the same dimensions but with different data. So when I click on the image, created from the array2D, I can see the coordinates, and therefore I can look up for the corresponding data in the other array, based on the coordinates.

Basically, I am using the array2D to show an intelligible image then I want to print some numbers in a textbox in Form2 when I click on a certain part of the image.

Something like: clicking on the image in Form2 -> getting x,y -> look up the corresponding x,y in some other array in Form1 -> print out the value of somerandomarray[x,y] to a textbox in Form2.

The only thing I cannot get that how to make an array in Form1 visible for the whole Form2?

Thank you!
OriginalGriff 19-Feb-18 4:14am    
That's not a good solution - it "locks" the two forms together and that's very bad from an OOPs point of view.
There are two other approaches:
1) Create an event in Form2 which Form1 handles - this says "I've been clicked". Form1 then fetches the click location from a Form2 property, processes it, and passes the info back to form2 either via a property or a method. This is the way most of .NET works.
2) Create a delegate method in Form1 which is called from Form2 (by passing the delegate in the constructor, say) and which accepts the click data, and returns the processed data. This is a bit more messy, but is probably slightly quicker to execute.

Both sound complex, but they aren't, not really.
See here:
https://www.codeproject.com/Tips/548131/Transferring-information-between-two-forms-Part
It shows how to do the first method.
Bice90 19-Feb-18 7:11am    
Thank you very much! I really appreciate that you went into the details.
OriginalGriff 19-Feb-18 14:03pm    
You're welcome!

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