【问题标题】:C# Accessing an array of labels from another form and altering their "Text" propertyC# 从另一个表单访问标签数组并更改其“文本”属性
【发布时间】:2017-01-26 11:57:16
【问题描述】:

这是我的第一篇文章。所以..批评总是受欢迎的。

只要看一下标题,我的问题就很直接了。 如何使用循环通过引用将值插入到不同的标签(get、set 方法采用不同的形式)

我尝试的是使用标签的引用创建一个数组。问题是......它将新值分配给数组,而不是更改将更改标签的引用。

我觉得比这更进一步解释它有点困难。 如果您有任何问题,我会尽我所能回答他们

    private void button1_Click(object sender, EventArgs e)
    {
        int numOfPeriods = Convert.ToInt32(cmbPeriods.Text)-1;
        string initialString = cmbStartTime.Text; //Gets the input from combo box "cmbStartTime".
        string newTime;
        decimal dec = Convert.ToDecimal(TimeSpan.Parse(initialString).TotalHours); //Converts the set by user Lesson start time to a decimal value.
        decimal dec2;
        decimal lessonLength = 1; // Default lesson length is set to 1 hour.

        TimeSpan time;
        Test FormOpen = new Test();

        string[] periodStartTimes = new string[9] //Loop referencing the labels on Form TEST
        {
            FormOpen.startTime,FormOpen.startTime2, FormOpen.startTime3, FormOpen.startTime4,
            FormOpen.startTime5, FormOpen.startTime6, FormOpen.startTime7, FormOpen.startTime8,
            FormOpen.startTime9
        };

        if (cmbLessonLength.Text != "") //If the combo box "cmbLessonLength" is not empty, use that value instead of the default lesson lenght.
        {
            lessonLength = Convert.ToDecimal(cmbLessonLength.Text)/60; //Converts minutes to hours.
        }

        dec2 = dec + lessonLength; 
        time = TimeSpan.FromHours(Double.Parse(dec2.ToString()));
        newTime = time.ToString();

        if (newTime[0] == '0')
        {
            FormOpen.startTime = initialString + " - " + newTime.Remove(5).Remove(0, 1);
        }
        else
        {
            FormOpen.startTime = initialString + " - " + newTime.Remove(5);
        }

        for (int x = 1; x <= numOfPeriods; x++)  //LOOP THAT ASSIGNS VALUES TO THE ARRAY AND NOT THE REFERENCE INSIDE IT
        {
            decimal workingNumber = lessonLength*x;
            decimal Convert0 = dec + workingNumber;
            TimeSpan Convert1 = TimeSpan.FromHours(Double.Parse(Convert0.ToString()));
            string Convert2 = Convert1.ToString().Remove(5);
            periodStartTimes[x] = Convert2;
        }

        FormOpen.subjectName = cmbSubjects.Text;
        FormOpen.startTime2 = periodStartTimes[1];  //IT DOES WORK LIKE THIS
        FormOpen.startTime3 = periodStartTimes[2];

        FormOpen.ShowDialog();

    }

我已经提供了整个代码,所以它更清晰,如果有更有效的编码方式,如果有人能给我一些提示,我将非常感激。

【问题讨论】:

    标签: c# arrays visual-studio reference get


    【解决方案1】:

    您的代码无法使用该数组运行 periodStartTimes 这是一个字符串数组,当您初始化它时,您会得到属性的值(即标签的当前文本)而不是对可用于更改标签的属性。

    在任何情况下,允许不同的类实例更改另一个类的内部值都是一种不好的做法。最好提供外部类使用的公共方法来更改内部属性。

    例如,您的测试表单类可能有一个名为 SetTextLabel 的公共方法

    public class Test : Form
    {
       ....
       public void SetLabelText(string name, string value)
       {
           switch(name)
           {
               case "startTime":
                  this.labelForStartTime.Text = value;
                  break;
               case "label1":
                  this.firstLabelToUpdate.Text = value;
                  break;
               ... 
               // and so on for all other labels 
           }
       }
    }
    

    通过这种方式,更改标签的代码位于 Test 类中,您可以添加所需的所有检查和验证,并具有对 Test 类的所有内部变量的完全访问权限。

    现在你的循环可以重写为

    for (int x = 1; x <= numOfPeriods; x++)  
    {
        decimal workingNumber = lessonLength*x;
        decimal Convert0 = dec + workingNumber;
        TimeSpan Convert1 = TimeSpan.FromHours(Double.Parse(Convert0.ToString()));
        string Convert2 = Convert1.ToString().Remove(5);
        FormOpen.SetLabelText("label" + x.ToString, Convert2;
    }
    

    当然,现在您不再需要该数组,这可能会消除您实际代码中存在的另一个错误。该数组固定为 9 个元素,但您从 1 开始循环 numOfPeriods(因此跳过第一个元素),如果 numOfPeriods 大于 8,您的代码将因 IndexOutOfRange 异常而崩溃

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多