将click方法的(继承)值传递给其他表单方法? [英] Passing (Inheritance) values from click Method to other form methods?
问题描述
你好,
自编写程序以来已经很长时间了,但是我决定要学习C#...
我正在编写一个具有某些字段的Windows窗体应用程序.这些字段用于定义多维数组的大小.表单用户输入字段并单击设置值"按钮后,其余的表单按钮将变为可用.
问题,如果要在setvalues_click方法内部创建数组,如何在方法之间传递数组?
Hello,
It has been a long time since I wrote a program but I decided that I want to learn C#...
I am writting a Windows Form application that has some fields. These fields are used to define the size of an multi-dimensional array. Once the fields are input by the form user and the set value button is clicked, the rest of the forms buttons become available.
Question, how do I pass the array from method to method, if I am creating the array inside of the setvalues_click method?
namespace WFRobotCar
{
private void Setvalue_Click(object sender, EventArgs e)
{
Myrc array_rc = new Myrc(Convert.ToInt32(this.XtextBox.Text), Convert.ToInt32(this.YtextBox.Text));
}
private void printrc_click(object sender, EventArgs e)
{
int i, j;
for (int i = 0; i < array_rc.GetLength(0); i++)
{
Console.Write(" ");
for (int j = 0; j < array_rc.GetLength(1); j++)
{
if ((i == int_current_row) && (j == int_current_col))
Console.ForegroundColor = ConsoleColor.Green;
if (array_rc[i, j])
{
Console.Write('-');
}
else
{
Console.Write('0');
}
if ((i == int_current_row) && (j == int_current_col))
Console.ResetColor();
}
Console.WriteLine();
}
}
}
在此先谢谢您,
Gladys
Thanks in advanced,
Gladys
推荐答案
只需将array
全局化为表单并继续.实际上,我会使用List
而不是数组,因为List
可以按需增长/缩小.
Just make thearray
global to the form and get on with it. In fact, I would use aList
instead of an array because aList
can grow/shrink on demand.
这可以是一种解决方案:
This can be a solution :
namespace WFRobotCar
{
Myrc array_rc;
private void Setvalue_Click(object sender, EventArgs e)
{
array_rc = new Myrc(Convert.ToInt32(this.XtextBox.Text), Convert.ToInt32(this.YtextBox.Text));
}
private void printrc_click(object sender, EventArgs e)
{
int i, j;
for (int i = 0; i < array_rc.GetLength(0); i++)
{
Console.Write(" ");
for (int j = 0; j < array_rc.GetLength(1); j++)
{
if ((i == int_current_row) && (j == int_current_col))
Console.ForegroundColor = ConsoleColor.Green;
if (array_rc[i, j])
{
Console.Write('-');
}
else
{
Console.Write('0');
}
if ((i == int_current_row) && (j == int_current_col))
Console.ResetColor();
}
Console.WriteLine();
}
}
}
private void Setvalue_Click(object sender, EventArgs e)
{
Myrc array_rc = new Myrc(Convert.ToInt32(this.XtextBox.Text), Convert.ToInt32(this.YtextBox.Text));
MyMethod(array_rc)
}
void MyMethod(MyRC arrayIn)
{
// use the array here
}
也许买一本C#书来学习这些基本知识?
Maybe buy a C# book to learn this basic stuff?
这篇关于将click方法的(继承)值传递给其他表单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!