[英]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.