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
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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.