无法以新的形式访问数组,C# [英] Cannot access array in a new form, C#

查看:88
本文介绍了无法以新的形式访问数组,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我对我的项目感到困惑,我需要一些帮助。我正在使用Visual Studio 2017,用于C#。



基本上我的问题是我无法访问Form1中生成的Form2中的数组。但问题有点好笑。我在Form1中生成一个矩阵,用作Form2中的位图。所以我基于一些实验数据在Form1中生成了一些矩阵,然后我可以将这个数组及其cols和行的数量传递给Form2,然后我可以将它显示为位图图片。



我还有一小段代码,如果我点击它,它会显示图像的X和Y坐标。这对我来说非常重要,因为基于这些坐标,我可以在不同的其他数组中查找值。我有一个数组作为位图组成图片,但我为每个像素分配了许多其他数据,我想将它们存储在不同的数组中。



什么是在Form2中显示数组及其值的最简单方法吗?我不想从Form2编辑数组,我只是想能够读取它们。所以当我点击图片时,我可以查找对应坐标的值。



感谢您的帮助和建议!



我尝试了什么:



在Form1中,这部分代码有效:

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:

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.



然后在Form2中,我使用了一个函数:


Then in Form2, I use a function:

public void ShowSLImg(int _row, int _column, double[,] aarray2D)
        {...dosomething...}





所以我能够将array2D和ROWS,COLUMNS传递给Form2,我可以使用它并基于array2D值,我能够在Form2中显示位图图像。



在这部分代码中,我可以获取鼠标位置的坐标,也可以根据颜色做一些事情。但我想扩展这个功能,并能够根据x,y坐标查找不同的数组。





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.

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;
 
        }

推荐答案

所以你使用array2D参数来构建你的位图,但你想要访问它

你的Form2代码?

这很简单:只需声明一个私有变量并让它引用数组参数:

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:
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...
   }

然后,您可以从Form2代码的任何部分访问原始数据。

Then you can access the raw data from any part of your Form2 code.


这篇关于无法以新的形式访问数组,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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