passing data from form2 to form1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajith3000
    New Member
    • Mar 2010
    • 5

    passing data from form2 to form1

    i have been devoloping a project using VB.NET,i am having a problem with passing a value from a newer form to the previous one.i have a form called doctormaintance (lets call this as form1) in that i have to select a specilasation filed for a doctor. this one i done in a separate form(form2). so when i select the reqrd specilisation for a doctor it should appear in the textbox in the previous form. i done it by creatin an object of form1 in form 2. so the data i selected is appearing in the text box,but the other fields which i entered in the form1 is getting cleared.i came to know this is because a new object form is created,so is there is a way to preserve the data i entered in the form 1 ...can i do this with out creating a new object of the form1

    i added the two forms screenshot
    Attached Files
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    How do I get my Form2 to react to something on my Form1?
    How do I make my Form1 control something on my Form2?
    Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
    It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
    It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
    Events tutorial (including Form to Form which is the same as class to class)
    This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.
    Bad: Directly accessing controls of one class/form from another.
    Code:
    bool IsOn = Form2.button1.IsChecked;


    Good: Use a property to get such information
    Code:
    //Form1
    bool IsOn = Form2.IsOn;
    Code:
    //Form 2
    public bool IsOn
    {
       get { return button1.Checked; }
       set { button1.Checked = value; }
    }
    It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: No break in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden on not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentali ze the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

    Code:
    Form2
    public bool IsOn
    {
       get { return btnMeaningfulName.Checked; }
       set {
                btnMeaningfulName.Checked = value;
                panelDashboard.Visible = value;
                labelStatus = value == true ? "On" : "Off";
                btnRunNow.Enabled = value;
    }
    Now when Form1 tells Form2 to turn on, Form2 will check a box, make an entire panel visible, change its Status label to say 'On' and enable a Run Now button.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Originally posted by ajith
      i'm sorry i cant see the reply you posted for my doubt..pls help me,where i can see it..please help
      What do you mean "you can't see the reply"? Then how do you know I replied?

      Also, please respond in the question thread, not in private messages. That way everyone can benefit from the answers. It's not reasonable for us all to have LOTS of threads and LOTS of private messages about those threads: It would just be impossible to keep track of.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by ajith
        when i click the link i'm getting the followin message

        "Invalid Thread specified. If you followed a valid link, please notify the administrator"
        There are only two links in my response and they both seem to work fine.



        If anyone else is having a problem with these links please speak up. I use these links quite often and nobody has spoken of a problem before.

        Comment

        • ajith3000
          New Member
          • Mar 2010
          • 5

          #5
          thank you

          sorry i dint know hw to see the thread i posted,i knew you replied to my thread from my inbox in Bytes.thanks for helping out and for your patience

          Comment

          Working...