10 Replies - 770 Views - Last Post: 03 July 2012 - 02:46 AM Rate Topic: -----

#1 fullyunknown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 23-March 12

Grab Data from Form1 while on Form2

Posted 02 July 2012 - 03:39 PM

I have read the tutorial and can pass data from Form1 to Form2 but I am trying to find out if it is at all possible to be on Form2 and grab data that is on Form1.

What I am trying to do...

3 Forms
MainForm
ReportForm
BuildForm

MainForm
On the MainForm I have a menustrip with open and build. Below the menu strip is a panel. When a user clicks open in menustrip, it opens the ReportForm in the panel.

ReportForm
The ReportForm has a variable called theQuery. For now its default set to test. (Later will be changed based on a text file open.)

BuildForm
The BuildForm contains (for testing) a textbox called texboxQuery.

What I am wanting to happen is a user clicks open. The ReportForm loads all the variables it will store(with more stuff later). I want the user to be able to hit build in the menustrip of the MainForm. Then the BuildForm loads and fills out the textbox: textboxQuery with the data saved on the ReportForm variable theQuery.

So basically the ReportForm is just storing the variables and their data that BuildForm will need. The reason for this, is because I want the ability to open multiply reports. With each report possibly containing a different set of data.

So with how I am doing it.. Is there a way that the BuildForm can actually pull the variable data from the ReportForm? Or is the only way to do it, is create a button on the ReportForm that opens the BuildForm and passes the variables that way?

This post has been edited by fullyunknown: 02 July 2012 - 03:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Grab Data from Form1 while on Form2

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

Re: Grab Data from Form1 while on Form2

Posted 02 July 2012 - 04:12 PM

Quote

I am trying to find out if it is at all possible to be on Form2 and grab data that is on Form1.
Ofcourse.
The easiest way is to pass the form1 to form2 while creating form2,than you can easy access to all data from form1.
passing data between two forms
Was This Post Helpful? 0
  • +
  • -

#3 fullyunknown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 23-March 12

Re: Grab Data from Form1 while on Form2

Posted 02 July 2012 - 04:46 PM

View Postsela007, on 02 July 2012 - 07:12 PM, said:

The easiest way is to pass the form1 to form2 while creating form2,than you can easy access to all data from form1.


Well this is my problem. I actually never open Form2 from Form1. In my example Form2 = BuildForm, Form1 = ReportForm, an then there is a MainForm. I open Form2 through the MainForm as Form1 is just a report form being opened in a Panel on the MainForm.

So I can not pass Form1 to Form2 as the button I am hitting is on the MainForm.

So it basically sounds like I am required to actually have to be on Form1, the form that stores the variables, and click a button on Form1 to send it to Form2?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1922
  • Posts: 5,731
  • Joined: 05-May 12

Re: Grab Data from Form1 while on Form2

Posted 02 July 2012 - 05:23 PM

I've been trying to hold myself back from posting on this thread but I can't stand it anymore. People forgive me if I sound like the crazy old man yelling "Get off my lawn!"

The prime issue is that MS completely failed the developer community with WinForms. WinForms development unfortunately steers programmers to mix view, model, and control code all into one form. This is why time again there is a question about how to pull data from another form. If you take time to at least just separate the model out of the form then you have a fighting chance.

So to wit, here is a basic outline:
class ProgramData {
    public ReportData ReportData;
    public BuildData BuildData;
}

class ReportData {
}

class BuildData {
}

class MainForm : Form {
    public MainForm(ProgramData programData) { ... }
}

class ReportForm : Form {
    public ReportForm(ReportData reportData) { ... }
}

class BuildForm : Form {
    public BuildForm(BuildData buildData, ReportData reportData) { ... }
}



In one of TlhIn'toq tutorials he shows how to keep the xxxData field up-to-date.

I hope this gives you a starting point.

This post has been edited by Skydiver: 02 July 2012 - 05:25 PM

