【问题标题】:Can't use array in Windows Forms [duplicate]无法在 Windows 窗体中使用数组 [重复]
【发布时间】:2018-12-23 12:30:01
【问题描述】:

我正在尝试初始化一个数组,将其设置为保存其他变量,然后在用户按下我的表单上的按钮时使用它。当我尝试在 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)
        {

        }
    }

}

对不起,如果这是一个愚蠢的问题,我真的需要回答它。

【问题讨论】:

  • 在“if(activity==activities[i])”和“activity+=activities[i]; "

标签: c# winforms


【解决方案1】:

你应该将 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();
    }
}

【讨论】:

  • 字符串[] 活动=新字符串[5];
  • 长度意味着分配你想要的长度
  • 这不是一个“全局变量”,它是一个实例变量。 Form1 类的每个实例都将获得其中一个。所有实例方法都可以访问它。
猜你喜欢
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2014-11-07
  • 2011-02-26
  • 1970-01-01
  • 2013-10-04
相关资源
最近更新 更多