Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#BeginnerListView
Hi ppl
 
Ive got bit of a question as im trying to understand something. I dont need a code as i already found a walkaround for my problem, so im just trying to uderstand why this does not work.
 
Im gonna try to explain it in simplest way I can.
I have 2 forms:
- Form 1 is my main app menu form.
- Form 2 is my logger window that holds a big ListView control
 
Now Form 2 is only created when user presses specific tool strip button. But before user does that (actually that does not matter because it does not work even when form is created earlier) Im trying to feed that ListView in Form2 with some data. Ok so lest go with some code:
 
Form1:
 
Form1 form = new Form1();
form.AddToListView(e.Name.ToString(), e.FullPath.ToString(), e.ChangeType.ToString());
 
Form2:
 
        internal void AddToListView(string fileName, string fileSourceLocation, string fileDestination)
        {
            ListViewItem item = new ListViewItem();
            item.Text = fileName;
            item.SubItems.Add(fileSourceLocation);
            item.SubItems.Add(fileDestination);
            myListView.Items.Add(item);
        }
 
I debugged whole thing and it passes all data to "myListView.Items.Add(item);". Im just surprised because it passes all the values to the last stage but nothing appears in my ListView. Like I mentioned earlier i did a little bit of walkaround, I wrote class that handles data exhchanging for me. But Im trying to learn something in here so i would like to know why my first attempt didnt work.
 
Than you all for Your time in advance. Sorry if I overcomplicated things.
Cheers
Posted 26-Sep-12 10:07am
Wajrak_209

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].
 
Please see all other suggestions. If your application is pretty simple, the solution could be as simple as passing a reference to one form instance to another form and providing some internal (no need for public!) members on that form to be used in another form. In case of more complex projects and several cases of such trivial collaborations, this simple approach could mess up code and invite some bugs, due to lack of more strict encapsulation.
 
Good luck,
—SA
  Permalink  
v2
Comments
Maciej Los at 26-Sep-12 16:45pm
   
+5!
Sergey Alexandrovich Kryukov at 26-Sep-12 16:52pm
   
Thank you, Maciej. --SA
Member 4408432 at 27-Sep-12 13:41pm
   
Thank You for a very quick reply. I read it all, and yes it works like a charm. I do get idea how to pass data between forms, but i still have one more question and i hope You dont mind answering it. I graduated uni with masters in computer programming but that was years and years ago and well i abandoned programming for many years (ill kept doing little projects nothin major) so Im "rusty" as hell, but Im not a complete noob. Enough excuses. Can You tell me what is disadvantage of passing data using (sorry i dont have "post code option in comment" so i uploaded it to pastebin) this code: http://pastebin.com/j9a2S9D4 towards using delegates, props etc. Cheers
Sergey Alexandrovich Kryukov at 27-Sep-12 16:05pm
   
First, I like your "enough excuses" attitude. You understand. Even if you have a permanent job, you can get somewhat rusty of you have to do the same things for extended period of time. In programming, though, this is easier: you can often take more then one activity at work; and you can do some work at home. So, in my experience, nothing helps more then doing some projects of your own: hobby, to help children, relatives/friends, etc. Writing to CodeProject is very useful, by the way... I saw your code. Here is the problem: you use a static member where you could avoid it. Static members adds problems in bigger projects, so, they are best avoided unless really needed. Also, there is no "internal" in the static fileObject declaration, but probably it is actually meant to be public or internal (public would be excessive, so better use internal). But it would mean non-private field, which lacks flexibility. It's better to allow only private fields, non-provide could be just properties and methods and events. For example, working with a static object will require locking if accessed from different threads. You could improve it. Here are the alternatives I can see: 1) Make fileWatcher a member of two forms, but the instance member. Create it outside the forms and pass the reference to both. As this is a reference type, you will have the same object referenced by two form, even though the two members are separate. 2) From your code, it looks like the use of this field is symmetric in the use of fileWatcher. But usually it is assymmetric: for example: both forms add event data, but only one uses it. In this case, keep this object only in one form, the one which uses it more. Provide and implement some interface used only by the second (less using) form, and provide only the minimally requires set of operations. 3) Advanced way of doing (1). Hide the fileWatcher from both forms. Create a "fileWatchingManager" (or something), encapsulating all possible operations with file watching, including resulting data. Provide some interface for both forms, or two separate interfaces (with common base interface) to pass to each form separately. This interface shall not expose the instance fileWatcher at all; you expose only semantic operations like "Add", "Get", or... whatever. Create its instance in local context (like Main) and pass it two form 1 and form 2. The reference on local context will be lost (unreachable), but the two form will, again, have two referenced to the same object. Use the benefit of having reference-type object and CG -- no need to worry what is the composition for what. Any questions? --SA
Member 4408432 at 13-Nov-12 16:30pm
   
First of all, thank You for Your time and quick reply. Second of all i do apologise for late reply, my work requires a lot of traveling so i have to prioritise. Ive got no questions, your explanation is very clear for me. Im glad there are still ppl who are willing to explain something without taking things for granted or abusing other ppl by saying "you should know this" or "google" it. Again thank you - Wajrak
Sergey Alexandrovich Kryukov at 13-Nov-12 17:51pm
   
You are very welcome. No need to apologize -- late reply is not an issue at all. Most members get notifications on every comments on our posts, so this is not a problem at all. It's very good that you came back, accepted the advice and provided some nice feedback -- thank you. (Too many members abandon their questions when more information is required or for some unknown reasons, which means waster time, unfortunately.) It's a pleasure to deal with someone who really can use the help and get some useful results. Unfortunately, not too many inquirers these days can really use help, and many are not really qualified to ask questions. That said, you should understand it and not take the answers like "Google it" or "you should know this" as offense. Your question and comments are quite adequate -- just the mere fact that your question was not down-voted or reported for abuse :-) -- yes, these days most questions are so bad that they receive such negative feedback, many are removed (I wish we could remove a lot more), for a good reasons -- many people are not qualified even to ask questions. Some minor part of inquirers start talking in a rude way right during asking questions, which is a considerable problem. You see, I think the purpose is helping people, not just answering questions, which may or may not deserve answers. In certain select cases, even the simple "don't waste your time and get to work" could be more essential help then answering some "question". As to "Google it", even this advice along can be better than some particular answer. Don't think such advice carries no useful information. In many cases, it does, as it informs someone: "you thought that your problem is unique, but in fact many solutions are available; and they are good enough". And, to be fair, many experts here provide "let me Google it for you" links, understanding that some members are not familiar with best keywords on the topic. Please see some questions and answers to get an idea. At the same time, I should admit that considerable number of answers are not adequate. But other members can see that and usually provide better alternatives... Thank you for understanding. Good luck, call again. --SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,838
1 OriginalGriff 313
2 Tadit Dash 300
3 Sergey Alexandrovich Kryukov 250
4 Peter Leow 220
0 Sergey Alexandrovich Kryukov 9,440
1 OriginalGriff 5,618
2 Peter Leow 4,280
3 Maciej Los 3,540
4 Abhinav S 3,363


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 26 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid