C# 如何在C中从文本框向数组插入输入#
我想在用户单击submit按钮时将用户输入插入数组。 这是我写的,但似乎不起作用。表单称为form1,它是自己的类,textbox是textbox1。注:我是编程新手C# 如何在C中从文本框向数组插入输入#,c#,arrays,textbox,windows-forms-designer,windowsformsintegration,C#,Arrays,Textbox,Windows Forms Designer,Windowsformsintegration,我想在用户单击submit按钮时将用户输入插入数组。 这是我写的,但似乎不起作用。表单称为form1,它是自己的类,textbox是textbox1。注:我是编程新手 //This is my array private string[] texts = new string[10]; public string[] Texts { get { return texts; } set { texts = value
//This is my array
private string[] texts = new string[10];
public string[] Texts
{
get { return texts; }
set { texts = value; }
}
//I then attempt to insert the value of the field into the textbox
form1 enterDetails = new form1();
for(int counter = 0; counter<Texts.Length; counter++)
{
texts[counter]=enterDetails.textbox1.Text;
}
//这是我的数组
私有字符串[]文本=新字符串[10];
公共字符串[]文本
{
获取{返回文本;}
设置{text=value;}
}
//然后我尝试将该字段的值插入文本框
form1 enterDetails=新form1();
对于(int counter=0;counter您在这里犯了一些愚蠢的错误:
在文本属性的setter中,您应该说
texts = value;
而不是guestNames=value;
您不需要创建form1的新实例,因为上面编写的所有代码都已在form1类中。如果没有,请尝试获取相同的form1实例
不需要,但应该设置属性而不是字段
替换
texts[counter] = .......
与
因此,您的完整代码应该如下所示:
public form1() //Initialize your properties in constructor.
{
Texts = new string[10]
}
private string[] texts;
public string[] Texts
{
get {return texts; }
set { texts = value; }
}
for(int counter = 0; counter<Texts.Length; counter++)
{
Texts[counter]=this.textbox1.Text;
}
public form1()//在构造函数中初始化属性。
{
文本=新字符串[10]
}
私有字符串[]文本;
公共字符串[]文本
{
获取{返回文本;}
设置{text=value;}
}
对于(int counter=0;counter什么不起作用?发生了什么?您似乎粘贴了来自不同作用域的2或3个代码片段,不可能知道发生了什么这可能并不能解决您的问题,但是:如果一个属性的setter设置的变量与getter检索的变量不同,那么该属性就会产生巨大的代码气味
public form1() //Initialize your properties in constructor.
{
Texts = new string[10]
}
private string[] texts;
public string[] Texts
{
get {return texts; }
set { texts = value; }
}
for(int counter = 0; counter<Texts.Length; counter++)
{
Texts[counter]=this.textbox1.Text;
}