如何让列表框显示复选框和单选按钮中的项目?- C# Windows 表单

huangapple go评论68阅读模式
英文:

How to make a listbox display items from a checked list and radio buttons? - C# Windows Form

问题

I'm doing a project where I am trying to simulate an ice cream parlor. For this specific section, I have the (mutually exclusive) radio buttons representing the dressing the customer can select. There are also a number of checked items (not mutually exclusive) which the customer can select in the checkedListBox. All of the items that a customer selects from the radio buttons and checkedListBox are supposed to appear in a listbox so that the customer can keep track of all of the ordered items.

Of course, all of the code here is very unfinished and basic. I don't plan on adding any of the calculations for the prices until I make sure that the structure itself is working.

This is what I currently have so far:

private void GetToppings()
{
    foreach (ListViewItem li in checkedListBox1.Items)
    {
        if (li.Selected == true)
        {
            label1.Text += li + " ";
        }
    }
    if (checkedListBox1.SelectedItem.ToString() == "Sprinkles")
    {
    }
    if (checkedListBox1.SelectedItem.ToString() == "Chocolate Chips")
    {
    }
    if (checkedListBox1.SelectedItem.ToString() == "M&Ms")
    {
    }
    if (checkedListBox1.SelectedItem.ToString() == "Oreos")
    {
    }
    if (checkedListBox1.SelectedItem.ToString() == "Cookie Dough")
    {
    }
}

private void GetDressing()
{
    if (radioButton1.Checked)
    {
        sDressing += "Caramel";
    }

    if (radioButton2.Checked)
    {
        sDressing += "Hot Fudge";
    }

    if (radioButton3.Checked)
    {
        sDressing += "Peanut Butter";
    }

    if (radioButton4.Checked)
    {
        sDressing += "Strawberry Syrup";
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 18; i++)
    {
        listBox1.Items.Add(i);
    }
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    listBox1.Items.Remove(listBox1.SelectedItem);
}

I am still very new to Windows Form programming in C#, so please forgive me if any of these questions/errors seem very basic.

英文:

I'm doing a project where I am trying to simulate an ice cream parlor. For this specific section, I have the (mutually exclusive) radio buttons representing the dressing the customer can select. There are also a number of checked items (not mutually exclusive) which the customer can select in the checkedListBox. All of the items that a customer selects from the radio buttons and checkedListBox are supposed to appear in a listbox. so that the customer can keep track of all of the ordered items.

Of course, all of the code here is very unfinished and basic. I don't plan on adding any of the calculations for the prices until I make sure that the structure itself is working.

This is what I currently have so far:

    private void GetToppings()
    {
        foreach (ListViewItem li in checkedListBox1.Items)
        {
            if (li.Selected == true)
            {
                label1.Text += li + &quot; &quot;;
            }
        }
        if (checkedListBox1.SelectedItem.ToString() == &quot;Sprinkles&quot;)
        {
        }
        if (checkedListBox1.SelectedItem.ToString() == &quot;Chocolate Chips&quot;)
        {
        }
        if (checkedListBox1.SelectedItem.ToString() == &quot;M&amp;Ms&quot;)
        {
        }
        if (checkedListBox1.SelectedItem.ToString() == &quot;Oreos&quot;)
        {
        }
        if (checkedListBox1.SelectedItem.ToString() == &quot;Cookie Dough&quot;)
        {
        }

    private void GetDressing()
    {
        if (radioButton1.Checked)
        {
            sDressing += &quot;Caramel&quot;;
        }

        if (radioButton2.Checked)
        {
            sDressing += &quot;Hot Fudge&quot;;
        }

        if (radioButton3.Checked)
        {
            sDressing += &quot;Peanut Butter&quot;;
        }

        if (radioButton4.Checked)
        {
            sDressing += &quot;Strawberry Syrup&quot;;
        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i&lt;18; i++)
        {
            listBox1.Items.Add(i);
        }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.Items.Remove(listBox1.SelectedItem);
    }

I am still very new to Windows Form programming in C#, so please forgive me if any of these questions/errors seem very basic.

答案1

得分: 0

`RadioButton`使用如下代码:

    var radioButton = Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
    if (radioButton is not null)
    {
        // 做一些事情
    }

对于`CheckedListBox`,考虑以下内容,通过一个具有文本和标识符的模型/类进行填充,因为在大多数情况下,以后您会使用数据源,这对于跟踪尚未存在的项目是很重要的,但最好在此时就这样做。

扩展方法用于获取CheckedListBox中的项目。放置在一个类文件中。

    public static class CheckedListBoxExtensions
    {
        public static List<T> CheckedList<T>(this CheckedListBox sender)
            => sender.Items.Cast<T>()
                .Where((_, index) => sender.GetItemChecked(index))
                .Select(item => item)
                .ToList();
    }

使用一个类/模型来填充CheckedListBox,ToString 用于显示该项目。放置在一个类文件中。

    public class Topping
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public override string ToString() => Name;
    }

