AnotherUser
AnotherUser

Reputation: 147

C# - Simple ListView problemm?

I have two forms, form1 and form2.

form1 has three buttons, button1(vanilla), button2(chocolate) and button3(nextpage).

form2 has a listView1

On form1 the user will click button1 or/and button2. If they click on both how do I display it underneath onto the next row on the listView1 on form2.

There's a screenshot below which shows what I mean

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);

    }

I want Chocolate to show onto the next line.

I want the Chocolate to show underneath Vanilla on the next line.

Upvotes: 0

Views: 55

Answers (2)

Shyju
Shyju

Reputation: 218732

Because you are concatenating both the strings to one string and passing that to the second forms constructor. What you should do is to pass a list of strings and loop through each one of them and add that to your listView.

So update your second form's constructors to accept a list of strings.

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

And in your first form,

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();
    }
}

EDIT : If you want to allow multiple instances of one string(When user clicks a button more than one time), You can simply remove the if(!_values.Contains( condition check before adding the item to the list. If you want to get the quantity of a string, you need to group it then do the count.

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.
}

Upvotes: 2

Rahul
Rahul

Reputation: 77876

You can do some trick like

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]);

    }

Upvotes: 0

Related Questions