Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Please Help! Function on Separate Form

Please Help! Function on Separate Form

Scheduled Pinned Locked Moved C#
help
32 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D DJdC

    Hey Christian, OnInit?lifecycle?viewstate? Dude it now seems like my problem is more than I can bare haha. The thing is Form2 has information that Form1 doesn't and sometimes I'd need to pass values too Form1, but first I need a way to link them I guess? They both belong to the same project but Form2 isnt able to call the function. I believe by simply calling the function to be performed as per normal, half my battle is won. Dave said to see posts regarding adding a reference of Form1 to Form2 or something like that? I'm not sure how or if this is it. Thanks man! Please reply!

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #23

    DJdC wrote: OnInit?lifecycle?viewstate? Is it a web app ? If so, stop now and learn what these things mean. If not, just tell me, as I've asked you so many times now. Your namespace suggests it is. DJdC wrote: The thing is Form2 has information that Form1, sometimes I'd need to pass values too, but first I need a way to link them I guess? OK, so Form1 is causing Form2 to be visible ? When you put 'Form1 form = new Form1', you create ANOTHER Form1, it has nothing to do with any other instance that may be visible. DJdC wrote: I believe by simply calling the function to be performed as per normal, half my battle is won. I'm not sure that this is the case, if you don't have a Form1 instance visible ( i.e. it's not a web app, and Form1 creates and shows Form2, or they are somehow both visible on screen at once ), then the battle is lost, there IS no Form1, and therefore no information. DJdC wrote: Dave said to see posts regarding adding a reference of Form1 to Form2 or something like that? Yes. In Form2, make an instance of Form1 as a member variable. Then make the default constructor private, and create a constructor that takes an instance of Form1 as a parameter, and pass in 'this' from Form1 when you create Form2. Then you can access the actual instance of Form1 that is visible from within Form2. Form2: class Form2 { private Form1 parentForm = null; // So it cannot be used private Form2(){}; public Form2(Form1 parent) { parentForm = parent; } } in Form1: Form2 form2 = new Form2(this); form2.ShowDialog(); // Add checks to dialog result if needed Now, if your code does not conform to this pattern ( where it's a windows app and Form1 creates Form2, then you need to further explain your problem. And I'll reiterate that if Form1 and Form2 share some data, it should exist in a seperate class, as a static instance, so that both forms can access it, if both need it, then it belongs to both and not one or the other. Christian Graus - Microsoft MVP - C++

    D 1 Reply Last reply
    0
    • C Christian Graus

      DJdC wrote: OnInit?lifecycle?viewstate? Is it a web app ? If so, stop now and learn what these things mean. If not, just tell me, as I've asked you so many times now. Your namespace suggests it is. DJdC wrote: The thing is Form2 has information that Form1, sometimes I'd need to pass values too, but first I need a way to link them I guess? OK, so Form1 is causing Form2 to be visible ? When you put 'Form1 form = new Form1', you create ANOTHER Form1, it has nothing to do with any other instance that may be visible. DJdC wrote: I believe by simply calling the function to be performed as per normal, half my battle is won. I'm not sure that this is the case, if you don't have a Form1 instance visible ( i.e. it's not a web app, and Form1 creates and shows Form2, or they are somehow both visible on screen at once ), then the battle is lost, there IS no Form1, and therefore no information. DJdC wrote: Dave said to see posts regarding adding a reference of Form1 to Form2 or something like that? Yes. In Form2, make an instance of Form1 as a member variable. Then make the default constructor private, and create a constructor that takes an instance of Form1 as a parameter, and pass in 'this' from Form1 when you create Form2. Then you can access the actual instance of Form1 that is visible from within Form2. Form2: class Form2 { private Form1 parentForm = null; // So it cannot be used private Form2(){}; public Form2(Form1 parent) { parentForm = parent; } } in Form1: Form2 form2 = new Form2(this); form2.ShowDialog(); // Add checks to dialog result if needed Now, if your code does not conform to this pattern ( where it's a windows app and Form1 creates Form2, then you need to further explain your problem. And I'll reiterate that if Form1 and Form2 share some data, it should exist in a seperate class, as a static instance, so that both forms can access it, if both need it, then it belongs to both and not one or the other. Christian Graus - Microsoft MVP - C++

      D Offline
      D Offline
      DJdC
      wrote on last edited by
      #24

      Okay Christian. Its a Windows Application that uses the internet but its nothing to do with ASP.NET . A C# Windows Application. webtest is just the name I gave it I'm sorry if this threw you off. I'm sure you're very familiar with Windows Applications integrating Google APIs? Yes I'm doing this as a project in school. Program works fine, nothing fancy, but I have a new requirement now you see, Supervisor gave me a task to save and retrieve information onto xml files. I haven't really mastered xml but I would later on when I got the foundation to practise with! Form1 is the actual application, I'd make a menu for the user to retrieve saved files via Form2, cause I don't wanna trouble myself of removing everything from Form1 so the user can see my xml information. Form2 is meant to display URLs, of which they are clickable and Form2's job is done. And that my friend is the logic I came up with. You might want to suggest another alternative but I can safely say it would just throw my fragile understanding in the bin, I'm an amateur in programming and I've only touched C# in April haha.

      C 1 Reply Last reply
      0
      • D DJdC

        Okay Christian. Its a Windows Application that uses the internet but its nothing to do with ASP.NET . A C# Windows Application. webtest is just the name I gave it I'm sorry if this threw you off. I'm sure you're very familiar with Windows Applications integrating Google APIs? Yes I'm doing this as a project in school. Program works fine, nothing fancy, but I have a new requirement now you see, Supervisor gave me a task to save and retrieve information onto xml files. I haven't really mastered xml but I would later on when I got the foundation to practise with! Form1 is the actual application, I'd make a menu for the user to retrieve saved files via Form2, cause I don't wanna trouble myself of removing everything from Form1 so the user can see my xml information. Form2 is meant to display URLs, of which they are clickable and Form2's job is done. And that my friend is the logic I came up with. You might want to suggest another alternative but I can safely say it would just throw my fragile understanding in the bin, I'm an amateur in programming and I've only touched C# in April haha.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #25

        DJdC wrote: I'm sorry if this threw you off. That's OK, I'm just trying to piece this together. DJdC wrote: I'm sure you're very familiar with Windows Applications integrating Google APIs? LOL - nope. DJdC wrote: You might want to suggest another alternative but I can safely say it would just throw my fragile understanding in the bin, I'm an amateur in programming and I've only touched C# in April haha. *grin* I'll do my best here. So as I understand it, the functionality you want from Form1 is the stuff that deals with the XML ? There is no reason that this can't be moved to a third class, so both classes can access the XML information from there. In fact, a good design would be to have a class that reads the XML, and then exposes the stuff the XML file stores, so this one file has the job of reading the XML, giving you access to the info, letting you change/add to it, and then saving it. That way, only one part of the code needs to know how your config file is being stored, the rest just knows there is a config file class that it uses. Or, if that's too hard, then what I illustratated, and making methods public, will solve your problem, at the expense of quality in terms of design. You may prefer to come back to that lesson, but a factor is, how smart is your teacher, and will you be marked on quality of design ? Remember, any class can be created any number of times, and any non static property will only have a specific value for a specific instance of the class. Imagine you have a class to represent a student. If every instance of the class had the same name, you could only create one student, right ? Your form is the same. You can make as many Form1 class instances as you like, but only the one you can see on the screen contains information that is the result of things you've done while using the program ( i.e. stuff where a value has been set outside the constructor ). I really hope that helps. Let us know if you need more info. Christian Graus - Microsoft MVP - C++

        D 1 Reply Last reply
        0
        • C Christian Graus

          DJdC wrote: I'm sorry if this threw you off. That's OK, I'm just trying to piece this together. DJdC wrote: I'm sure you're very familiar with Windows Applications integrating Google APIs? LOL - nope. DJdC wrote: You might want to suggest another alternative but I can safely say it would just throw my fragile understanding in the bin, I'm an amateur in programming and I've only touched C# in April haha. *grin* I'll do my best here. So as I understand it, the functionality you want from Form1 is the stuff that deals with the XML ? There is no reason that this can't be moved to a third class, so both classes can access the XML information from there. In fact, a good design would be to have a class that reads the XML, and then exposes the stuff the XML file stores, so this one file has the job of reading the XML, giving you access to the info, letting you change/add to it, and then saving it. That way, only one part of the code needs to know how your config file is being stored, the rest just knows there is a config file class that it uses. Or, if that's too hard, then what I illustratated, and making methods public, will solve your problem, at the expense of quality in terms of design. You may prefer to come back to that lesson, but a factor is, how smart is your teacher, and will you be marked on quality of design ? Remember, any class can be created any number of times, and any non static property will only have a specific value for a specific instance of the class. Imagine you have a class to represent a student. If every instance of the class had the same name, you could only create one student, right ? Your form is the same. You can make as many Form1 class instances as you like, but only the one you can see on the screen contains information that is the result of things you've done while using the program ( i.e. stuff where a value has been set outside the constructor ). I really hope that helps. Let us know if you need more info. Christian Graus - Microsoft MVP - C++

          D Offline
          D Offline
          DJdC
          wrote on last edited by
          #26

          Errrmmm my components function won't have any xml codes. I wanted Form2 to handle all the xmls, and display whatever URL strings necessary, which are clickable, and on clicking it closes Form2 and Form1 runs the components function as per normal, of course with an extra code dealing with this string. The strings are meant to be URLs, and one of the things components() adds is a web browser window which I'd intend to use to navigate to the URL the Form2 string would suggest. I don't know, the logic just seems very clear cut to me I really don't intend to confuse you. And at this point, haha, I won't bother abt how nice my code looks, as long as it works and I understand everything when presentation time comes hahaha.

          C 1 Reply Last reply
          0
          • D DJdC

            Errrmmm my components function won't have any xml codes. I wanted Form2 to handle all the xmls, and display whatever URL strings necessary, which are clickable, and on clicking it closes Form2 and Form1 runs the components function as per normal, of course with an extra code dealing with this string. The strings are meant to be URLs, and one of the things components() adds is a web browser window which I'd intend to use to navigate to the URL the Form2 string would suggest. I don't know, the logic just seems very clear cut to me I really don't intend to confuse you. And at this point, haha, I won't bother abt how nice my code looks, as long as it works and I understand everything when presentation time comes hahaha.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #27

            DJdC wrote: I wanted Form2 to handle all the xmls, and display whatever URL strings necessary, which are clickable, and on clicking it closes Form2 and Form1 runs the components function as per normal, of course with an extra code dealing with this string. So where is the issue ? Form2 returns the string selected, and Form1 uses it ? What does Form1 need to give Form2 again ? DJdC wrote: and one of the things components() adds is a web browser window which I'd intend to use to navigate to the URL the Form2 string would suggest. This method adds a web browser to Form1 ? If you want a web browser in Form2, you need to add it there. Why do you need to add it using code ? DJdC wrote: And at this point, haha, I won't bother abt how nice my code looks, as long as it works and I understand everything when presentation time comes hahaha. It's not about appearance, it's about good design. But sure, I can appreciate that you want to get over the initial learning curve before you worry about that. Christian Graus - Microsoft MVP - C++

            D 1 Reply Last reply
            0
            • C Christian Graus

              DJdC wrote: I wanted Form2 to handle all the xmls, and display whatever URL strings necessary, which are clickable, and on clicking it closes Form2 and Form1 runs the components function as per normal, of course with an extra code dealing with this string. So where is the issue ? Form2 returns the string selected, and Form1 uses it ? What does Form1 need to give Form2 again ? DJdC wrote: and one of the things components() adds is a web browser window which I'd intend to use to navigate to the URL the Form2 string would suggest. This method adds a web browser to Form1 ? If you want a web browser in Form2, you need to add it there. Why do you need to add it using code ? DJdC wrote: And at this point, haha, I won't bother abt how nice my code looks, as long as it works and I understand everything when presentation time comes hahaha. It's not about appearance, it's about good design. But sure, I can appreciate that you want to get over the initial learning curve before you worry about that. Christian Graus - Microsoft MVP - C++

              D Offline
              D Offline
              DJdC
              wrote on last edited by
              #28

              Form1 doesn't give Form2 anything, Form1 simply opens Form2 when necessary, letting them be open simultaneously. Form2 is meant to display clickable strings(I mean URLs) frm xml files, thats all. Once anything is clicked, I want to close Form2 and let components() do its job on Form1. Yes components() adds a web browser on Form1, and no I don't need the browser on Form2, because Form1 has plenty other functions than just calling Form2. So basically I need to know how to link forms I guess? Or at least thats what I think I need to know.

              C 1 Reply Last reply
              0
              • C Christian Graus

                What I don't get is, if it's a web app, why does Form2 need to call a method that pertains to form1 ? I think something else is not right here. Christian Graus - Microsoft MVP - C++

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #29

                It's a web app? I didn't see that anywhere. I'll go back and look... when I get a chance. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                C 1 Reply Last reply
                0
                • D DJdC

                  I have 2 forms, form1 and form2. form1 has a function called components. I need now, for form2 to execute this function on form1 as per normal. I have no clue on form linking, please help me! Thanks!

                  J Offline
                  J Offline
                  Jack Bond
                  wrote on last edited by
                  #30

                  You could pass form2 a reference to form1, and then form2 could simply call components() like it would for any other classes' method. Another approach(and possibly cleaner) would be for form2 to fire an event that form1 is registered to listen to, and then in form1's event handler it could call components().

                  1 Reply Last reply
                  0
                  • D DJdC

                    Form1 doesn't give Form2 anything, Form1 simply opens Form2 when necessary, letting them be open simultaneously. Form2 is meant to display clickable strings(I mean URLs) frm xml files, thats all. Once anything is clicked, I want to close Form2 and let components() do its job on Form1. Yes components() adds a web browser on Form1, and no I don't need the browser on Form2, because Form1 has plenty other functions than just calling Form2. So basically I need to know how to link forms I guess? Or at least thats what I think I need to know.

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #31

                    If Form1 has no information that Form2 needs, then you've been wasting your time all along in Form1 Form2 dlg = new Form2(); if (dlg.ShowDialog() == DialogResult.OK) { // any post processing in here } I assume you've done this all along. If you don't need the browser in Form2, then I'm at a loss as to why you'd want to call the function in form2 ? Christian Graus - Microsoft MVP - C++

                    1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      It's a web app? I didn't see that anywhere. I'll go back and look... when I get a chance. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #32

                      Dave Kreskowiak wrote: It's a web app? No, it's not, but the project is called 'webapplication', because it uses the google API. It sounds like he has no need to pass anything anywhere, I'm not sure where he's been confused. Christian Graus - Microsoft MVP - C++

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups