【问题标题】:how to show the checked checkbox into a messagebox in C#如何在 C# 中将选中的复选框显示到消息框中
【发布时间】:2020-01-25 04:26:26
【问题描述】:

我想知道如何像下图那样做:

在“摘要”(右侧)的“您购买:”下,我想列出用户在“可用书籍”(左侧)中签入的选项

private void btnPurchase_Click(object sender, EventArgs e)
    {
        string BOOKS;
        MessageBox.Show("You Purchase :\n"
                +"\t" + BOOKS + "\n" //checked checkbox shows here
                + "The selected payment method is : " + payment
                + "\nYour comment about us : " + txtKomen.Text);
    }

我使用上面的代码,它只显示第一个选中的复选框,如何在消息框中添加另一个选中的复选框?

我应该为BOOKS 使用数组吗?如果是这样,如何将其循环到消息框中?

【问题讨论】:

    标签: c# arrays checkbox


    【解决方案1】:

    你可以这样做:

    private void btnPurchase_Click(object sender, EventArgs e)
    {
        string[] BOOKS;
        var sb = new StringBuilder();
        foreach(var item in BOOKS)
        {
            sb.Append($"\t{item}");
            sb.AppendNewLine();
        }
        MessageBox.Show("You Purchase :\n"
            + sb.ToString()//checked checkbox shows here
            + "The selected payment method is : " + payment
            + "\nYour comment about us : " + txtKomen.Text);
    }
    

    虽然我不确定.Append.AppendNewLine() 是否存在于StringBuilder 中,但无论如何Visual Studio 应该会告诉您正确的名称。

    【讨论】:

    • 有一个错误使用未分配的局部变量'BOOKS'
    【解决方案2】:
    private void button1_Click(object sender, EventArgs e)
    {
        string books = "";
        foreach (var itemChecked in checkedListBox1.CheckedItems)
        {
            books += itemChecked + " ";
        }
        MessageBox.Show("You Purchase :\n" + "\t" + books + "\n");
    
    }
    

    如果你想使用它,你必须在你的表单上添加 CheckedListBox。

    【讨论】:

    • 它的工作原理顺便说一句,非常感谢,但我需要问一些问题,我需要添加“\t”以在书名前显示一个标签,我应该在哪里放置“\t " ?
    • 这张图片对您有帮助吗?
    • 绝对可以,但是如果我们想添加图书的价格作为示例,listcheckbox 可以做到吗?
    • 您可以为此使用 GridView。
    • 请原谅,但该怎么做呢?我对这种语言完全陌生,也许你可以告诉我怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多