我正在尝试初始化一个数组,将其设置为保存其他变量,然后在用户按下表单上的按钮时使用它。当我尝试使用 button1_Click 类中的数组时,它无法识别它并说它在当前设置中不存在。namespace WindowsFormsApp1{ public partial class Form1 : Form { string activity; int activityTime; bool enteredInt = false; //initializes variables used to store activities string activity1 = ""; string activity2 = ""; string activity3 = ""; string activity4 = ""; string activity5 = ""; //intitializes variables used to store activity times int activityTime1 = 0; int activityTime2 = 0; int activityTime3 = 0; int activityTime4 = 0; int activityTime5 = 0; int i; public Form1() { InitializeComponent(); string[] activities = { activity1, activity2, activity3, activity4, activity5 }; int[] activityTimes = { activityTime1, activityTime2, activityTime3, activityTime4, activityTime5 }; } private void label1_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { enteredInt = Int32.TryParse(textBox2.Text, out activityTime); if (textBox1.Text != "" && textBox2.Text != "" && enteredInt == true) { activity = textBox1.Text; activityTime = Int32.Parse(textBox2.Text); } else { MessageBox.Show("Please enter valid activity and time."); } for(i=0;i<5;i++) { if (activity==activities[i]) { activity += activities[i]; i = 5; } } } private void textBox1_TextChanged(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } }}对不起,如果这是一个愚蠢的问题,我真的需要回答。
1 回答
FFIVE
TA贡献1797条经验 获得超6个赞
你应该定义 int[]activityTimes 作为一个全局变量,把它放在外面
public partial class Form1 : Form
{
string activity;
int activityTime;
bool enteredInt = false;
//initializes variables used to store activities
string activity1 = "";
string activity2 = "";
string activity3 = "";
string activity4 = "";
string activity5 = "";
//intitializes variables used to store activity times
int activityTime1 = 0;
int activityTime2 = 0;
int activityTime3 = 0;
int activityTime4 = 0;
int activityTime5 = 0;
int i;
string[] activities=new string[length];
public Form1()
{
InitializeComponent();
}
}
- 1 回答
- 0 关注
- 140 浏览
添加回答
举报
0/150
提交
取消