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. How to access methos of one form from another?

How to access methos of one form from another?

Scheduled Pinned Locked Moved C#
tutorialquestion
6 Posts 3 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.
  • T Offline
    T Offline
    TrooperIronMan
    wrote on last edited by
    #1

    For example I'm buiding Find/Search function in new form and I want to be able to search for sting in parent form from this child form (child form have text box where user enter word he is looking for)? Parent form have multi Rich Text Box(es) (in tabs)... menus etc So this is probably simple I tried few things but none works... I guess I'm just doing something wrong. Thanks :)

    L E 2 Replies Last reply
    0
    • T TrooperIronMan

      For example I'm buiding Find/Search function in new form and I want to be able to search for sting in parent form from this child form (child form have text box where user enter word he is looking for)? Parent form have multi Rich Text Box(es) (in tabs)... menus etc So this is probably simple I tried few things but none works... I guess I'm just doing something wrong. Thanks :)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      If you have a Form (say mainForm) containing text, and you want an interactive search facility, you could create a dialog using a new Form (say findDialog) which has mainForm as parent (so it hides when mainForm hides), and which is shown as modeless dialog (i.e. it can give up focus to other windows in same app). Now mainForm can have button event handlers attached to the buttons of findDialog, read an editTextBox on findDialog, and do whatever is required within mainForm. So the trick is in mainForm attaching event handlers to another form's buttons, but then it did create that other form (findDialog) so it has a reference to it. Hence it boils down to one of the following: - make the Controls on findDialog public (is not what Visual Studio Designer does by default, but there is a "Modifiers" field on the properties panel) - add public properties to get access to the non-public controls you need - add one or more public methods to findDialog to attach an event handler to the non-public controls you need. The choice is yours. Personaly I often create dialogs programmatically (rather than through Designer) and make all controls public. :) -- modified at 14:05 Saturday 6th January, 2007

      Luc Pattyn

      T 1 Reply Last reply
      0
      • T TrooperIronMan

        For example I'm buiding Find/Search function in new form and I want to be able to search for sting in parent form from this child form (child form have text box where user enter word he is looking for)? Parent form have multi Rich Text Box(es) (in tabs)... menus etc So this is probably simple I tried few things but none works... I guess I'm just doing something wrong. Thanks :)

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        Event Listeners are another option. Personally I usually use Dialogs, which has already been said.


        On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

        1 Reply Last reply
        0
        • L Luc Pattyn

          If you have a Form (say mainForm) containing text, and you want an interactive search facility, you could create a dialog using a new Form (say findDialog) which has mainForm as parent (so it hides when mainForm hides), and which is shown as modeless dialog (i.e. it can give up focus to other windows in same app). Now mainForm can have button event handlers attached to the buttons of findDialog, read an editTextBox on findDialog, and do whatever is required within mainForm. So the trick is in mainForm attaching event handlers to another form's buttons, but then it did create that other form (findDialog) so it has a reference to it. Hence it boils down to one of the following: - make the Controls on findDialog public (is not what Visual Studio Designer does by default, but there is a "Modifiers" field on the properties panel) - add public properties to get access to the non-public controls you need - add one or more public methods to findDialog to attach an event handler to the non-public controls you need. The choice is yours. Personaly I often create dialogs programmatically (rather than through Designer) and make all controls public. :) -- modified at 14:05 Saturday 6th January, 2007

          Luc Pattyn

          T Offline
          T Offline
          TrooperIronMan
          wrote on last edited by
          #4

          OK I like idea, I did change properties to buttons and textbox in child to public, so I can now access them within main form, but how do I add event handler to button from child form in main form? Sorry for trivial question but I'm new in this. Also does anyone have idea why I can't access richtextbox in main form from child form it's labeled as public also? I can for example access text box in child form from main form, but it's of no use, I need to attach that event so it does that on button click not before or after.

          L 1 Reply Last reply
          0
          • T TrooperIronMan

            OK I like idea, I did change properties to buttons and textbox in child to public, so I can now access them within main form, but how do I add event handler to button from child form in main form? Sorry for trivial question but I'm new in this. Also does anyone have idea why I can't access richtextbox in main form from child form it's labeled as public also? I can for example access text box in child form from main form, but it's of no use, I need to attach that event so it does that on button click not before or after.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            No no, the simplest way is the other way around, it is the main form that does everything about the text, including the searching, the findDialog's purpose is to specify and trigger the search, not to perform it. And the architecture would be something like this (incomplete!):

            public class mainForm : Form {

            FindDialog findDialog;	// reference to the find dialog
            
            public mainForm() {	// constructor
            	// ...
            }
            
            // this could be called by clicking a "Open Find" button on mainForm
            public void btnOpenFind\_Click(object sender, EventArgs e) {
            	findDialog=new FindDialog();
            	findDialog.btnFind.Click+=new EventHandler(btnFind\_Click);
            	findDialog.btnClose.Click+=new EventHandler(btnClose\_Click);
            	findDialog.ShowDialog();
            }
            private void btnFind\_Click(object sender, EventArgs e) {
            	// read findDialog.tbSearch.Text
            	// and perform search in the text inside mainForm
            }
            private void btnClose\_Click(object sender, EventArgs e) {
            	// do postprocessing if needed (e.g. preserve current settings),
            	// then
            	findDialog.Close();
            }
            

            }

            public class FindDialog : Form {
            public Button btnFind; // will start the search
            public Button btnClose; // closes the dialog
            public TextBox tbSearch; // holds what to search
            }

            Hope this helps you way forward.

            Luc Pattyn

            T 1 Reply Last reply
            0
            • L Luc Pattyn

              No no, the simplest way is the other way around, it is the main form that does everything about the text, including the searching, the findDialog's purpose is to specify and trigger the search, not to perform it. And the architecture would be something like this (incomplete!):

              public class mainForm : Form {

              FindDialog findDialog;	// reference to the find dialog
              
              public mainForm() {	// constructor
              	// ...
              }
              
              // this could be called by clicking a "Open Find" button on mainForm
              public void btnOpenFind\_Click(object sender, EventArgs e) {
              	findDialog=new FindDialog();
              	findDialog.btnFind.Click+=new EventHandler(btnFind\_Click);
              	findDialog.btnClose.Click+=new EventHandler(btnClose\_Click);
              	findDialog.ShowDialog();
              }
              private void btnFind\_Click(object sender, EventArgs e) {
              	// read findDialog.tbSearch.Text
              	// and perform search in the text inside mainForm
              }
              private void btnClose\_Click(object sender, EventArgs e) {
              	// do postprocessing if needed (e.g. preserve current settings),
              	// then
              	findDialog.Close();
              }
              

              }

              public class FindDialog : Form {
              public Button btnFind; // will start the search
              public Button btnClose; // closes the dialog
              public TextBox tbSearch; // holds what to search
              }

              Hope this helps you way forward.

              Luc Pattyn

              T Offline
              T Offline
              TrooperIronMan
              wrote on last edited by
              #6

              It works thanks, can someone explain what we did here (I understand basic), so we can add event to current object to instance in another object?

              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