实现

    public partial class StackOverflowForm : Form
    {
        public StackOverflowForm()
        {
            InitializeComponent();
            List<Topping> toppings = new List<Topping>
            {
                new Topping() { Id = 1, Name = "Sprinkles" },
                new Topping() { Id = 2, Name = "Chocolate Chips" },
                new Topping() { Id = 3, Name = "M&Ms" },
                new Topping() { Id = 4, Name = "Oreos" },
                new Topping() { Id = 5, Name = "Cookie Dough" }
            };
    
            checkedListBox1.DataSource = toppings;
        }
    
        private void GetToppingsButton_Click(object sender, EventArgs e)
        {
            List<Topping> toppings = checkedListBox1.CheckedList<Topping>();
            if (toppings.Count > 0)
            {
                listBox1.DataSource = toppings;
            }
            else
            {
                listBox1.DataSource = null;
            }
        }
    }
英文:

The RadioButton use the following

var radioButton = Controls.OfType&lt;RadioButton&gt;().FirstOrDefault(x =&gt; x.Checked);
if (radioButton is not null)
{
    // do something
}

For the CheckedListBox consider the following which populates via a model/class which has text and a identifier as in most cases at a later date you work with a data source this is important to keep track of items which you are not there yet but best to do it just the same.

Extension method to get items in the CheckedListBox. Place in a class file.

public static class CheckedListBoxExtensions
{
    public static List&lt;T&gt; CheckedList&lt;T&gt;(this CheckedListBox sender)
        =&gt; sender.Items.Cast&lt;T&gt;()
            .Where((_, index) =&gt; sender.GetItemChecked(index))
            .Select(item =&gt; item)
            .ToList();
}

Use a class/model for populating the CheckedListBox, ToString is used to display the item. Place in a class file.

public class Topping
{
    public int Id { get; set; }
    public string Name { get; set; }
    public override string ToString() =&gt; Name;
}

Implementation

public partial class StackOverflowForm : Form
{
    public StackOverflowForm()
    {
        InitializeComponent();
        List&lt;Topping&gt; toppings = new List&lt;Topping&gt;
        {
            new Topping() { Id = 1, Name = &quot;Sprinkles&quot; },
            new Topping() { Id = 2, Name = &quot;Chocolate Chips&quot; },
            new Topping() { Id = 3, Name = &quot;M&amp;Ms&quot; },
            new Topping() { Id = 4, Name = &quot;Oreos&quot; },
            new Topping() { Id = 5, Name = &quot;Cookie Dough&quot; }
        };

        checkedListBox1.DataSource = toppings;
    }

    private void GetToppingsButton_Click(object sender, EventArgs e)
    {
        List&lt;Topping&gt; toppings = checkedListBox1.CheckedList&lt;Topping&gt;();
        if (toppings.Count &gt; 0)
        {
            listBox1.DataSource = toppings;
        }
        else
        {
            listBox1.DataSource = null;
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月19日 01:11:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定