Results 1 to 9 of 9

Thread: Referencing forms in both directions

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2016
    Posts
    41

    Referencing forms in both directions

    Hi All,
    I have a vb.net solution, which has two projects in it.

    In Project A i have added a reference to Project B. From Project A i can now run procedures/show forms etc that are in Project B.

    How do you reference the other way, so Project B calls procedures etc in Project A?

    If you try and reference it the other way round this isn't allowed as it causes a circular dependancy.

    Many thanks,
    Sijcooke.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Referencing forms in both directions

    You don't. If you think you need to then you're doing it wrong. If you explain the functionality that you're trying to achieve, rather than how you're trying to achieve it, then we can probably explain the right way to do it. The answer may be a third project that gets referenced by the other two and contains the common types, or it may be something else. For instance, we often see cases where Form1 opens Form2 and someone thinks that Form2 should then make changes directly on Form1. That approach is wrong. Form2 should even know that Form1 exists, which means that, should they be in different projects, the project that Form2 is in doesn't need to know about the project that Form1 is in. Form2 should simply make the appropriate data available and Form1 retrieves that data and makes its own changes to itself.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2016
    Posts
    41

    Re: Referencing forms in both directions

    Hi, Thanks for the reply. Sorry I do not understand. I have searched online and people say about making the data available in variables and then reading these variables. This seems like far more code than is necessary.

    For many years i have always done similar to this, and it always works - never a problem, and minimal code.

    Just create a couple forms (form1 and form2).
    Put a textbox on Form1.
    On form2 add a button.
    This buttons click event has some code like this: msgbox(form1.textbox1.text) or form1.textbox1.text = "New text"

    This works fine!

    From what i read online, am i right in saying you should...

    Public textboxvalue as string in form1 or in a module.
    Add the following code to textbox1.textchanged(): textboxvalue = textbox1.text

    In Form2.button.click have something like msgbox(textboxvalue) or textboxvalue = "New text"
    If the code is textboxvalue = "New text", then somewhere maybe in form1 i would need to set textbox1.text = textboxvalue

    Any help really is appreciated. I want to code in the correct way, but everyone seems to say the way i do it is wrong.. but they don't say why it is wrong! It seems good to me with minimal code.

    Thanks

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Referencing forms in both directions

    What is the reasoning for having two separate projects?

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2016
    Posts
    41

    Re: Referencing forms in both directions

    Quote Originally Posted by OptionBase1 View Post
    What is the reasoning for having two separate projects?
    Just to split the application up into seperate dlls to make it easier to manage/work with - class library.
    I assume this is the right thing to do!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Referencing forms in both directions

    I agree with OB1.

    However, what JMC stated IS more code than is absolutely necessary. That doesn't make it wrong.

    The point about classes is that they should be independent of one another. Forms are just classes. You can certainly have Form1 reach into Form2, and have Form2 reach into Form1, but at that point, they are so entwined that you'll never get them apart. There are times when that doesn't matter, but if that's the case, then there certainly isn't any reason to have Form1 and Form2 in different projects. They are utterly dependent on each other, so even attempting to separate them is going to be painful.

    It's also fairly difficult for two forms to be mutually interdependent unless you are relying on the default instances of the form, which is a bad idea for other reasons. If you are working with non-default instances, then if Form1 creates and shows Form2, Form1 has a reference to Form2, which it can store. Form2 has no idea about Form1, though, so for it to know about Form1, you'd have to pass the instance of Form1 to Form2 in either the constructor or by setting a property...or by calling some specialized method for that purpose. That's why forms aren't mutually interdependent unless you rely on the default instance. If you DO rely on the default instance, then in Form2 you can call a method on Form1 with Form1.YourMethod. Of course, that is using the default instance with all the logical baggage that comes with that.

    If you are using default instances, then there is even more reason not to have Form1 and Form2 be in separate projects.
    My usual boring signature: Nothing

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Referencing forms in both directions

    Quote Originally Posted by sijcooke View Post
    Just to split the application up into seperate dlls to make it easier to manage/work with - class library.
    I assume this is the right thing to do!
    It can be....or not.

    In general, keeping code organized is a good thing. However, it's like any other filing system. If it is a good system, then it is organized. If every item is in a different folder, then all you've really done is increased the complexity, and finding any item may end up being harder than if they were all in one folder. So, whether or not it is the right thing to do depends largely on whether or not the libraries are well organized. There's no solid rule about that, either. Some of my projects have only one dll, some have many. For the ones that have had many, there are some elements that have bounced around from one dll to another as I was coming to better understand the relationship between elements.
    My usual boring signature: Nothing

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Referencing forms in both directions

    Quote Originally Posted by sijcooke View Post
    Just to split the application up into seperate dlls to make it easier to manage/work with - class library.
    I assume this is the right thing to do!
    I trust you recognize that there is a level of irony with that reasoning considering you probably wouldn't even have this issue if you were using a single project.

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2016
    Posts
    41

    Re: Referencing forms in both directions

    Thanks for the replies.

    After reading, i think i can remove one of the projects, and bring this code into the main project.
    There is however one other project in the solution, which i kept seperate as this code/form is an 'addon' to the main project. My thinking is that some pc's running the application will have the addon, and some will not.. it is better to keep it as a seperate dll from the core application.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width