I have a two forms that I created using the design view of Visual C# Express (Form1 & infoEntry). I set the properties to of the listview in Form1.

public System.Windows.Forms.ListView toSend;

I've invoked the second form and sent some data using code below. Works great.

infoEntry dataWindow = new infoEntry(); 
dataWindow.filePath(selectedFile); \\sending some data to new form
dataWindow.ShowDialog();

I'm running into a problem when I attempt to send data back to Form1. The following code is within a button_Click

            string patientid = (patientidSender.ToString());
            string shift2 = (shift2Sender.ToString());
            string[] row1 = { ("value1"), ("value2") };
            Form1.toSend.Items.Add("test").SubItems.AddRange(row1); \\<--- this line causing the error.

The last line of code causes an error "An object reference is required for the non-static field"
But when I use that exact same line in the First Form (Form1) it doesn't seem to have a problem addind items. What can I do to fix this please? And thank you in advance.

Recommended Answers

All 3 Replies

In my opinion:
If you must to create the instance of your toSend variable before you try to add items. In your code example you try to add items to the null object.

The object toSend is a listView box in Form1. I'm attempting to populate it with varaibles from a text box in another form that was invoked by Form1. So if I'm understanding the response correctly-- I need to make a variable that sends variables? I new to coding and if that is the case I can't even begin to visualize how to do so.

Add a property to "infoEntry" to accept a reference to the ListView.

    private ListView _lv;
    public ListView lv
    {
        get { return _lv; }
        set { _lv = value; }
    }

Then set that property before showing "dataWindow"

dataWindow.lv = this.toSend;
dataWindow.ShowDialog();

Then: lv.Items.Add("test").SubItems.AddRange(row1);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.