[英]How can I copy HTML textbox values from one domain to another domain's textboxes?
[英]Pass Values from Textboxes / Radio buttons to One TextBox
我有几个不同的单选按钮和四个文本框。
我要实现的是从所有文本框中获取值,并将其全部显示在一个摘要文本框中(摘要)。
我设法从所有单选按钮获取值,但是我无法从文本框获取值。
我希望它看起来像这样:
RB1-RB2 -RB3-TXTB1-TXTB2-TXTB3-TXTB4
单选按钮以及文本框都在组框中。
private void summary_TextChanged(object sender, EventArgs e)
{
var radios = this.Controls.OfType<GroupBox>().OrderBy(x => x.TabIndex)
.SelectMany(x => x.Controls.OfType<RadioButton>())
.Where(x => x.Checked == true)
.Select(x => x.Text).ToList();
this.summary.Text = string.Join("-", radios);
var textboxes = this.Controls.OfType<GroupBox>().OrderBy(x => x.TabIndex)
.SelectMany(x => x.Controls.OfType<TextBox>())
.Select(x => x.Text).ToList();
this.summary.Text = string.Join("-", textboxes);
}
最后一行用文本框的内容替换了摘要文本框的内容,因此单选按钮的内容丢失了。
您需要做的是附加到上一个内容
// Adding also a - to separate radiobuttons from textboxes
this.summary.AppendText("-" + string.Join("-", textboxes));
同样,如果将此代码附加到summary
TextChanged事件上,那么也会有一个大问题,因为调用此代码时,您更改了同一TextBox summary
的内容,因此代码会自行调用。
通常,WinForms引擎足够聪明,可以避免这种递归,但是您的代码无法使用Form引擎的安全措施,因为您两次更改了summary
内容。
所以可能是这样的:
不要将此代码添加为摘要文本框的事件处理程序,只需将其用作其他文本框TextChanged事件或RadioButtons选中事件的事件处理程序即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.