Was This Post Helpful? 1
  • +
  • -

#5 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

Re: Grab Data from Form1 while on Form2

Posted 02 July 2012 - 05:25 PM

Quote

So it basically sounds like I am required to actually have to be on Form1, the form that stores the variables, and click a button on Form1 to send it to Form2?
no, you can be on any form a and access to any data(if its public) from any other form, you just need to send reference to that form.
take a look at this example: form1 is main form,form2 is "opened" from form1, form2 reads data from form1.
form1:
 public partial class Form1 : Form
    {
        public string DataToPass = "form1 data";

        public Form1()
        {
            InitializeComponent();
        }

        private void frm1_Load(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this);
            frm.Show();
        }
    }

form2:
  public partial class Form2 : Form
    {
       private  Form1 FirstForm; 
        public Form2(Form1 f1)
        {
            InitializeComponent();
            FirstForm  = f1;
        }
        private void frm2_Load(object sender, EventArgs e)
        {
            MessageBox.Show(FirstForm.DataToPass);
        }
    }


And if you have 3 forms, and form1 is the main form,and form2 is "opened" from form1,and form3 is opened from form2, and you want to reach data in form3 from form1, you need to pass reference of the form1 through form2 to the form3.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1922
  • Posts: 5,731
  • Joined: 05-May 12

Re: Grab Data from Form1 while on Form2

Posted 02 July 2012 - 05:32 PM

View Postsela007, on 02 July 2012 - 05:25 PM, said:

And if you have 3 forms, and form1 is the main form,and form2 is "opened" from form1,and form3 is opened from form2, and you want to reach data in form3 from form1, you need to pass reference of the form1 through form2 to the form3.


