I seem to be have a great deal of trouble with a simple issue. Yes I'm new to c# but I try to learn what I can without jumping to post a question. In this case I just think I'm not asking the right question.
No code samples will help here because I'm talking about the basics ( implementation ). I have not really coded anything yet, just use the visual builder to create my windows forms and menus.
The issue I'm having is when I select a menu item (call it: set paths ) I want that list view on my main form to load from the path selected when I hit ok on form2. So I did a simple find folder dialog and I have my new path stored on form2 in a text box. When I hit ok on that form2 I want my listview form1 to populate. I know how to do all of this but I can not for the life of me access form1 from form2 or vice versa.
I tried making a call back function but I get that non-static variable cannot be referenced... error because my form1 is static, so I can't create any non static methods. I looked in to EventArgs but that just seems like an over kill for such a common request.
So what is the general way to do this?
If you need to access something on another form, just hold a reference to it from your first form, like this:
That said, if you just want to get a file path from the user, you want to use the OpenFileDialog class.
Robert's answer is correct as far as accessing members on another form. However, in general you should be storing the state of your application (call it the "model") separately from the state of your user interface (call it the "view"). This becomes very important as your application grows beyond one or two interactions. There are several philosophies or patterns about how to tie the two together (Google "model-view-controller" (MVC) and "model-view-viewmodel" (MVVM) for example), and if you really want to do this correctly I would recommend learning about those. My preference is for the MVVM approach, and you can do it fairly easily with Windows Forms even though it was designed with WPF applications in mind.
In .NET, the basic piece of code you should use to implement the connection between your viewmodel and your view is an interface called INotifyPropertyChanged. You create a class that implements this interface and sends notifications whenever a property changes, so for example for your path property you would create this class:
It may seem like a lot of work, but now in your form1 you can create a new "ViewModel" instance, subscribe to the event, and then pass the instance to form2. form2 then simply updates the Path property on the viewmodel instance whenever the user selects a different path.
So, Form1 needs this code near the top:
And this goes in the Form1 constructor:
And when you create/show form2:
The form2 constructor then stores its own reference to the "viewmodel" instance, and sets the Path property whenever the path is changed by the user.
And finally the event handler in Form1:
Here's a link to a really good MVVM intro using Windows Forms, it uses two forms like you have in your example. MVVM (Model-View-ViewModel) Pattern For Windows Form Applications, using C#