C# 字符串数组属性返回窗体上文本框的文本属性
因此,我有一个由脚本动态生成的表单,它有一个标签和一个文本框,用于作为参数提取的每个字符串。我希望用户填写每个文本框,然后选择OK按钮。之后,我想处理每个文本框的每个项目。以前只有1项时,我编写了一个非常简单的属性来获取文本框的值C# 字符串数组属性返回窗体上文本框的文本属性,c#,arrays,winforms,C#,Arrays,Winforms,因此,我有一个由脚本动态生成的表单,它有一个标签和一个文本框,用于作为参数提取的每个字符串。我希望用户填写每个文本框,然后选择OK按钮。之后,我想处理每个文本框的每个项目。以前只有1项时,我编写了一个非常简单的属性来获取文本框的值 Public string Text { get { return textbox1.Text; } } 我希望我能通过动态的文本框做同样优雅的事情 Public string [] Text { get { return **Each text box text
Public string Text { get { return textbox1.Text; } }
我希望我能通过动态的文本框做同样优雅的事情
Public string [] Text { get { return **Each text box text as an array**; } }
我已经考虑了一段时间,我想不出一个优雅的方法来设置它,下面是如何将文本框添加到表单中
string[] keywords = Environment.GetCommandLineArgs();
int placement = 10;
foreach (string keyword in keywords)
{
if (keyword == keywords[0])
continue;
Label lbl = new Label();
lbl.Text = keyword;
lbl.Top = placement;
this.Controls.Add(lbl);
TextBox txt = new TextBox();
txt.Top = placement;
txt.Left = 100;
this.Controls.Add(txt);
placement += 20;
}
我可以在表单关闭时循环遍历每个文本框的文本,并用值填充公共数组,但我更愿意想出一些更优雅的方法,摆脱强迫做事的习惯。有人对如何实现这一点有什么好主意吗?我想我可以将所有文本框添加到一个数组中,然后让string属性以某种方式获取指定文本框的文本,或者干脆将文本框数组改为公共属性。有没有办法写这样的东西
Public string [] Text { get { return TextBoxArray[**value trying to be gotten**].Text; } }
或者这对于一个属性来说太多了,需要成为一个方法吗?还有其他人的想法吗?我意识到这是一个有点琐碎的问题,可以用多种方法解决,我只是想拓宽我的视野,用一些很酷的方法来完成类似的事情。从技术上讲,属性只是getter/setter方法的语法糖分-因此属性可以做方法可以做的任何事情(但大多数时候它不应该!) 假设您的命令行参数不能重复,我只想将所有文本框添加到Dictionary对象中 基本上在启动时: 创建一个:
Dictionary<string, TextBox> boxes;
然后在getter中,您可以使用
boxes[commandName]
这将返回您可以操作的文本框。这当然都是强类型:)
下面是一些示例代码:
string[] keywords = Environment.GetCommandLineArgs();
Dictionary<string, TextBox> boxes = new Dictionary<string, TextBox>();
int placement = 10;
foreach (string keyword in keywords)
{
if (keyword == keywords[0])
continue;
Label lbl = new Label();
lbl.Text = keyword;
lbl.Top = placement;
this.Controls.Add(lbl);
TextBox txt = new TextBox();
txt.Top = placement;
txt.Left = 100;
this.Controls.Add(txt);
placement += 20;
boxes.Add(keyword, txt);
}
string[]keywords=Environment.GetCommandLineArgs();
字典框=新字典();
整数位置=10;
foreach(关键字中的字符串关键字)
{
如果(关键字==关键字[0])
继续;
标签lbl=新标签();
lbl.Text=关键字;
lbl.Top=位置;
this.Controls.Add(lbl);
TextBox txt=新的TextBox();
txt.Top=位置;
txt.Left=100;
this.Controls.Add(txt);
位置+=20;
添加(关键字,txt);
}
那么我想你不需要一个getter,只要让box字典可见就行了
public Dictionary<string, TextBox> Boxes { get { return boxes; } }
公共字典框{get{return box;}
如果您对文本框不感兴趣,只想要值:
public Dictionary<string, string> Arguments
{
get
{
Dictionary<string, string> vals = new Dictionary<string, string>();
foreach(KeyValuePair<string, TextBox> kvp in boxes)
{
vals.Add(kvp.Key, kvp.Value.Text);
}
return vals;
}
}
公共字典参数
{
得到
{
字典VAL=新字典();
foreach(框中的KeyValuePair kvp)
{
添加(kvp.Key、kvp.Value.Text);
}
返回VAL;
}
}
从技术上讲,属性只是getter/setter方法的语法糖——因此属性可以做方法可以做的任何事情(但大多数时候它不应该!)
假设您的命令行参数不能重复,我只想将所有文本框添加到Dictionary对象中
基本上在启动时:
创建一个:
Dictionary<string, TextBox> boxes;
然后在getter中,您可以使用
boxes[commandName]
这将返回您可以操作的文本框。这当然都是强类型:)
下面是一些示例代码:
string[] keywords = Environment.GetCommandLineArgs();
Dictionary<string, TextBox> boxes = new Dictionary<string, TextBox>();
int placement = 10;
foreach (string keyword in keywords)
{
if (keyword == keywords[0])
continue;
Label lbl = new Label();
lbl.Text = keyword;
lbl.Top = placement;
this.Controls.Add(lbl);
TextBox txt = new TextBox();
txt.Top = placement;
txt.Left = 100;
this.Controls.Add(txt);
placement += 20;
boxes.Add(keyword, txt);
}
string[]keywords=Environment.GetCommandLineArgs();
字典框=新字典();
整数位置=10;
foreach(关键字中的字符串关键字)
{
如果(关键字==关键字[0])
继续;
标签lbl=新标签();
lbl.Text=关键字;
lbl.Top=位置;
this.Controls.Add(lbl);
TextBox txt=新的TextBox();
txt.Top=位置;
txt.Left=100;
this.Controls.Add(txt);
位置+=20;
添加(关键字,txt);
}
那么我想你不需要一个getter,只要让box字典可见就行了
public Dictionary<string, TextBox> Boxes { get { return boxes; } }
公共字典框{get{return box;}
如果您对文本框不感兴趣,只想要值:
public Dictionary<string, string> Arguments
{
get
{
Dictionary<string, string> vals = new Dictionary<string, string>();
foreach(KeyValuePair<string, TextBox> kvp in boxes)
{
vals.Add(kvp.Key, kvp.Value.Text);
}
return vals;
}
}
公共字典参数
{
得到
{
字典VAL=新字典();
foreach(框中的KeyValuePair kvp)
{
添加(kvp.Key、kvp.Value.Text);
}
返回VAL;
}
}
这里有一个想法:
Public System.Collections.Generic.List<string> Text {
get {
System.Collections.Generic.List<string> returnValue = new string[textBoxCount];
foreach (Control ctrl in this.Controls)
if (ctrl.GetType() == typeof(TextBox))
returnValue.Add(((TextBox)ctrl).Text);
return returnValue;
}
}
Public System.Collections.Generic.List Text{
得到{
System.Collections.Generic.List returnValue=新字符串[textBoxCount];
foreach(此.Controls中的控件ctrl)
if(ctrl.GetType()==typeof(文本框))
returnValue.Add(((文本框)ctrl).Text);
返回值;
}
}
当然,它会遍历所有控件,但至少不必将它们保存在内存中的某种变量中,如System.Collections.Generic.List。或者您也可以这样做,然后在这个foreach上删除if。这里有一个想法:
Public System.Collections.Generic.List<string> Text {
get {
System.Collections.Generic.List<string> returnValue = new string[textBoxCount];
foreach (Control ctrl in this.Controls)
if (ctrl.GetType() == typeof(TextBox))
returnValue.Add(((TextBox)ctrl).Text);
return returnValue;
}
}
Public System.Collections.Generic.List Text{
得到{
System.Collections.Generic.List returnValue=新字符串[textBoxCount];
foreach(此.Controls中的控件ctrl)
if(ctrl.GetType()==typeof(文本框))
returnValue.Add(((文本框)ctrl).Text);
返回值;
}
}
当然,它会遍历所有控件,但至少不必将它们保存在内存中的某种变量中,如System.Collections.Generic.List。或者您可以这样做,并在此foreach上删除if。其他选项可能更好,但这可能是最直接和简洁的:(使用LINQ,添加
使用System.LINQ;
,需要.NET 3.5或更高版本)
public string[]Text{get{return Controls.OfType().Select(x=>x.Text.ToArray();}
这与AlejoBrz的解决方案非常相似,但使用LINQ更清晰、简洁。其他选项可能更好,但这是li