【问题标题】:C# - Simple ListView problemm?C# - 简单的 ListView 问题?
【发布时间】:2016-01-30 21:36:44
【问题描述】:

我有两个表格,表格 1 和表格 2。

form1 有三个按钮,button1(vanilla)、button2(chocolate) 和 button3(nextpage)。

form2 有一个 listView1

在 form1 上,用户将单击 button1 或/和 button2。如果他们同时单击两者,我如何将其显示在 form2 上 listView1 的下一行中。

下面有一张截图说明了我的意思

public partial class form1 : Form
{

    public form1()
    {
        InitializeComponent();
    }

    public static string buttonValue = "";
    public static string buttonValue1 = "";

    public void button1_Click(object sender, EventArgs e)
    {
        buttonValue = "Vanillaaaa";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        buttonValue1 = "Chocolate";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        form2 form2 = new form2(buttonValue + buttonValue1);
        form2.Show();
        this.Hide();
    }
}

Form2

public partial class form2 : Form
{
    private string _passedValue = "";
    private string _passedValue1 = "";

    public form2(string passedValue)
    {
        InitializeComponent();
        _passedValue = passedValue;
        listView1.Items.Add(_passedValue);
        listView1.Items.Add(_passedValue1);

    }

我希望巧克力在下一行的香草下方显示。

【问题讨论】:

    标签: c# forms listview button


    【解决方案1】:

    因为您将两个字符串连接到一个字符串并将其传递给第二个表单构造函数。您应该做的是传递一个字符串列表并循环遍历每个字符串并将其添加到您的 listView。

    所以更新您的第二个表单的构造函数以接受字符串列表。

    public partial class form2 : Form
    {                   
        public form2(List<string> passedValues)
        {
            InitializeComponent();
            foreach(var item in passedValues)
            {
               listView1.Items.Add(item);
            }
        }
    }
    

    在你的第一种形式中,

    public partial class form1 : Form
    {
        private string vanilla = "Vanilla";
        private string chocolate= "Chocolate";
        private List<string> _values= new List<string>();
        public form1()
        {
            InitializeComponent();
        }
    
        public void button1_Click(object sender, EventArgs e)
        {
           if (!_values.Contains(vanilla))
           {
              _values.Add(vanilla);
           }           
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
           if (!_values.Contains(chocolate))
           {
              _values.Add(chocolate);
           } 
        }
    
        private void button3_Click(object sender, EventArgs e)
        {
            form2 form2 = new form2(_values);
            form2.Show();
            this.Hide();
        }
    }
    

    编辑:如果你想允许一个字符串的多个实例(当用户点击一个按钮不止一次时),你可以简单地删除if(!_values.Contains( 条件检查,然后再将项目添加到列表。如果你想得到一个字符串的数量,你需要对它进行分组然后进行计数。

    var grouped = _values.GroupBy(k => k, v => v,
                           (k, v) => new { Name = k, Count = v.Count()}).ToList();
    foreach (var item in grouped)
    {
        var name = item.Name;
        var count = item.Count;
        //do something with the name and count now.
    }
    

    【讨论】:

    • 谢谢它的工作:),我想问.. 当我点击 Vanilla 2 次时,如何在 Quantity 列中显示 2?你能告诉我并引导我走向正确的方向吗?
    • 我上面说的该怎么做?
    • 查看更新答案中的更新。你基本上需要做一个group by
    • 这对我来说没有任何意义
    • 请您帮帮我并提供一些建议?
    【解决方案2】:

    你可以做一些类似的技巧

    public partial class form1 : Form
    {
    public form1()
    {
        InitializeComponent();
    }
    
    public static string buttonValue = "";
    
    public void button1_Click(object sender, EventArgs e)
    {
        buttonValue = "Vanillaaaa";
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        buttonValue += "~Chocolate";
    }
    
    private void button3_Click(object sender, EventArgs e)
    {
        form2 form2 = new form2(buttonValue);
        form2.Show();
        this.Hide();
    }
    
    
    
    
    public partial class form2 : Form
    {
        public form2(string passedValue)
        {
            InitializeComponent();
           string[] _passedValue = passedValue.Split('~');
            listView1.Items.Add(_passedValue[0]);
            listView1.Items.Add(_passedValue[1]);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多