[英]Pass Values from Textboxes / Radio buttons to One TextBox
I have few different radio buttons and four textboxes. 我有几个不同的单选按钮和四个文本框。
What I want to achieve is to get values from all textboxes and display it all in one summary textbox (summary). 我要实现的是从所有文本框中获取值,并将其全部显示在一个摘要文本框中(摘要)。
I have managed to get values from all radio button however I cannot get the values from textboxes. 我设法从所有单选按钮获取值,但是我无法从文本框获取值。
I want this to look like this: 我希望它看起来像这样:
RB1 - RB2 -RB3 - TXTB1 - TXTB2 - TXTB3 - TXTB4 RB1-RB2 -RB3-TXTB1-TXTB2-TXTB3-TXTB4
Radio button as well as textboxes are all in groupboxes. 单选按钮以及文本框都在组框中。
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);
}
The last line replaces the content of the summary textbox with the content of the textboxes, thus the content of the radiobuttons is lost. 最后一行用文本框的内容替换了摘要文本框的内容,因此单选按钮的内容丢失了。
What you need to do is to Append to the previous content 您需要做的是附加到上一个内容
// Adding also a - to separate radiobuttons from textboxes
this.summary.AppendText("-" + string.Join("-", textboxes));
Also if you have attached this code to the summary
TextChanged event then there is a big problem because when this code is called you change the content of the same TextBox summary
and thus the code recalls itself. 同样,如果将此代码附加到summary
TextChanged事件上,那么也会有一个大问题,因为调用此代码时,您更改了同一TextBox summary
的内容,因此代码会自行调用。
Usually, the WinForms engine is smart enough to avoid this kind of recursion but your code defeats the safety measures of the Form engine because you change the summary
content two times. 通常,WinForms引擎足够聪明,可以避免这种递归,但是您的代码无法使用Form引擎的安全措施,因为您两次更改了summary
内容。
So what is probably happening is this: 所以可能是这样的:
Do not add this code as the event handler for the summary textbox, just use it as the event handler for the other textboxes TextChanged event or for the RadioButtons checked event. 不要将此代码添加为摘要文本框的事件处理程序,只需将其用作其他文本框TextChanged事件或RadioButtons选中事件的事件处理程序即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.