简体   繁体   English

将值从文本框/单选按钮传递到一个文本框

[英]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: 所以可能是这样的:

  1. summary is blank, you trigger in some way the TextChanged event 摘要为空白,您将以某种方式触发TextChanged事件
  2. summary is set to the radios text, TextChanged event is recalled again 摘要设置为单选文本,再次调用TextChanged事件
  3. summary is set to the same radio text, nothing changes and the WinForms engine avoid to recall the TextChanged event handler 摘要设置为相同的单选文本,不进行任何更改,WinForms引擎避免调用TextChanged事件处理程序
  4. summary is set to the textboxes text (or new text is appended to the previous), TextChanged is recalled 将“摘要”设置为文本框文本(或将新文本追加到前一个文本框),并调用TextChanged
  5. continue from point 2 从第二点继续

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.

相关问题 如何将HTML文本框值从一个域复制到另一个域的文本框? - How can I copy HTML textbox values from one domain to another domain's textboxes? 将多个文本框中的值插入一列,但为每个文本框值创建一个新行 - Insert values from several textboxes into one column but creating a new row for each textbox value 如何使用for循环将选定单选按钮的结果显示到一个文本框中? - How to display results from selected radio buttons into one textbox using a for loop? 单击一个按钮以将文本框和面板的值传输到一个多行文本框 - Clicking a button to transfer values of textboxes and a panel to one multiline textbox 一个组件中的 Blazor 单选按钮从其他组件中取消选择单选按钮 - Blazor radio buttons in one component unselecting radio buttons from the others 添加两个文本框中的值,并在第三个文本框中显示总和 - Add values from two textboxes and display the sum in third textbox 如何从 2 个动态文本框中计算值并显示在第三个动态文本框中? - How to calculate values from 2 dynamic textboxes and displaying in third dynamic textbox? 将文本框值从视图传递到按钮 - Pass textbox value from view to buttons C# windows 表单,如何将单选按钮/复选框值输入到多个文本框中? - C# windows form, how to have radio buttons/checkboxes values input into multiple textboxes? 用文本框之间的文本框之一定位 - Position with one of the textbox in between textboxes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM