C# C“从另一个表单访问标签数组并更改其”;“文本”;财产
这是我的第一篇文章。所以,批评总是受欢迎的 我的问题是直截了当地看标题。 如何使用循环通过引用将值插入到不同的标签(get、set方法的形式不同) 我尝试的是创建一个包含标签引用的数组。事情是。。它将新值指定给数组,而不是更改引用,这将更改标签 我觉得再进一步解释有点困难。 如果你有任何问题,我会尽我所能回答你C# C“从另一个表单访问标签数组并更改其”;“文本”;财产,c#,arrays,visual-studio,reference,get,C#,Arrays,Visual Studio,Reference,Get,这是我的第一篇文章。所以,批评总是受欢迎的 我的问题是直截了当地看标题。 如何使用循环通过引用将值插入到不同的标签(get、set方法的形式不同) 我尝试的是创建一个包含标签引用的数组。事情是。。它将新值指定给数组,而不是更改引用,这将更改标签 我觉得再进一步解释有点困难。 如果你有任何问题,我会尽我所能回答你 private void button1_Click(object sender, EventArgs e) { int numOfPeriods = C
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();
}
private void按钮1\u单击(对象发送者,事件参数e)
{
int numOfPeriods=Convert.ToInt32(cmbcperiods.Text)-1;
string initialString=cmbStartTime.Text;//从组合框“cmbStartTime”获取输入。
字符串newTime;
decimal dec=Convert.ToDecimal(TimeSpan.Parse(initialString).TotalHours);//将用户设置的课程开始时间转换为十进制值。
十进制dec2;
decimal lessonLength=1;//默认课程长度设置为1小时。
时间跨度时间;
Test FormOpen=新测试();
string[]periodStartTimes=新字符串[9]//循环引用表单测试上的标签
{
FormOpen.startTime,FormOpen.startTime2,FormOpen.startTime3,FormOpen.startTime4,
FormOpen.startTime5,FormOpen.startTime6,FormOpen.startTime7,FormOpen.startTime8,
FormOpen.startTime9
};
if(cmbLessonLength.Text!=“”)//如果组合框“cmbLessonLength”不为空,请使用该值而不是默认的课程长度。
{
lessonLength=Convert.ToDecimal(cmbLessonLength.Text)/60;//将分钟转换为小时。
}
dec2=dec+lessonLength;
time=TimeSpan.FromHours(Double.Parse(dec2.ToString());
newTime=time.ToString();
如果(新时间[0]='0')
{
FormOpen.startTime=initialString+“-”+newTime.Remove(5)、Remove(0,1);
}
其他的
{
FormOpen.startTime=initialString+“-”+newTime.Remove(5);
}
对于(int x=1;x您的代码无法使用该数组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
}
}
}
通过这种方式,更改标签的代码位于测试类内部,您可以添加对测试类的所有内部变量具有完全访问权限所需的所有检查和验证
现在您的循环可以重写为
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;
}
for(int x=1;x