1

I've looked up many question on how to do this, but none of them seem to work. I have a ListView on my main view, and I'm populating it from adding data to it from another form. On the other form, I have a couple strings from text fields I'd like to pass to it. Here's my code:

// Add new booking form:

namespace Booker
{
  public partial class newBookingForm : Form
{
    public newBookingForm()
    {
        InitializeComponent();
    }

    private void NewBookingForm_Load(object sender, EventArgs e)
    {
        roomField.Focus();
    }

    private void newBookingButton_Click(object sender, EventArgs e)
    {
        // Add to ListView
        // Add to Parse
        string room = this.roomField.Text;
        string date = this.datePicker.Value.ToShortDateString();
        string time = this.timeField.Text;
        string person = "<username>"; // Get current Parse user

        Booker booker = new Booker();
        string[] array = new string[4] { room, date, time, person };
        booker.UpdatingListView(array);

        this.Hide();
    }
}

}

// ListView form:

namespace Booker
{
public partial class Booker : Form
{
    delegate void MyDelegate(string[] array);

    public Booker()
    {
        InitializeComponent();
    }

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);
        Environment.Exit(0);
    }

    private void Booker_Load(object sender, EventArgs e)
    {
        listView.View = View.Details;
        listView.AllowColumnReorder = false;
    }

    /* Methods for the Booker form */

    private void newBookingButton_Click(object sender, EventArgs e)
    {
        newBookingForm nbf = new newBookingForm();
        nbf.Show();
    }

    public void UpdatingListView(string[] array)
    {
        if (this.listView.InvokeRequired)
            this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array });
        else
        {
            ListViewItem lvi = new ListViewItem(array[0]);
            lvi.SubItems.Add(array[1]);
            this.listView.Items.Add(lvi);
        }
    }

    private void exitButton_Click(object sender, EventArgs e)
    {
        // Error - no action being sent
    }

    private void helpButton_Click(object sender, EventArgs e)
    {
        // Display help panel
    }

    private void contactButton_Click(object sender, EventArgs e)
    {
        // Open panel/email
    }

    private void newBooking_Click(object sender, EventArgs e)
    {
        newBookingButton_Click(sender, e);

        //string[] row = { "Meeting room", "April 11, 2013", "12:00PM-1:00PM", "Ryan" };
        //var listViewItem = new ListViewItem(row);
        //listView.Items.Add(listViewItem);
    }
Ryan Cohen
  • 61
  • 2
  • 11
  • You forgot to ask a question – tnw Apr 11 '13 at 19:40
  • Also, it appears you pass in your array to `UpdatingListView` and then just take `array[1]` and put it into `lvi`. You don't do anything with the rest of the array, then add it to your `listView`. Is that the issue? That everything in your array doesn't appear in your `listView`? – tnw Apr 11 '13 at 19:42
  • The issue is that none of the data is entered into the ListView. – Ryan Cohen Apr 11 '13 at 19:54
  • Yeah, like I said, you don't ever do anything with `array[2]` and `array[3]`. You don't see anything in the `listView`? From what you have, you should see the first two items of your array in one column. – tnw Apr 11 '13 at 19:59
  • I edited it in my code, and now I'm adding the entire array. I created a data object class to store the values, then try to add them to the ListView. Trying now... – Ryan Cohen Apr 11 '13 at 20:01
  • See my answer. May be what you're looking for. – tnw Apr 11 '13 at 20:03

2 Answers2

1

Try this in UpdatingListView:

ListViewItem lvi = new ListViewItem(array[0],0);
lvi.SubItems.Add(array[1]);
lvi.SubItems.Add(array[2]);
lvi.SubItems.Add(array[3]);
this.listView.Items.Add(lvi);

This will create a column in the first position of your ListView with the 4 items of your array as rows in that column.

tnw
  • 13,521
  • 15
  • 70
  • 111
  • @RyanCohen Have you stepped through the code and verified that your array actually even contains anything? – tnw Apr 11 '13 at 20:18
  • Yes, I have. I used MessageBoxes to show the data in the array even from the UpdatingListView method. – Ryan Cohen Apr 11 '13 at 20:21
  • @RyanCohen OK, well there's clearly something else going on that I can't see. This code is more or less pulled directly from the [MSDN Reference guide to ListView](http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx). – tnw Apr 11 '13 at 20:25
0

To add items from another class, just access the other class' method to add from Program. Like this:

// Class we're adding from:

string name = "Ryan";
string site = "imryan.net";

string[] array = new string[2] = { name, site };
Program.classWithListView.UpdatingListView(array);

// Class we're adding to:

public void UpdatingListView(string[] array)
    {

        if (this.listView.InvokeRequired)
        {
            this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array });

        } else {
            ListViewItem lvi = new ListViewItem(array[0]);
            lvi.SubItems.Add(array[1]);
            lvi.SubItems.Add(array[2]);
            this.listView.Items.Add(lvi);
        }
    }

The string array will be added to the ListView as SubItems.

Ryan Cohen
  • 61
  • 2
  • 11