【问题标题】:How do I show a text in text box in correspondence to controls?如何在文本框中显示与控件相对应的文本?
【发布时间】:2018-05-24 00:46:46
【问题描述】:

我对 Windows 窗体的编程非常陌生。目前,这只是一个程序的想法,但让我写一个简单的程序流程:

  1. 控件很少,两个单选按钮三个复选框控件一个下拉式组合框控件一个列表框控件,最后是文本框控件来显示输出。
  2. 如果我选择组合框上的内容,它也会选择单选按钮内容。列表框也是如此 - 复选框控制关系。

也就是说,组合框和单选按钮中都存在 content1 和 content2。链接这两个函数没有问题,但是......我在打印文本时遇到了一些麻烦。也就是说,如果我选择 content1,文本框上会出现以下字符串:

“选择的内容是:content1”

对于复选框-列表框关系,我有color1、color2和color3。可以选择多于1种颜色(勾选,如果是复选框),选择后,下面的字符串也会出现在前面所说的内容选择文本下:

"选择的内容是:content1" "选择的颜色是:color1 color2"

如何使文本格式显示?到目前为止,我正在使用此代码,但我还无法在文本框中显示文本。文本框的名称是 textBoxResults

if (radioContent1.Checked == true)
        {

            textBoxResults.Text = "Chosen content is : content1";
        }

另外,从概念上讲,如何将 color1 链接到 3 的复选框,以便如果我选中一个或多个框,以便它对应于列表框控件中 color1 到 3 的内容中选择的一个或多个选项?

提前感谢您的回答。这将帮助我更多地了解 Windows 窗体。

【问题讨论】:

  • 这段代码你在哪里写的?在组合框中 indexchanged 事件?你为 checkbox.Checked 写过代码吗?
  • 有什么问题?使用textBoxResults.Text +=(注意+=)。我不明白你的问题。

标签: c# winforms


【解决方案1】:

以下只是入门的基本思路,但可以通过不同方式进行增强。

在您的表单加载事件中,添加以下代码:

radioButton1.CheckedChanged += RadioButtons_CheckedChanged;
radioButton2.CheckedChanged += RadioButtons_CheckedChanged;
radioButton3.CheckedChanged += RadioButtons_CheckedChanged;

然后您可以使用RadioButtons_CheckedChanged 事件处理程序来相应地更改文本:

private void RadioButtons_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton1.Checked)
        textBox1.Text = "Chosen content is Content1.";
    else if (radioButton2.Checked)
        textBox1.Text = "Chosen content is Content2.";
    else if (radioButton3.Checked)
        textBox1.Text = "Chosen content is Content3.";
}

同样,对于复选框:

checkBox1.CheckedChanged += CheckBoxes_CheckedChanged;
checkBox2.CheckedChanged += CheckBoxes_CheckedChanged;
checkBox3.CheckedChanged += CheckBoxes_CheckedChanged;

然后:

private void CheckBoxes_CheckedChanged(object sender, EventArgs e)
{
    if (!checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked)
    {
        textBox2.Clear();
        return;
    }

    textBox2.Text = "Chosen colors are :";
    if (checkBox1.Checked)
        textBox2.Text += " color1";
    if (checkBox2.Checked)
        textBox2.Text += " color2";
    if (checkBox3.Checked)
        textBox2.Text += " color3";
}

【讨论】:

  • 感谢您的帮助!这是一个迟到的回应,但是当我想为你的答案投票时,我突然失去了我的 Stack Overflow 帐户链接。这真的很有帮助,谢谢!
猜你喜欢
  • 2011-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
相关资源
最近更新 更多