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. communication between two child forms in C#

communication between two child forms in C#

Scheduled Pinned Locked Moved C#
csharpquestion
9 Posts 2 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.
  • U Offline
    U Offline
    User 13293914
    wrote on last edited by
    #1

    I want to perform some communication between two child forms that are each opened from a parent form (separate button to show each form). I want to change the visibility and value of a numericalUpDown and also the visibility and text of a textbox. I have used properties, the get part is working perfectly, but the set part works only on the child.show() event, e.g. when I click on Child1Form.button to show the Child2Form the "set" part works, but if I do not show Child2Form, it does not work. and i also do not want to use from setting files. I used form event handling. The method has gotten fired and the message has shown correctly, but the changes in text and visibility have not. On the parents side where I call the publisher form, means Child3 form:

    Child3 child3 = new Child3();
    Child1 child1 = new Child1();
    child3.Child1Button2Clicked += child1.child3_Child1Button2Clicked;
    child3.Show();

    On the subscriber(listener) side: Event Handling between two child forms that each open from the same parent (Two child - one parent).

    public void child3_Child1Button2Clicked(object sender, FormsCommunication e)
    {
    NumericalUD1.Visible = e.NumericalUDPass;
    textBox1.Text = e.textBoxPass;
    MessageBox.Show("it got fire" + ": " + e.NumericalUDPass.ToString() + ": " + e.textBoxPass.ToString());
    }

    On the publisher side:

    public event EventHandler Child1Button2Clicked;

    //button click event to set the values and fire the method.

    private void Child3Btn1_Click(object sender, EventArgs e)
    {
    FormsCommunication formsCommunication = new
    FormsCommunication("2782",Convert.ToBoolean(false));

    OnChild1Button2Clicked(formsCommunication);
    

    }

    protected virtual void OnChild1Button2Clicked(FormsCommunication e)
    {
    Child1Button2Clicked?.Invoke(this, e);
    }

    //the constructor in the EventArgs Class:

    public FormsCommunication(string textBox, bool NumericalUD)
    {
    textBoxPass = textBox;
    NumericalUDPass = NumericalUD;
    }

    public string textBoxPass { get; private set; }
    public bool NumericalUDPass { get; private set; }

    OriginalGriffO 1 Reply Last reply
    0
    • U User 13293914

      I want to perform some communication between two child forms that are each opened from a parent form (separate button to show each form). I want to change the visibility and value of a numericalUpDown and also the visibility and text of a textbox. I have used properties, the get part is working perfectly, but the set part works only on the child.show() event, e.g. when I click on Child1Form.button to show the Child2Form the "set" part works, but if I do not show Child2Form, it does not work. and i also do not want to use from setting files. I used form event handling. The method has gotten fired and the message has shown correctly, but the changes in text and visibility have not. On the parents side where I call the publisher form, means Child3 form:

      Child3 child3 = new Child3();
      Child1 child1 = new Child1();
      child3.Child1Button2Clicked += child1.child3_Child1Button2Clicked;
      child3.Show();

      On the subscriber(listener) side: Event Handling between two child forms that each open from the same parent (Two child - one parent).

      public void child3_Child1Button2Clicked(object sender, FormsCommunication e)
      {
      NumericalUD1.Visible = e.NumericalUDPass;
      textBox1.Text = e.textBoxPass;
      MessageBox.Show("it got fire" + ": " + e.NumericalUDPass.ToString() + ": " + e.textBoxPass.ToString());
      }

      On the publisher side:

      public event EventHandler Child1Button2Clicked;

      //button click event to set the values and fire the method.

      private void Child3Btn1_Click(object sender, EventArgs e)
      {
      FormsCommunication formsCommunication = new
      FormsCommunication("2782",Convert.ToBoolean(false));

      OnChild1Button2Clicked(formsCommunication);
      

      }

      protected virtual void OnChild1Button2Clicked(FormsCommunication e)
      {
      Child1Button2Clicked?.Invoke(this, e);
      }

      //the constructor in the EventArgs Class:

      public FormsCommunication(string textBox, bool NumericalUD)
      {
      textBoxPass = textBox;
      NumericalUDPass = NumericalUD;
      }

      public string textBoxPass { get; private set; }
      public bool NumericalUDPass { get; private set; }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Do yourself a favour and scrap that - form2 shouldn't even know form 3 exists, much less try to modify it's behaviour. See here: Transferring information between two forms, Part 3: Child to Child[^] Form1 needs to know they both exist because it opens them - so all communication goes via the parent.

      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      U 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Do yourself a favour and scrap that - form2 shouldn't even know form 3 exists, much less try to modify it's behaviour. See here: Transferring information between two forms, Part 3: Child to Child[^] Form1 needs to know they both exist because it opens them - so all communication goes via the parent.

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        U Offline
        U Offline
        User 13293914
        wrote on last edited by
        #3

        Thank you so much for your help. Is it safe to use from setting files in the user scope and share it between two text boxes for example?

        OriginalGriffO 1 Reply Last reply
        0
        • U User 13293914

          Thank you so much for your help. Is it safe to use from setting files in the user scope and share it between two text boxes for example?

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Why would you want to do something that cumbersome? Perhap you need to explain in more detail exactly what you are trying to do?

          Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          U 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Why would you want to do something that cumbersome? Perhap you need to explain in more detail exactly what you are trying to do?

            Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            U Offline
            U Offline
            User 13293914
            wrote on last edited by
            #5

            Hi, thanks a million for your reply. Sometimes there are many data that i want to share between two forms. So it is easier to use from properties.settings.default (for me). that works very good and i can save and retrieve my data in the most simple way. So sometimes i prefer to use from them. setting files with user scope, are accessible all over the program directly. So i am doubtful about their security hazards. That is why i have asked the question.

            OriginalGriffO 1 Reply Last reply
            0
            • U User 13293914

              Hi, thanks a million for your reply. Sometimes there are many data that i want to share between two forms. So it is easier to use from properties.settings.default (for me). that works very good and i can save and retrieve my data in the most simple way. So sometimes i prefer to use from them. setting files with user scope, are accessible all over the program directly. So i am doubtful about their security hazards. That is why i have asked the question.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Do you have any idea how inefficient that is? Every time you want to access items in memory, you will be going via a hard drive! You do realise that Properties.Settings is backed by an XML file, so every time you write to them there is a heck of a lot of "read the file, copy it up to the parameter we want, write the new value instead, copy the rest of the file" going on behind the scenes? And reading it means "read the file, scan it for the parameter we want, copy the value" each time? And that if you want to use the same class in two places, they will clash on the settings file because it only knows about one parameter? Start doing it a lot - and you will, because "it works" - and you will slug the heck out of your computer. Learn to do it properly, and it not only works better, but is considerably more maintainable.

              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              U 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Do you have any idea how inefficient that is? Every time you want to access items in memory, you will be going via a hard drive! You do realise that Properties.Settings is backed by an XML file, so every time you write to them there is a heck of a lot of "read the file, copy it up to the parameter we want, write the new value instead, copy the rest of the file" going on behind the scenes? And reading it means "read the file, scan it for the parameter we want, copy the value" each time? And that if you want to use the same class in two places, they will clash on the settings file because it only knows about one parameter? Start doing it a lot - and you will, because "it works" - and you will slug the heck out of your computer. Learn to do it properly, and it not only works better, but is considerably more maintainable.

                Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                U Offline
                U Offline
                User 13293914
                wrote on last edited by
                #7

                Hi, I do not know how should i thanks from you for your help and guide. Can you give me a link of an article that how should i save and retrive my data in the minimum time and what the best way is? .thanks a million.

                OriginalGriffO 1 Reply Last reply
                0
                • U User 13293914

                  Hi, I do not know how should i thanks from you for your help and guide. Can you give me a link of an article that how should i save and retrive my data in the minimum time and what the best way is? .thanks a million.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Well, the way I'd do it is to create a separate class to act as a "data transfer" class and fill an instance of that in the property getter. That way, all the related data is together, and in its correct data format ready to be used. You presumably know how to create a class?

                  Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  U 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Well, the way I'd do it is to create a separate class to act as a "data transfer" class and fill an instance of that in the property getter. That way, all the related data is together, and in its correct data format ready to be used. You presumably know how to create a class?

                    Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                    U Offline
                    U Offline
                    User 13293914
                    wrote on last edited by
                    #9

                    Hi, As i have gotten your point, Yes i know how to create a class, and how to use from get and set. But my means is about save and retrieve data and not about sharing them. But if you means is a different thing, no unfortunately i have no idea about that and if so, do me a favour and guide me through it, please. Thaaaaaaaank you again .

                    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