Although that will work, it goes completely against the Law of Demeter. (Although the WikiPedia article is succint, this makes for much better reading: http://www.ccs.neu.e...boy/demeter.pdf )
Was This Post Helpful? 1
  • +
  • -

#7 fullyunknown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 23-March 12

Re: Grab Data from Form1 while on Form2

Posted 02 July 2012 - 06:04 PM

View PostSkydiver, on 02 July 2012 - 08:32 PM, said:

Although that will work, it goes completely against the Law of Demeter. (Although the WikiPedia article is succint, this makes for much better reading: http://www.ccs.neu.e...boy/demeter.pdf )


Thank you for this and the help above. I will definitely look more into it tomorrow. Once again. Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 829
  • Joined: 21-December 11

Re: Grab Data from Form1 while on Form2

Posted 02 July 2012 - 06:18 PM

Quote

Although that will work, it goes completely against the Law of Demeter.
ok, but this is the easiest way i think(for understanding). I'm not sure what Law Of Demeter is talking about, can you point out the irregularities in my example, or show the proper way of doing this by following the Law of Demeter?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Grab Data from Form1 while on Form2

Posted 02 July 2012 - 06:18 PM

See FAQ # 3. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2012
Spoiler

Quote

But when I kick off the background worker and it goes to change a label in this panel


I'll stop you right there. That underlying principal for your program is the problem and just won't work. The worker is a different thread than the one that created the GUI.

Background threads do not, ever, directly access anything that they didn't make. Period. Accept that and start re-designing.

Background worker raises events. You main class that makes this background worker has to subscribe to those events. Then that thread can update its GUI.

This is how is it meant to work. Each 'thing' in a program really should have only one purpose in life and cause no side affects. The background worker should do its job and nothing else. Its job is not to manage the GUI. The GUI thread's job is to manage the GUI.

Think of it this way: Your car dashboard has one job: To be the GUI for your car. But if the speedometer dies you don't want that to keep the engine from starting. The speedometer and the engine are 'loosely bound' for a reason. Your program is the same. You need the GUI to run whether or not the background worker does, and you want the background worker to run regardless which GUI you have on display.

So the background worker is kept ignorant of which panel you have up. It just says "I'm 50% done". If you have the simple panel or the complex panel on display doesn't matter. It hears the event and does whatever it is designed to do to display those results. Maybe the simple panel uses a progress bar, while the advanced panel display the exact percentage. The background thread doesn't know or care how it is shown.

Quote

So my main application should make a dozen threads to update the dozen similar controls?


To me that sounds like the main application is doing too much work and micromanaging the other controls. If the main application is coded for 12 background threads and panels, what happens in 6 months when you want to add a couple more panels? You have to update all your infrastructure to support them.

Personally I would make UserControls that consist of the Panel, its own backgroundworker and any pure objects that you might want to serialize for later restoration. The idea is to make a complete and fully self-contained 'Lego Block' of functionality. Then use your application to just hook them all together.

Now you can place 2 of your custom controls on screen... or 20... and they each take complete care of themselves. Only when one of your custom controls raises an event like LogThis(string message) or HighLevelWarning(int level) does the application have to do anything. The bulk of the work is done within the UserControl

In each of these UserControls I would create methods for Start() and Stop(). Let the UserControl handle its own needs.

Your main application should do nothing more than:

StartAllPanels()
{
   foreach(CustomPanel cp in myPanelsCollection)
   {
      cp.Start(); // Tell the panel to start
      cp.WarningMessage += WarningHandlerMethod; // Subscribe to warning event
      cp.LogThis += LogMessageHandlerMethod; // Subscribe to logging event
   }
}


See how your main application has no idea what the panel needs to do to start up? It doesn't try to micromanage the panel and tell it to start it's backgroundworker. The main app is blissfully ignorant of the inner needs of the component. Its like a the driver of a car. The driver doesn't tell the car: Close 12v power, start fuel pump, begin ignition computer, calibrate fuel/air mix, confirm foot is on brake, start ignition... The driver just turns the key to Start(). Your application should be the same. It only needs to know what it NEEDS to know. Let the rest of the parts take on as much responsibility as you can push on them.

Spending a little time on these tutorials before writing any more code should help you with a better understanding of all this and enable you to architect a better blueprint for your application. Work smarter not harder.

The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Separating data from GUI - PLUS - serializing the data to XML
WPF version (WPF-MVVM data binding)
Passing values between forms/classes
Decouple your multi-threaded work from the GUI so forms don't hang
[/spoiler]

  • Q: What are lambdas? What Is (Obj, E) => ?
    A: Excellent article by Curtis Rutland

  • Q: ... how to do x, y, z with the HTML that comes out of a website ...
    A: Check out the HTML Agility Pack. Yes its free.

  • Q:... how to do x,y,z with a database {probably for the first time}...
    A: Read this tutorial
    Entire section of tutorials
    Parameterizing Your SQL Queries: The RIGHT Way To Query A Database.
    Using SqlDependency to monitor SQL database changes

  • Q: Can someone explain bitwise operations?
    A: Terrific explanation by CodingSup3rnatur@l-360. Be sure to +1 him for this thread.






    I'm lost while learning xyz... I don't know where to begin...

  • Q:I'm trying to teach myself programming. I'm writing a program to do xyz, rather than following a guided self teaching book or lesson plan or on-line tutorial because I just don't learn from books.
    A:
    Spoiler




  • Q: I'm making a maze generator/solver and...
    A: http://www.codeproje...enerator-Solver


  • Q: I am making a calculator as my school homework project and ...
    A: Calculator tutorial

  • Q: I've been struggling with this for days/weeks and I can't figure it out and my professor is worthless and can't teach. Can someone here explain it to me?
    A:
    Spoiler



  • Q: I am doing a dice game for a school homework and...
    A:Drawing dice on the console



  • Q:

    Quote

    Hi,I'm currently learning C# in class at the moment and I'm a little bit stuck on what this question means.[...]show me where I can get help,

    A:
    Spoiler


  • Q: I want to write a program that does x, y and z with features for A, B, and C. {Notice there is no question in this question}
    A:
    Spoiler


  • Q: I need to code to do x... Someone tell me the code for y...
    A:
    Spoiler



    Discussion topics:

  • Q: What is the best programming language/OS? What programming language/OS should I learn?
    A:
    Spoiler


  • Q: How do I become a programmer?
    A: Click the link

  • Q: I'm not really sure what I want to do with my future? Do you guys like programming? I think I kinda like math and games and computers? What should I do or study?
    A:
    Spoiler


  • Q: What's your installation like? What sofware do you use? What are your Visual Studio extensions?
    A:
    Spoiler






    Working for myself/Deploying/Selling software:

  • So you want to be a game programmer...

  • Q:How do I stay a free lance coder for a living?
    A:http://justcreatived...eelancing-life/

  • Q: How do I become a better coder?
    Read this, and practice a lot.
    How to be a better coder

  • Q: Are there any resources for small C# projects a novice to become a better coder?
    A:
    Spoiler



  • Q: How do I sell the software I've developed?
    A:http://www.dreaminco...m_notifications

  • Q: How do I decide how much to charge for my application?
    A: Read this
    A lot of questions about freelancing were covered in the Q&A with the Experts thread

  • Q: How do I deploy my program / Make an installer?
    [b]A:
    C# application deployment project
    Include the smallest .NET possible

  • [/spoiler]


    Was This Post Helpful? 0
    • +
    • -

    #10 Skydiver  Icon User is online

    • Code herder
    • member icon

    Reputation: 1922
    • Posts: 5,731
    • Joined: 05-May 12

    Re: Grab Data from Form1 while on Form2

    Posted 02 July 2012 - 07:03 PM

    View Postsela007, on 02 July 2012 - 06:18 PM, said:

    Quote

    Although that will work, it goes completely against the Law of Demeter.
    ok, but this is the easiest way i think(for understanding). I'm not sure what Law Of Demeter is talking about, can you point out the irregularities in my example, or show the proper way of doing this by following the Law of Demeter?


    A simplistic way to restate the "Law of Demeter" is "use only one dot". It is a design principle that encourages loose coupling of components. I highly recommend that you read the PDF I linked above because it is good reading and it will serve you well in the years to come.

    So in your example with Form1, Form2, and Form3, you would be in this situation:
    class Form1 {
        public string DataToPass = "Harry";
    }
    
    class Form2 {
        public Form1 FirstForm;
        public Form2(Form1 form1) {
            FirstForm = form1;
        }
    }
    
    class Form3 {
        public Form2 SecondForm;
        public Form3(Form2 form2) {
            SecondForm = form2;
        }
    
        void DoSomethingWithDataToPass() {
            SecondForm.FirstForm.DataToPass = "Voldemort";   //!!! We used more than one dot.
        }
    }
    
    


    This type of code establishes tight coupling between forms 1, 2, 3. Form3 needs to know that Form2 has a public Form1 reference. Form2 has to have a reference to Form1 even if it doesn't really need it, but just for the sake of Form3.

    Or put another way is to quote from Meyers from "Effective C++": "Friends don't let friends play with their privates.". If you sit down and think about it, why does DataToPass, FirstForm, and SecondForm have to be public. They should really be private because they should only concern those classes. But now to effect the data passing you make the privates public (which is effectively what 'friend' does in C++).

    I hope that all makes sense.
    Was This Post Helpful? 1
    • +
    • -

    #11 sela007  Icon User is offline

    • D.I.C Addict

    Reputation: 137
    • View blog
    • Posts: 829
    • Joined: 21-December 11

    Re: Grab Data from Form1 while on Form2

    Posted 03 July 2012 - 02:46 AM

    View PostSkydiver, on 02 July 2012 - 07:03 PM, said:

    I hope that all makes sense.

    Thank you, now its much clearer.
    Was This Post Helpful? 0
    • +
    • -

    Page 1 of 1